swapna267 commented on code in PR #15471:
URL: https://github.com/apache/iceberg/pull/15471#discussion_r2934183469


##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/sink/dynamic/VariantAvroDynamicTableRecordGenerator.java:
##########
@@ -0,0 +1,223 @@
+/*
+ * 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 java.util.Map;
+import org.apache.avro.Schema.Parser;
+import org.apache.flink.api.common.functions.OpenContext;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+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.flink.util.Collector;
+import org.apache.iceberg.DistributionMode;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.avro.AvroSchemaUtil;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.FlinkCreateTableOptions;
+import org.apache.iceberg.flink.FlinkSchemaUtil;
+import org.apache.iceberg.flink.FlinkWriteOptions;
+import org.apache.iceberg.flink.data.VariantRowDataWrapper;
+import 
org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.base.Splitter;
+
+public class VariantAvroDynamicTableRecordGenerator extends 
DynamicTableRecordGenerator {
+
+  private transient Map<String, Integer> fieldNameToPosition;
+  private static final Splitter COMMA = Splitter.on(',');
+  private transient int maxCacheSize;
+  private transient Map<TableIdentifier, SchemaAndPartitionSpecCacheItem> 
tableCache;
+  @VisibleForTesting static final String DATA_COLUMN = "data";
+  @VisibleForTesting static final String AVRO_SCHEMA_COLUMN = "avro_schema";
+  @VisibleForTesting static final String AVRO_SCHEMA_ID_COLUMN = 
"avro_schema_id";
+  @VisibleForTesting static final String PARTITION_COLUMNS = 
"partition_columns";
+
+  public VariantAvroDynamicTableRecordGenerator(
+      RowType rowType, Map<String, String> writeProperties) {
+    super(rowType, writeProperties);
+
+    String catalogDatabaseColumn = 
FlinkCreateTableOptions.CATALOG_DATABASE.key();
+    Preconditions.checkArgument(
+        rowType.getFieldIndex(catalogDatabaseColumn) != -1
+            || writeProperties().containsKey(catalogDatabaseColumn),
+        "Invalid %s:null.Either %s column should be passed in Row or set in 
table options",
+        catalogDatabaseColumn,
+        catalogDatabaseColumn);
+
+    String catalogTableColumn = FlinkCreateTableOptions.CATALOG_TABLE.key();
+    Preconditions.checkArgument(
+        rowType.getFieldIndex(catalogTableColumn) != -1
+            || writeProperties().containsKey(catalogTableColumn),
+        "Invalid %s:null.Either %s column should be passed in Row or set in 
table options",
+        catalogTableColumn,
+        catalogTableColumn);
+
+    validateRequiredFieldAndType(DATA_COLUMN, new VariantType(false));
+    validateRequiredFieldAndType(AVRO_SCHEMA_COLUMN, new VarCharType(false, 
Integer.MAX_VALUE));
+    validateRequiredFieldAndType(AVRO_SCHEMA_ID_COLUMN, new VarCharType(false, 
Integer.MAX_VALUE));
+  }
+
+  @Override
+  public void open(OpenContext openContext) throws Exception {
+    super.open(openContext);
+
+    this.fieldNameToPosition = fieldNameToPositionMapping();
+
+    String value = 
writeProperties().get(FlinkWriteOptions.CACHE_MAX_SIZE.key());
+    if (value != null) {
+      maxCacheSize = Integer.parseInt(value);
+    } else {
+      maxCacheSize = FlinkWriteOptions.CACHE_MAX_SIZE.defaultValue();
+    }
+
+    this.tableCache = new LRUCache<>(maxCacheSize);
+  }
+
+  @Override
+  public void generate(RowData inputRecord, Collector<DynamicRecord> out) 
throws Exception {
+    String catalogDatabaseColumn = 
FlinkCreateTableOptions.CATALOG_DATABASE.key();
+    String catalogDb =
+        stringTypedColumnValue(
+            inputRecord, catalogDatabaseColumn, 
writeProperties().get(catalogDatabaseColumn));
+
+    String catalogTableColumn = FlinkCreateTableOptions.CATALOG_TABLE.key();
+    String catalogTable =
+        stringTypedColumnValue(
+            inputRecord, catalogTableColumn, 
writeProperties().get(catalogTableColumn));
+
+    // All write options overrides should be inferred in DynamicIcebergSink
+    String branch = stringTypedColumnValue(inputRecord, 
FlinkWriteOptions.BRANCH.key());
+
+    DistributionMode distributionMode = null;
+    String distributionModeStr =
+        stringTypedColumnValue(inputRecord, 
FlinkWriteOptions.DISTRIBUTION_MODE.key());

Review Comment:
   Planning to centralize all write options like Branch / distribution mode in 
DynamicIcebergSink itself, similar to how it's done in IcebergSink.
   As it already takes care of reading them from write properties or table 
properties.



##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/sink/dynamic/VariantAvroDynamicTableRecordGenerator.java:
##########
@@ -0,0 +1,223 @@
+/*
+ * 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 java.util.Map;
+import org.apache.avro.Schema.Parser;
+import org.apache.flink.api.common.functions.OpenContext;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+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.flink.util.Collector;
+import org.apache.iceberg.DistributionMode;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.avro.AvroSchemaUtil;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.flink.FlinkCreateTableOptions;
+import org.apache.iceberg.flink.FlinkSchemaUtil;
+import org.apache.iceberg.flink.FlinkWriteOptions;
+import org.apache.iceberg.flink.data.VariantRowDataWrapper;
+import 
org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.base.Splitter;
+
+public class VariantAvroDynamicTableRecordGenerator extends 
DynamicTableRecordGenerator {
+
+  private transient Map<String, Integer> fieldNameToPosition;
+  private static final Splitter COMMA = Splitter.on(',');
+  private transient int maxCacheSize;
+  private transient Map<TableIdentifier, SchemaAndPartitionSpecCacheItem> 
tableCache;
+  @VisibleForTesting static final String DATA_COLUMN = "data";
+  @VisibleForTesting static final String AVRO_SCHEMA_COLUMN = "avro_schema";
+  @VisibleForTesting static final String AVRO_SCHEMA_ID_COLUMN = 
"avro_schema_id";
+  @VisibleForTesting static final String PARTITION_COLUMNS = 
"partition_columns";
+
+  public VariantAvroDynamicTableRecordGenerator(
+      RowType rowType, Map<String, String> writeProperties) {
+    super(rowType, writeProperties);
+
+    String catalogDatabaseColumn = 
FlinkCreateTableOptions.CATALOG_DATABASE.key();
+    Preconditions.checkArgument(
+        rowType.getFieldIndex(catalogDatabaseColumn) != -1
+            || writeProperties().containsKey(catalogDatabaseColumn),
+        "Invalid %s:null.Either %s column should be passed in Row or set in 
table options",
+        catalogDatabaseColumn,
+        catalogDatabaseColumn);
+
+    String catalogTableColumn = FlinkCreateTableOptions.CATALOG_TABLE.key();
+    Preconditions.checkArgument(
+        rowType.getFieldIndex(catalogTableColumn) != -1
+            || writeProperties().containsKey(catalogTableColumn),
+        "Invalid %s:null.Either %s column should be passed in Row or set in 
table options",
+        catalogTableColumn,
+        catalogTableColumn);
+
+    validateRequiredFieldAndType(DATA_COLUMN, new VariantType(false));
+    validateRequiredFieldAndType(AVRO_SCHEMA_COLUMN, new VarCharType(false, 
Integer.MAX_VALUE));
+    validateRequiredFieldAndType(AVRO_SCHEMA_ID_COLUMN, new VarCharType(false, 
Integer.MAX_VALUE));
+  }
+
+  @Override
+  public void open(OpenContext openContext) throws Exception {
+    super.open(openContext);
+
+    this.fieldNameToPosition = fieldNameToPositionMapping();
+
+    String value = 
writeProperties().get(FlinkWriteOptions.CACHE_MAX_SIZE.key());
+    if (value != null) {
+      maxCacheSize = Integer.parseInt(value);
+    } else {
+      maxCacheSize = FlinkWriteOptions.CACHE_MAX_SIZE.defaultValue();
+    }
+
+    this.tableCache = new LRUCache<>(maxCacheSize);
+  }
+
+  @Override
+  public void generate(RowData inputRecord, Collector<DynamicRecord> out) 
throws Exception {
+    String catalogDatabaseColumn = 
FlinkCreateTableOptions.CATALOG_DATABASE.key();
+    String catalogDb =
+        stringTypedColumnValue(
+            inputRecord, catalogDatabaseColumn, 
writeProperties().get(catalogDatabaseColumn));
+
+    String catalogTableColumn = FlinkCreateTableOptions.CATALOG_TABLE.key();
+    String catalogTable =
+        stringTypedColumnValue(
+            inputRecord, catalogTableColumn, 
writeProperties().get(catalogTableColumn));
+
+    // All write options overrides should be inferred in DynamicIcebergSink
+    String branch = stringTypedColumnValue(inputRecord, 
FlinkWriteOptions.BRANCH.key());

Review Comment:
   Planning to centralize all write options like  Branch / distribution mode in 
DynamicIcebergSink itself,  similar to how it's done in IcebergSink. 
   As it already takes care of reading them from write properties or table 
properties.



-- 
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]

Reply via email to