pvary commented on code in PR #17644: URL: https://github.com/apache/iceberg/pull/17644#discussion_r4798500003
########## flink/v2.3/flink/src/test/java/org/apache/iceberg/flink/data/TestFlinkMultisetWrite.java: ########## @@ -0,0 +1,176 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.iceberg.flink.data; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Map; +import org.apache.flink.table.data.ArrayData; +import org.apache.flink.table.data.GenericMapData; +import org.apache.flink.table.data.GenericRowData; +import org.apache.flink.table.data.MapData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.data.StringData; +import org.apache.flink.table.types.logical.IntType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.MultisetType; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.table.types.logical.VarCharType; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.Files; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.encryption.EncryptedFiles; +import org.apache.iceberg.formats.FormatModelRegistry; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.io.DataWriter; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Iterables; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.types.Types; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +class TestFlinkMultisetWrite { + + private static final Map<Object, Integer> STRING_TAGS = ImmutableMap.of("a", 2, "b", 1); + private static final Map<Object, Integer> INT_TAGS = ImmutableMap.of(7, 2, 9, 1); + + @TempDir private Path temp; + + private static Schema schemaWithTags(Type elementType) { + return new Schema( + Types.NestedField.required(1, "id", Types.IntegerType.get()), + Types.NestedField.optional( + 2, "tags", Types.MapType.ofRequired(3, 4, elementType, Types.IntegerType.get()))); + } + + // Built by hand: converting the schema with FlinkSchemaUtil would give a MAP, not a MULTISET + private static RowType rowTypeWithTags(LogicalType elementType) { + return RowType.of( + new LogicalType[] {new IntType(false), new MultisetType(true, elementType)}, + new String[] {"id", "tags"}); + } + + private static RowData rowWithTags(Map<Object, Integer> tags) { + if (tags == null) { + return GenericRowData.of(1, null); + } + + Map<Object, Object> counts = Maps.newHashMap(); + tags.forEach( + (tag, count) -> + counts.put(tag instanceof String ? StringData.fromString((String) tag) : tag, count)); + return GenericRowData.of(1, new GenericMapData(counts)); + } + + private static Map<Object, Integer> tagsOf(RowData row, LogicalType elementType) { + if (row.isNullAt(1)) { + return null; + } + + MapData tags = row.getMap(1); + ArrayData.ElementGetter keyGetter = ArrayData.createElementGetter(elementType); + ArrayData counts = tags.valueArray(); + Map<Object, Integer> result = Maps.newHashMap(); + for (int i = 0; i < tags.size(); i++) { + Object key = keyGetter.getElementOrNull(tags.keyArray(), i); + result.put(key instanceof StringData ? key.toString() : key, counts.getInt(i)); + } + + return result; + } + + private void assertRoundTrip( Review Comment: Taking a look with fresh eyes, what would you think about reusing `TestHelpers.assertRows`: ``` private void assertRoundTrip(FileFormat format, LogicalType elementType, Map<Object, Object> tags) throws IOException { RowType flinkType = RowType.of( new LogicalType[] {new IntType(false), new MultisetType(true, elementType)}, new String[] {"id", "tags"}); Schema schema = new Schema(FlinkSchemaUtil.convert(flinkType).asStructType().fields()); RowData row = GenericRowData.of(1, tags == null ? null : new GenericMapData(tags)); // write with FormatModelRegistry as today, using flinkType and row // then read back: TestHelpers.assertRows( ImmutableList.copyOf(reader), ImmutableList.of(row), FlinkSchemaUtil.convert(schema)); } ``` We could remove `rowWithTags` and `tagsOf`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
