openinx commented on a change in pull request #4132:
URL: https://github.com/apache/iceberg/pull/4132#discussion_r816430687



##########
File path: 
flink/v1.14/flink/src/main/java/org/apache/iceberg/flink/sink/FlinkTaskWriter.java
##########
@@ -0,0 +1,212 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Function;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.iceberg.ContentFile;
+import org.apache.iceberg.PartitionKey;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.deletes.PositionDelete;
+import org.apache.iceberg.flink.RowDataWrapper;
+import org.apache.iceberg.io.DataWriteResult;
+import org.apache.iceberg.io.DeleteWriteResult;
+import org.apache.iceberg.io.FileIO;
+import org.apache.iceberg.io.PartitioningWriter;
+import org.apache.iceberg.io.PartitioningWriterFactory;
+import org.apache.iceberg.io.PathOffset;
+import org.apache.iceberg.io.StructCopy;
+import org.apache.iceberg.io.TaskWriter;
+import org.apache.iceberg.io.WriteResult;
+import org.apache.iceberg.relocated.com.google.common.collect.ObjectArrays;
+import org.apache.iceberg.types.TypeUtil;
+import org.apache.iceberg.util.StructLikeMap;
+import org.apache.iceberg.util.StructProjection;
+import org.apache.iceberg.util.Tasks;
+
+public class FlinkTaskWriter implements TaskWriter<RowData> {
+  public static Function<RowData, StructLike> partitionerFor(
+      PartitionSpec spec,
+      Schema schema,
+      RowType flinkSchema) {
+    return new Function<RowData, StructLike>() {
+      private final RowDataWrapper wrapper = new RowDataWrapper(flinkSchema, 
schema.asStruct());
+      private final PartitionKey partitionKey = new PartitionKey(spec, schema);
+
+      @Override
+      public StructLike apply(RowData row) {
+        partitionKey.partition(wrapper.wrap(row));
+        return partitionKey;
+      }
+    };
+  }
+
+  public static Function<RowData, StructLike> equalityKeyerFor(
+      Set<Integer> equalityFieldIds,
+      Schema schema,
+      RowType flinkSchema) {
+    return new Function<RowData, StructLike>() {
+      private final RowDataWrapper wrapper = new RowDataWrapper(flinkSchema, 
schema.asStruct());
+      private final StructProjection structProjection = 
StructProjection.create(schema, equalityFieldIds);
+
+      @Override
+      public StructLike apply(RowData row) {
+        structProjection.wrap(wrapper.wrap(row));
+        return structProjection;
+      }
+    };
+  }
+
+  private final PartitioningWriter<RowData, DataWriteResult> insertWriter;
+  private final PartitioningWriter<RowData, DeleteWriteResult> 
equalityDeleteWriter;
+  private final PartitioningWriter<PositionDelete<RowData>, DeleteWriteResult> 
positionDeleteWriter;
+
+  private final PartitionSpec spec;
+  private final FileIO io;
+  private final boolean upsert;
+  private final Function<RowData, StructLike> partitioner;
+  private final Function<RowData, StructLike> equalityKeyer;
+
+  private StructLike partition;
+  private StructLike equalityKey;
+  private final PositionDelete<RowData> positionDelete;
+  private final Map<StructLike, PathOffset> insertedRowMap; // map row Key to 
PathOffset
+
+  protected FlinkTaskWriter(
+      PartitioningWriterFactory<RowData> partitioningWriterFactory,
+      Function<RowData, StructLike> partitioner,
+      Schema schema, PartitionSpec spec, Set<Integer> equalityFieldIds,
+      FileIO io, RowType flinkSchema, boolean upsert) {
+    this.insertWriter = partitioningWriterFactory.newDataWriter();
+    this.equalityDeleteWriter = 
partitioningWriterFactory.newEqualityDeleteWriter();
+    this.positionDeleteWriter = 
partitioningWriterFactory.newPositionDeleteWriter();
+
+    this.spec = spec;
+    this.io = io;
+    this.upsert = upsert;
+
+    this.insertedRowMap = 
StructLikeMap.create(TypeUtil.select(schema.asStruct(), equalityFieldIds));
+    this.positionDelete = PositionDelete.create();
+    this.partitioner = partitioner;
+    this.equalityKeyer = equalityKeyerFor(equalityFieldIds, schema, 
flinkSchema);
+  }
+
+  @Override
+  @SuppressWarnings("DuplicateBranchesInSwitch")
+  public void write(RowData row) throws IOException {
+    partition = partitioner.apply(row);
+    equalityKey = equalityKeyer.apply(row);
+
+    switch (row.getRowKind()) {
+      case INSERT:
+        if (upsert) {
+          delete(row);
+        }
+        insert(row);
+        break;

Review comment:
       Nit: Please leave a empty new line between two cases code block.




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