swapna267 commented on code in PR #16450: URL: https://github.com/apache/iceberg/pull/16450#discussion_r3358699204
########## flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/data/TestVariantRowDataWrapper.java: ########## @@ -0,0 +1,172 @@ +/* + * 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 org.apache.flink.table.data.ArrayData; +import org.apache.flink.table.data.GenericArrayData; +import org.apache.flink.table.data.GenericMapData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.data.StringData; +import org.apache.flink.table.types.logical.ArrayType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.LogicalTypeRoot; +import org.apache.flink.table.types.logical.MapType; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.types.variant.Variant; +import org.apache.iceberg.flink.DataGenerator; +import org.apache.iceberg.flink.DataGenerators; +import org.junit.jupiter.api.Test; + +public class TestVariantRowDataWrapper { + + @Test + public void testPrimitives() { + testDataGenerator(new DataGenerators.Primitives()); + } + + @Test + public void testStructOfPrimitive() { + testDataGenerator(new DataGenerators.StructOfPrimitive()); + } + + @Test + public void testStructOfArray() { + testDataGenerator(new DataGenerators.StructOfArray()); + } + + @Test + public void testStructOfMap() { + testDataGenerator(new DataGenerators.StructOfMap()); + } + + @Test + public void testStructOfStruct() { + testDataGenerator(new DataGenerators.StructOfStruct()); + } + + @Test + public void testArrayOfPrimitive() { + testDataGenerator(new DataGenerators.ArrayOfPrimitive()); + } + + @Test + public void testArrayOfArray() { + testDataGenerator(new DataGenerators.ArrayOfArray()); + } + + @Test + public void testArrayOfMap() { + testDataGenerator(new DataGenerators.ArrayOfMap()); + } + + @Test + public void testArrayOfStruct() { + testDataGenerator(new DataGenerators.ArrayOfStruct()); + } + + @Test + public void testMapOfPrimitives() { + testDataGenerator(new DataGenerators.MapOfPrimitives()); + } + + @Test + public void testMapOfArray() { + testDataGenerator(new DataGenerators.MapOfArray()); + } + + @Test + public void testMapOfMap() { + testDataGenerator(new DataGenerators.MapOfMap()); + } + + @Test + public void testMapOfStruct() { + testDataGenerator(new DataGenerators.MapOfStruct()); + } + + private static void testDataGenerator(DataGenerator dataGenerator) { + RowType rowType = dataGenerator.flinkRowType(); + Variant variant = dataGenerator.generateFlinkVariantData(); + RowData rowData = dataGenerator.generateFlinkRowData(); + VariantRowDataWrapper variantRowDataWrapper = variantRowDataWrapper(rowType, variant); + compareData(rowType, variantRowDataWrapper, rowData); + } + + private static VariantRowDataWrapper variantRowDataWrapper(RowType rowType, Variant variant) { + VariantRowDataWrapper wrapper = new VariantRowDataWrapper(rowType); + return wrapper.wrap(variant); + } + + private static void compareData(RowType rowType, Object variantRowDataWrapper, Object rowData) { + for (int i = 0; i < rowType.getFieldCount(); i++) { + LogicalType logicalType = rowType.getTypeAt(i); + RowData.FieldGetter fieldGetter = RowData.createFieldGetter(logicalType, i); + + Object variantValue = fieldGetter.getFieldOrNull((RowData) variantRowDataWrapper); + Object rowDataValue = fieldGetter.getFieldOrNull((RowData) rowData); + + switch (logicalType.getTypeRoot()) { + case ROW: + compareData((RowType) logicalType, variantValue, rowDataValue); + return; Review Comment: oops. Thanks for the catch. This fix exposed a corner case with Negative Nanosecond value added in PR , https://github.com/apache/iceberg/pull/15475 . TestCase introduced a time before epoch. `LocalDateTime.of(1969, 12, 31, 23, 59, 59, 987654321)`. where `TimestampData.fromEpochMillis(nanos / 1_000_000L, (int) (nanos % 1_000_000L)) ` doesn't handle those correctly. Changed to `TimestampData.fromEpochMillis(Math.floorDiv(nanos, 1_000_000L), (int) Math.floorMod(nanos, 1_000_000L))` for now. There may be other places too where this needs to be handled , like for millis also . But do we support Negative nanos in general ? -- 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]
