You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2020/08/04 06:36:29 UTC

[GitHub] [iceberg] YesOrNo828 commented on a change in pull request #1185: Flink: Add the iceberg files committer to collect data files and commit to iceberg table.

YesOrNo828 commented on a change in pull request #1185:
URL: https://github.com/apache/iceberg/pull/1185#discussion_r464830310



##########
File path: flink/src/main/java/org/apache/iceberg/flink/PartitionKey.java
##########
@@ -0,0 +1,126 @@
+/*
+ * 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;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import org.apache.flink.types.Row;
+import org.apache.iceberg.PartitionField;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.transforms.Transform;
+import org.apache.iceberg.types.Types;
+
+public class PartitionKey implements StructLike {
+
+  private final Object[] partitionTuple;
+
+  private PartitionKey(Object[] partitionTuple) {
+    this.partitionTuple = partitionTuple;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (!(o instanceof PartitionKey)) {
+      return false;
+    }
+
+    PartitionKey that = (PartitionKey) o;
+    return Arrays.equals(partitionTuple, that.partitionTuple);
+  }
+
+  @Override
+  public int hashCode() {
+    return Arrays.hashCode(partitionTuple);
+  }
+
+  @Override
+  public int size() {
+    return partitionTuple.length;
+  }
+
+  @Override
+  public <T> T get(int pos, Class<T> javaClass) {
+    return javaClass.cast(partitionTuple[pos]);
+  }
+
+  public Object[] getPartitionTuple() {
+    return partitionTuple;
+  }
+
+  @Override
+  public <T> void set(int pos, T value) {
+    partitionTuple[pos] = value;
+  }
+
+  private static Map<Integer, Integer> buildFieldId2PosMap(Schema schema) {
+    Map<Integer, Integer> fieldId2Position = Maps.newHashMap();
+    List<Types.NestedField> nestedFields = schema.asStruct().fields();
+    for (int i = 0; i < nestedFields.size(); i++) {
+      fieldId2Position.put(nestedFields.get(i).fieldId(), i);
+    }
+    return fieldId2Position;
+  }
+
+  public static Builder builder(PartitionSpec spec) {
+    return new Builder(spec);
+  }
+
+  public static class Builder {
+    private final int size;
+
+    private final int[] pos;
+    private final Transform[] transforms;
+
+    private Builder(PartitionSpec spec) {
+      List<PartitionField> fields = spec.fields();
+      this.size = fields.size();
+      this.pos = new int[size];
+      this.transforms = new Transform[size];
+
+      Map<Integer, Integer> fieldId2Pos = buildFieldId2PosMap(spec.schema());
+
+      for (int i = 0; i < size; i += 1) {
+        PartitionField field = fields.get(i);
+        Integer position = fieldId2Pos.get(field.sourceId());
+        Preconditions.checkArgument(position != null,
+            "Field source id from PartitionSpec MUST exist in the original schema");
+        this.pos[i] = position;
+        this.transforms[i] = field.transform();
+      }
+    }
+
+    @SuppressWarnings("unchecked")
+    public PartitionKey build(Row row) {
+      Object[] partitionTuple = new Object[size];
+      for (int i = 0; i < partitionTuple.length; i += 1) {
+        partitionTuple[i] = transforms[i].apply(row.getField(pos[i]));

Review comment:
       `pos[i]` It seemed the fields of flink ddl must be the same sequence with the fields of iceberg schema . what if flink ddl misses some fields? I think partition keys' indices will be different from in iceberg schema




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org