diff --git a/api/src/main/java/org/apache/iceberg/PartitionKey.java b/api/src/main/java/org/apache/iceberg/PartitionKey.java
index fc56d1a45347..08e3ea319b32 100644
--- a/api/src/main/java/org/apache/iceberg/PartitionKey.java
+++ b/api/src/main/java/org/apache/iceberg/PartitionKey.java
@@ -18,13 +18,8 @@
*/
package org.apache.iceberg;
-import java.io.Serializable;
-import java.lang.reflect.Array;
-import java.util.Arrays;
import java.util.List;
-import java.util.function.Function;
-import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
-import org.apache.iceberg.util.SerializableFunction;
+import java.util.stream.Collectors;
/**
* A struct of partition values.
@@ -32,58 +27,23 @@
*
Instances of this class can produce partition values from a data row passed to {@link
* #partition(StructLike)}.
*/
-public class PartitionKey implements StructLike, Serializable {
+public class PartitionKey extends StructTransform {
private final PartitionSpec spec;
- private final int size;
- private final Object[] partitionTuple;
- private final SerializableFunction[] transforms;
- private final Accessor[] accessors;
+ private final Schema inputSchema;
@SuppressWarnings("unchecked")
public PartitionKey(PartitionSpec spec, Schema inputSchema) {
+ super(inputSchema, fieldTransform(spec));
this.spec = spec;
-
- List fields = spec.fields();
- this.size = fields.size();
- this.partitionTuple = new Object[size];
- this.transforms = new SerializableFunction[size];
- this.accessors = (Accessor[]) Array.newInstance(Accessor.class, size);
-
- Schema schema = spec.schema();
- for (int i = 0; i < size; i += 1) {
- PartitionField field = fields.get(i);
- Accessor accessor = inputSchema.accessorForField(field.sourceId());
- Preconditions.checkArgument(
- accessor != null,
- "Cannot build accessor for field: " + schema.findField(field.sourceId()));
- this.accessors[i] = accessor;
- this.transforms[i] = field.transform().bind(accessor.type());
- }
+ this.inputSchema = inputSchema;
}
private PartitionKey(PartitionKey toCopy) {
+ // only need deep copy inside StructTransform
+ super(toCopy);
this.spec = toCopy.spec;
- this.size = toCopy.size;
- this.partitionTuple = new Object[toCopy.partitionTuple.length];
- this.transforms = toCopy.transforms;
- this.accessors = toCopy.accessors;
-
- System.arraycopy(toCopy.partitionTuple, 0, this.partitionTuple, 0, partitionTuple.length);
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("[");
- for (int i = 0; i < partitionTuple.length; i += 1) {
- if (i > 0) {
- sb.append(", ");
- }
- sb.append(partitionTuple[i]);
- }
- sb.append("]");
- return sb.toString();
+ this.inputSchema = toCopy.inputSchema;
}
public PartitionKey copy() {
@@ -101,41 +61,14 @@ public String toPath() {
*/
@SuppressWarnings("unchecked")
public void partition(StructLike row) {
- for (int i = 0; i < partitionTuple.length; i += 1) {
- Function