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/12/12 23:22:10 UTC

[GitHub] [iceberg] johnclara commented on a change in pull request #1919: Add UpdatePartitionSpec implementation

johnclara commented on a change in pull request #1919:
URL: https://github.com/apache/iceberg/pull/1919#discussion_r541800623



##########
File path: core/src/main/java/org/apache/iceberg/BaseUpdatePartitionSpec.java
##########
@@ -0,0 +1,307 @@
+/*
+ * 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;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.expressions.BoundReference;
+import org.apache.iceberg.expressions.BoundTerm;
+import org.apache.iceberg.expressions.BoundTransform;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.expressions.Term;
+import org.apache.iceberg.expressions.UnboundTerm;
+import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.apache.iceberg.transforms.PartitionSpecVisitor;
+import org.apache.iceberg.transforms.Transform;
+import org.apache.iceberg.transforms.Transforms;
+import org.apache.iceberg.transforms.UnknownTransform;
+import org.apache.iceberg.util.Pair;
+
+class BaseUpdatePartitionSpec implements UpdatePartitionSpec {
+  private final TableOperations ops;
+  private final boolean caseSensitive;
+  private final TableMetadata base;
+  private final int formatVersion;
+  private final PartitionSpec spec;
+  private final Schema schema;
+  private final Map<String, PartitionField> nameToField;
+  private final Map<Pair<Integer, String>, PartitionField> transformToField;
+
+  private final List<PartitionField> adds = Lists.newArrayList();
+  private final Map<Pair<Integer, String>, PartitionField> transformToAddedField = Maps.newHashMap();
+  private final Set<Object> deletes = Sets.newHashSet();
+  private final Map<String, String> renames = Maps.newHashMap();
+
+  private int lastAssignedPartitionId;
+
+  BaseUpdatePartitionSpec(TableOperations ops, boolean caseSensitive) {
+    this.ops = ops;
+    this.caseSensitive = caseSensitive;
+    this.base = ops.current();
+    this.formatVersion = base.formatVersion();
+    this.spec = base.spec();
+    this.schema = spec.schema();
+    this.nameToField = indexSpecByName(spec);
+    this.transformToField = indexSpecByTransform(spec);
+    this.lastAssignedPartitionId = spec.fields().stream().mapToInt(PartitionField::fieldId).max().orElse(999);
+
+    spec.fields().stream()
+        .filter(field -> field.transform() instanceof UnknownTransform)
+        .findAny()
+        .ifPresent(field -> {
+          throw new IllegalArgumentException("Cannot update partition spec with unknown transform: " + field);
+        });
+  }
+
+  /**
+   * For testing only.
+   */
+  @VisibleForTesting
+  BaseUpdatePartitionSpec(int formatVersion, PartitionSpec spec) {
+    this(formatVersion, spec, spec.fields().stream().mapToInt(PartitionField::fieldId).max().orElse(999));
+  }
+
+  /**
+   * For testing only.
+   */
+  @VisibleForTesting
+  BaseUpdatePartitionSpec(int formatVersion, PartitionSpec spec, int lastAssignedPartitionId) {
+    this.ops = null;
+    this.base = null;
+    this.formatVersion = formatVersion;
+    this.caseSensitive = true;
+    this.spec = spec;
+    this.schema = spec.schema();
+    this.nameToField = indexSpecByName(spec);
+    this.transformToField = indexSpecByTransform(spec);
+    this.lastAssignedPartitionId = lastAssignedPartitionId;
+  }
+
+  private int assignFieldId() {
+    this.lastAssignedPartitionId += 1;
+    return lastAssignedPartitionId;
+  }
+
+  @Override
+  public BaseUpdatePartitionSpec addField(String sourceName) {
+    return addField(Expressions.ref(sourceName));
+  }
+
+  @Override
+  public BaseUpdatePartitionSpec addField(Term term) {
+    return addField(null, term);
+  }
+
+  @Override
+  public BaseUpdatePartitionSpec addField(String name, Term term) {

Review comment:
       When javadocs are added later, maybe add a warning about not being threadsafe?




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