adrians commented on code in PR #16515: URL: https://github.com/apache/iceberg/pull/16515#discussion_r3332317870
########## core/src/test/java/org/apache/iceberg/TestPartitionSpecSatisfies.java: ########## @@ -0,0 +1,414 @@ +/* + * 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 static org.apache.iceberg.expressions.Expressions.bucket; +import static org.apache.iceberg.expressions.Expressions.day; +import static org.apache.iceberg.expressions.Expressions.hour; +import static org.apache.iceberg.expressions.Expressions.month; +import static org.apache.iceberg.expressions.Expressions.truncate; +import static org.apache.iceberg.expressions.Expressions.year; +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.iceberg.types.Types; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; + +@ExtendWith(ParameterizedTestExtension.class) +public class TestPartitionSpecSatisfies extends TestBase { + private static final Schema SCHEMA = + new Schema( + Types.NestedField.required(1, "id", Types.LongType.get()), + Types.NestedField.required(2, "ts", Types.TimestampType.withZone()), + Types.NestedField.required(3, "category", Types.StringType.get()), + Types.NestedField.optional(4, "data", Types.StringType.get())); + + private static final PartitionSpec UNPARTITIONED = PartitionSpec.builderFor(SCHEMA).build(); + private static final PartitionSpec PARTITIONED = + PartitionSpec.builderFor(SCHEMA) + .identity("category") + .day("ts") + .bucket("id", 16, "shard") + .build(); + + @TestTemplate + public void testUnpartitioned() { + assertThat(UNPARTITIONED.satisfies(UNPARTITIONED)).isTrue(); + } + + @TestTemplate + public void testPartitionedSatisfiesUnpartitioned() { + assertThat(PARTITIONED.satisfies(UNPARTITIONED)).isTrue(); + } + + @TestTemplate + void testUnpartitionedDoesNotSatisfyPartitioned() { + assertThat(UNPARTITIONED.satisfies(PARTITIONED)).isFalse(); + } + + @TestTemplate + void testSameSpecSatisfiesItself() { + assertThat(PARTITIONED.satisfies(PARTITIONED)).isTrue(); + } + + @TestTemplate + void testDifferentSourceColumnDoesNotSatisfy() { + PartitionSpec byCategory = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED) + .addField(org.apache.iceberg.expressions.Expressions.ref("category")) + .apply(); + PartitionSpec byDay = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(day("ts")).apply(); + assertThat(byDay.satisfies(byCategory)).isFalse(); + assertThat(byCategory.satisfies(byDay)).isFalse(); + } + + @TestTemplate + void testHourSatisfiesMonth() { + PartitionSpec byMonth = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(month("ts")).apply(); + PartitionSpec byHour = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(hour("ts")).apply(); + assertThat(byHour.satisfies(byMonth)).isTrue(); + } + + @TestTemplate + void testHourSatisfiedDayAndMonth() { + PartitionSpec byMonth = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(month("ts")).apply(); + PartitionSpec byMonthAndDay = + new BaseUpdatePartitionSpec(formatVersion, byMonth).addField(day("ts")).apply(); + PartitionSpec byHour = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(hour("ts")).apply(); + assertThat(byHour.satisfies(byMonthAndDay)).isTrue(); + } + + @TestTemplate + void testYearDoesNotSatisfyMonth() { + PartitionSpec byMonth = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(month("ts")).apply(); + PartitionSpec byYear = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(year("ts")).apply(); + assertThat(byYear.satisfies(byMonth)).isFalse(); + } + + @TestTemplate + void testHourSatisfyYear() { + PartitionSpec byYear = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(year("ts")).apply(); + PartitionSpec byHour = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(hour("ts")).apply(); + // hour is finer than year, so hour DOES satisfy year + assertThat(byHour.satisfies(byYear)).isTrue(); + } + + @TestTemplate + void testMultiFieldBothTransformsSatisfy() { + PartitionSpec byCategoryAndMonth = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(month("ts")).apply(); + byCategoryAndMonth = + new BaseUpdatePartitionSpec(formatVersion, byCategoryAndMonth) + .addField("category_field", org.apache.iceberg.expressions.Expressions.ref("category")) + .apply(); + + PartitionSpec byCategoryAndDay = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(day("ts")).apply(); + byCategoryAndDay = + new BaseUpdatePartitionSpec(formatVersion, byCategoryAndDay) + .addField("category_field", org.apache.iceberg.expressions.Expressions.ref("category")) + .apply(); + + // day is finer than month, and identity satisfies identity, so this should satisfy + assertThat(byCategoryAndDay.satisfies(byCategoryAndMonth)).isTrue(); + } + + @TestTemplate + void testMultiFieldOneTransformDoesNotSatisfy() { + PartitionSpec byCategoryAndDay = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(day("ts")).apply(); + byCategoryAndDay = + new BaseUpdatePartitionSpec(formatVersion, byCategoryAndDay) + .addField("category_field", org.apache.iceberg.expressions.Expressions.ref("category")) + .apply(); + + PartitionSpec byCategoryAndHour = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED).addField(hour("ts")).apply(); + byCategoryAndHour = + new BaseUpdatePartitionSpec(formatVersion, byCategoryAndHour) + .addField("category_field", org.apache.iceberg.expressions.Expressions.ref("category")) + .apply(); + + // day is coarser than hour, so day does NOT satisfy hour + assertThat(byCategoryAndDay.satisfies(byCategoryAndHour)).isFalse(); + } + + @TestTemplate + void testTruncateWiderSatisfiesNarrower() { + PartitionSpec byTruncate5 = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED) + .addField(truncate("data", 5)) + .apply(); + PartitionSpec byTruncate10 = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED) + .addField(truncate("data", 10)) + .apply(); + // truncate(10) preserves more information than truncate(5), so truncate(10) satisfies + // truncate(5) + assertThat(byTruncate10.satisfies(byTruncate5)).isTrue(); + } + + @TestTemplate + void testTruncateNarrowerDoesNotSatisfyWider() { + PartitionSpec byTruncate5 = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED) + .addField(truncate("data", 5)) + .apply(); + PartitionSpec byTruncate10 = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED) + .addField(truncate("data", 10)) + .apply(); + assertThat(byTruncate5.satisfies(byTruncate10)).isFalse(); + } + + @TestTemplate + void testBucketDifferentWidthDoesNotSatisfy() { + PartitionSpec byBucket16 = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED) + .addField(bucket("id", 16)) + .apply(); + PartitionSpec byBucket32 = + new BaseUpdatePartitionSpec(formatVersion, UNPARTITIONED) + .addField(bucket("id", 32)) + .apply(); + // different bucket widths produce different partitions — neither satisfies the other + assertThat(byBucket32.satisfies(byBucket16)).isFalse(); Review Comment: Hi! I'd argue that, in order to cover more cases, `byBucket32.satisfies(byBucket16))` should be evaluated to `True`. Due to the way the buckets are built (`def bucket_N(x) = (murmur3_x86_32_hash(x) & Integer.MAX_VALUE) % N`, according to https://iceberg.apache.org/spec/#bucket-transform-details ), having `bucket("id", 2)` (so the `N=2`) means that the table is partitioned in "odd hashes" and "even hashes". If we move the partitioning to `bucket("id", 8)` it just means that the "odd hashes" bucket must be split into 4 parts, and each part will have an odd identifier (and similar for "even hashes"). Another case that's not handled is `byCategory.satisfies(byCategoryBucket16)`, and that should be evaluated to `True`, too. The root cause for those missing cases this is what `satisfiesOrderOf` tries to achieve: it's a way to deduplicate redundant ordering-criteria in `SortOrderUtil.java`, situations where `ORDER BY DAY(ts), ts` can be rewritten as `ORDER BY ts`. In the repartitioning context, it provides guarantees that "new finer partitions can be precisely mapped on the coarse partitioning" and "whenever a coarse partition is split, the finer partitions are consecutive in the table". The second guarantee makes it that `byBucket32.satisfies(byBucket16))` and `byCategory.satisfies(byCategoryBucket16)` return `False`, even if only the first property is necessary for our purpose. As a fix, I think a new predicate similar to `Transform.satisfiesOrderOf` should be defined in the `Transform` interface, that in the parent-class just returns the result of `self.satisfiesOrderOf(other)`, but in the `Identity` and `Bucket` classes it does additional checks. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
