stevenzwu commented on code in PR #10678: URL: https://github.com/apache/iceberg/pull/10678#discussion_r1702860252
########## core/src/test/java/org/apache/iceberg/TestPartitionSpecBuilderCaseSensitivity.java: ########## @@ -0,0 +1,697 @@ +/* + * 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.types.Types.NestedField.required; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.types.Types.NestedField; +import org.apache.iceberg.types.Types.StructType; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public class TestPartitionSpecBuilderCaseSensitivity { + + private static final int V2_FORMAT_VERSION = 2; + private static final Schema SCHEMA_CASE_INSENSITIVE = + new Schema( + required(1, "id", Types.IntegerType.get()), + required(2, "data", Types.StringType.get()), + required(3, "category", Types.StringType.get()), + required(4, "order_date", Types.DateType.get()), + required(5, "order_time", Types.TimestampType.withoutZone()), + required(6, "ship_date", Types.DateType.get()), + required(7, "ship_time", Types.TimestampType.withoutZone())); + + private static final Schema SCHEMA_CASE_SENSITIVE = + new Schema( + required(1, "id", Types.IntegerType.get()), + required(2, "data", Types.StringType.get()), + required(3, "DATA", Types.StringType.get()), + required(4, "order_date", Types.DateType.get()), + required(5, "ORDER_DATE", Types.DateType.get()), + required(6, "order_time", Types.TimestampType.withoutZone()), + required(7, "ORDER_TIME", Types.TimestampType.withoutZone())); + + @TempDir private Path temp; + private File tableDir = null; + + @BeforeEach + public void setupTableDir() throws IOException { + this.tableDir = Files.createTempDirectory(temp, "junit").toFile(); + } + + @AfterEach + public void cleanupTables() { + TestTables.clearTables(); + } + + @Test + public void testPartitionTypeWithColumnNamesThatDifferOnlyInLetterCase() { + Schema schema = + new Schema( + required(1, "id", Types.IntegerType.get()), + required(2, "data", Types.StringType.get()), + required(3, "DATA", Types.StringType.get()), + required(4, "order_date", Types.DateType.get())); + PartitionSpec spec = PartitionSpec.builderFor(schema).identity("data").identity("DATA").build(); + TestTables.TestTable table = + TestTables.create(tableDir, "test", schema, spec, V2_FORMAT_VERSION); + + StructType expectedType = + StructType.of( + NestedField.optional(1000, "data", Types.StringType.get()), + NestedField.optional(1001, "DATA", Types.StringType.get())); + StructType actualType = Partitioning.partitionType(table); + assertThat(actualType).isEqualTo(expectedType); + } + + @Test + public void testPartitionTypeWithIdentityTargetName() { + PartitionSpec spec = + PartitionSpec.builderFor(SCHEMA_CASE_INSENSITIVE).identity("data", "p1").build(); + TestTables.TestTable table = + TestTables.create(tableDir, "test", SCHEMA_CASE_INSENSITIVE, spec, V2_FORMAT_VERSION); + + StructType expectedType = + StructType.of(NestedField.optional(1000, "p1", Types.StringType.get())); + StructType actualType = Partitioning.partitionType(table); + assertThat(actualType).isEqualTo(expectedType); + } + + @Test + public void testBucketSourceNameDoesNotAllowExactDuplicateWhenCaseSensitive() { + assertThatIllegalArgumentException() + .isThrownBy( + () -> + PartitionSpec.builderFor(SCHEMA_CASE_SENSITIVE) + .bucket("data", 10, "p1") Review Comment: nit: it is a bit difficult to read `p1` vs `P1`. use some other more distinguiable names, like `partition1` and `PARTITION1`. this applies to other tests too ########## api/src/main/java/org/apache/iceberg/types/TypeUtil.java: ########## @@ -182,11 +181,10 @@ public static Map<Integer, String> indexQuotedNameById( } public static Map<String, Integer> indexByLowerCaseName(Types.StructType struct) { - Map<String, Integer> indexByLowerCaseName = Maps.newHashMap(); + ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder(); indexByName(struct) - .forEach( - (name, integer) -> indexByLowerCaseName.put(name.toLowerCase(Locale.ROOT), integer)); - return indexByLowerCaseName; + .forEach((name, integer) -> builder.put(name.toLowerCase(Locale.ROOT), integer)); + return builder.buildOrThrow(); Review Comment: Instead of depending on ImmutableMap builder for the dup check, I am wondering if we should handle the collision check here in order to throw an exception with more informative error msg than `IllegalArgumentException: Multiple entries with same key: a=2 and a=1`. E.g., the error msg can be sth like `Unable to build field name to id mapping because two fields have the same lower case name...`. We can also get the original field name since we know the two field ids and have the original schema/struct. also let's add Javadoc to this method to document the behavior under collision. ########## core/src/test/java/org/apache/iceberg/TestPartitionSpecInfo.java: ########## @@ -95,6 +96,30 @@ public void testSpecInfoPartitionedTable() { .doesNotContainKey(Integer.MAX_VALUE); } + @TestTemplate + public void testSpecInfoPartitionedTableCaseInsensitive() { + PartitionSpec spec = + PartitionSpec.builderFor(schema).caseSensitive(false).identity("DATA").build(); + TestTables.TestTable table = TestTables.create(tableDir, "test", schema, spec, formatVersion); + + assertThat(table.spec()).isEqualTo(spec); + assertThat(table.spec().lastAssignedFieldId()).isEqualTo(spec.lastAssignedFieldId()); + assertThat(table.specs()) + .containsExactly(entry(spec.specId(), spec)) + .doesNotContainKey(Integer.MAX_VALUE); Review Comment: for my education, what is this assertion for? -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org