rdblue commented on a change in pull request #1145: URL: https://github.com/apache/iceberg/pull/1145#discussion_r449709483
########## 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) { Review comment: In other cases, we avoid allocating a new key for the partition tuple every row, and we defensively copy partition keys. It might be a good idea to generalize the `PartitionKey` from Spark and use it here. ---------------------------------------------------------------- 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: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
