stevenzwu commented on a change in pull request #2898:
URL: https://github.com/apache/iceberg/pull/2898#discussion_r719050707



##########
File path: 
flink/src/main/java/org/apache/iceberg/flink/sink/EqualityFieldKeySelector.java
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.util.List;
+import org.apache.flink.api.java.functions.KeySelector;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.types.TypeUtil;
+import org.apache.iceberg.util.StructLikeWrapper;
+import org.apache.iceberg.util.StructProjection;
+
+/**
+ * Create a {@link KeySelector} to shuffle by equality fields, to ensure same 
equality fields record will be emitted to
+ * same writer. That can prevent create duplicate record when insert and 
delete one row which have same equality field
+ * values on different writer in one transaction, and guarantee pos-delete 
will take effect.
+ */
+class EqualityFieldKeySelector extends BaseKeySelector<RowData, 
StructLikeWrapper> {
+
+  private final Schema schema;
+  private final Schema deleteSchema;
+
+  private transient StructProjection projection;
+  private transient StructLikeWrapper wrapper;
+
+  EqualityFieldKeySelector(List<Integer> equalityFieldIds, Schema schema, 
RowType flinkSchema) {
+    super(schema, flinkSchema);
+    this.schema = schema;
+    this.deleteSchema = TypeUtil.select(schema, 
Sets.newHashSet(equalityFieldIds));
+  }
+
+  @Override
+  public StructLikeWrapper getKey(RowData row) {
+    // lazily construct because projection and wrapper are not serializable.
+    projection = projection == null ? StructProjection.create(schema, 
deleteSchema) : projection;
+    wrapper = wrapper == null ? 
StructLikeWrapper.forType(deleteSchema.asStruct()) : wrapper;
+    return wrapper.set(projection.wrap(lazyRowDataWrapper().wrap(row)));

Review comment:
       why does the return type need to be `StructLikeWrapper`? Can we directly 
return `projection.wrap(lazyRowDataWrapper().wrap(row))`?




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