rdblue commented on a change in pull request #922: URL: https://github.com/apache/iceberg/pull/922#discussion_r435444523
########## File path: core/src/main/java/org/apache/iceberg/PartitionSpecUpdate.java ########## @@ -0,0 +1,214 @@ +/* + * 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 com.google.common.base.Preconditions; +import com.google.common.collect.Maps; +import java.util.Map; +import org.apache.iceberg.transforms.Transform; + +/** + * PartitionSpec evolution API implementation. + */ +class PartitionSpecUpdate implements UpdatePartitionSpec { + + private final TableMetadata base; + private final TableOperations ops; + private PartitionSpec.Builder newSpecBuilder; + + PartitionSpecUpdate(TableOperations ops) { + this.ops = ops; + this.base = ops.current(); + this.newSpecBuilder = PartitionSpec.builderFor(base.schema()); + for (PartitionField field : base.spec().fields()) { + this.newSpecBuilder.add(field.sourceId(), field.fieldId(), field.name(), field.transform().toString()); + } + } + + @Override + public PartitionSpec apply() { + Preconditions.checkNotNull(newSpecBuilder, "new partition spec is not set"); + return newSpecBuilder.build(); + } + + @Override + public UpdatePartitionSpec clear() { + this.newSpecBuilder = PartitionSpec.builderFor(base.schema()); + return this; + } + + @Override + public UpdatePartitionSpec addIdentityField(String sourceName, String targetName) { + newSpecBuilder.identity(sourceName, targetName); + return this; + } + + @Override + public UpdatePartitionSpec addIdentityField(String sourceName) { + newSpecBuilder.identity(sourceName); + return this; + } + + @Override + public UpdatePartitionSpec addYearField(String sourceName, String targetName) { + newSpecBuilder.year(sourceName, targetName); + return this; + } + + @Override + public UpdatePartitionSpec addYearField(String sourceName) { + newSpecBuilder.year(sourceName); + return this; + } + + @Override + public UpdatePartitionSpec addMonthField(String sourceName, String targetName) { + newSpecBuilder.month(sourceName, targetName); + return this; + } + + @Override + public UpdatePartitionSpec addMonthField(String sourceName) { + newSpecBuilder.month(sourceName); + return this; + } + + @Override + public UpdatePartitionSpec addDayField(String sourceName, String targetName) { + newSpecBuilder.day(sourceName, targetName); + return this; + } + + @Override + public UpdatePartitionSpec addDayField(String sourceName) { + newSpecBuilder.day(sourceName); + return this; + } + + @Override + public UpdatePartitionSpec addHourField(String sourceName, String targetName) { + newSpecBuilder.hour(sourceName, targetName); + return this; + } + + @Override + public UpdatePartitionSpec addHourField(String sourceName) { + newSpecBuilder.hour(sourceName); + return this; + } + + @Override + public UpdatePartitionSpec addBucketField(String sourceName, int numBuckets, String targetName) { + newSpecBuilder.bucket(sourceName, numBuckets, targetName); + return this; + } + + @Override + public UpdatePartitionSpec addBucketField(String sourceName, int numBuckets) { + newSpecBuilder.bucket(sourceName, numBuckets); + return this; + } + + @Override + public UpdatePartitionSpec addTruncateField(String sourceName, int width, String targetName) { + newSpecBuilder.truncate(sourceName, width, targetName); + return this; + } + + @Override + public UpdatePartitionSpec addTruncateField(String sourceName, int width) { + newSpecBuilder.truncate(sourceName, width); + return this; + } + + @Override + public UpdatePartitionSpec addField(int sourceId, String name, String transform) { + newSpecBuilder.add(sourceId, name, transform); + return this; + } + + @Override + public UpdatePartitionSpec addField(int sourceId, String name, Transform<?, ?> transform) { + return addField(sourceId, name, transform.toString()); + } + + @Override + public UpdatePartitionSpec renameField(String name, String newName) { + newSpecBuilder.rename(name, newName); + return this; + } + + @Override + public UpdatePartitionSpec removeField(String name) { + newSpecBuilder.remove(name, base.formatVersion() == 1); + return this; + } + + @Override + public UpdatePartitionSpec replaceField(String name, String transform) { + int sourceId = newSpecBuilder.remove(name, base.formatVersion() == 1); + return addField(sourceId, name, transform); + } + + @Override + public UpdatePartitionSpec replaceField(String name, Transform<?, ?> transform) { Review comment: I'm not sure it makes much sense to include this. When would it be reasonable to use it? The most common replacement case I can think of is to change the granularity of a transform. For example, `days` to `hours` or `truncate[10]` to `truncate[5]`. In the first case, it wouldn't make sense to use this because the name is something like `ts_day`, which would be misleading if used for hours. For the second case, although the partition values are comparable, it would still be confusing to mix together bins with different widths. I can't think of a case where we would want to use the same partition field ID with a different transform, other than deleting a transform in v1 by replacing it with `alwaysNull`. So I don't think we need the `replaceField` method, at least not right now. Let's get this in without the feature and we can add it later if there is a use case for it. ---------------------------------------------------------------- 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]
