pvary commented on code in PR #16450: URL: https://github.com/apache/iceberg/pull/16450#discussion_r3380134022
########## flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/sink/dynamic/TestVariantAvroDynamicTableRecordGenerator.java: ########## @@ -0,0 +1,487 @@ +/* + * 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.sink.dynamic; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import org.apache.avro.Schema; +import org.apache.avro.SchemaBuilder; +import org.apache.flink.api.common.functions.DefaultOpenContext; +import org.apache.flink.api.common.functions.util.ListCollector; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.table.data.GenericRowData; +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.RowType; +import org.apache.flink.table.types.logical.VarCharType; +import org.apache.flink.table.types.logical.VariantType; +import org.apache.flink.types.variant.Variant; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.avro.AvroSchemaUtil; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.flink.DataGenerator; +import org.apache.iceberg.flink.DataGenerators; +import org.apache.iceberg.flink.FlinkCreateTableOptions; +import org.apache.iceberg.flink.FlinkWriteOptions; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.junit.jupiter.api.Test; + +class TestVariantAvroDynamicTableRecordGenerator { + + private static final String TEST_DB = "test_db"; + private static final String TEST_TABLE = "test_table"; + private static final String BRANCH = "main"; + private static final Configuration FLINK_CONFIG = new Configuration(); + + @Test + void testMissingRequiredFields() { + List<LogicalType> types = Lists.newArrayList(); + List<String> names = Lists.newArrayList(); + + types.add(new VarCharType(false, 255)); + names.add(FlinkCreateTableOptions.CATALOG_DATABASE.key()); + types.add(new VarCharType(false, 255)); + names.add(FlinkCreateTableOptions.CATALOG_TABLE.key()); + + RowType rowType = RowType.of(types.toArray(new LogicalType[0]), names.toArray(new String[0])); + + assertThatThrownBy( + () -> + new VariantAvroDynamicTableRecordGenerator( + rowType, Collections.emptyMap(), FLINK_CONFIG)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining( + "Missing column data. Expected column data of type VARIANT NOT NULL."); + } + + @Test + void testWithWritePropertiesOverride() throws Exception { + DataGenerator dataGenerator = new DataGenerators.Primitives(); + Variant variantData = dataGenerator.generateFlinkVariantData(); + Schema schema = dataGenerator.avroSchema(); + String schemaVersion = "v1"; + + Map<String, String> writeProperties = Maps.newHashMap(); + writeProperties.put(FlinkCreateTableOptions.CATALOG_DATABASE.key(), "test_db_override"); + writeProperties.put(FlinkCreateTableOptions.CATALOG_TABLE.key(), "test_table_override"); + + RowType rowType = createRowTypeWithRequiredFields(); + VariantAvroDynamicTableRecordGenerator generator = + new VariantAvroDynamicTableRecordGenerator(rowType, writeProperties, FLINK_CONFIG); + generator.open(new DefaultOpenContext()); + + RowData inputRecord = + GenericRowData.of( + variantData, + StringData.fromString(schema.toString()), + StringData.fromString(schema.getName() + schemaVersion)); + + List<DynamicRecord> records = Lists.newArrayList(); + ListCollector<DynamicRecord> collector = new ListCollector<>(records); + generator.generate(inputRecord, collector); + + assertThat(records).hasSize(1); + DynamicRecord record = records.get(0); + assertThat(record.tableIdentifier()) + .isEqualTo(TableIdentifier.of("test_db_override", "test_table_override")); + assertThat(record.branch()).isNull(); + assertThat(record.spec()).isEqualTo(PartitionSpec.unpartitioned()); + assertThat(record.writeParallelism()).isEqualTo(0); Review Comment: shall we test ditribution mode? -- 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]
