rdblue commented on a change in pull request #1175: URL: https://github.com/apache/iceberg/pull/1175#discussion_r454512523
########## File path: flink/src/test/java/org/apache/iceberg/flink/TestPartitionKey.java ########## @@ -0,0 +1,143 @@ +/* + * 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.flink; + +import org.apache.flink.types.Row; +import org.apache.iceberg.AssertHelpers; +import org.apache.iceberg.PartitionKey; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.util.DateTimeUtil; +import org.junit.Assert; +import org.junit.Test; + +public class TestPartitionKey { + + @Test + public void testSimplePartition() { + Schema schema = new Schema( + Types.NestedField.optional(1, "id", Types.IntegerType.get()), + Types.NestedField.optional(2, "data", Types.StringType.get()), + Types.NestedField.optional(3, "address", Types.StringType.get()) + ); + + PartitionSpec spec = PartitionSpec.builderFor(schema) + .identity("address") + .build(); + RowWrapper rowWrapper = new RowWrapper(schema.asStruct()); + + Row row1 = Row.of(101, "hello", "addr-1"); + PartitionKey partitionKey = new PartitionKey(spec, schema); + partitionKey.partition(rowWrapper.wrap(row1)); + Assert.assertEquals(partitionKey.size(), 1); + Assert.assertEquals(partitionKey.get(0, String.class), "addr-1"); + + Row row2 = Row.of(102, "world", "addr-2"); + partitionKey.partition(rowWrapper.wrap(row2)); + Assert.assertEquals(partitionKey.size(), 1); + Assert.assertEquals(partitionKey.get(0, String.class), "addr-2"); + } + + @Test + public void testPartitionWithNestedType() { + Schema schema = new Schema( + Types.NestedField.optional(1, "id", Types.IntegerType.get()), + Types.NestedField.optional(2, "structType", Types.StructType.of( + Types.NestedField.optional(3, "innerStringType", Types.StringType.get()), + Types.NestedField.optional(4, "innerIntegerType", Types.IntegerType.get()) + )), + Types.NestedField.optional(5, "listType", Types.ListType.ofOptional(6, Types.LongType.get())), + Types.NestedField.optional(7, "mapType", + Types.MapType.ofRequired(8, 9, Types.IntegerType.get(), Types.StringType.get())), + Types.NestedField.required(10, "ts", Types.TimestampType.withZone()) + ); + RowWrapper rowWrapper = new RowWrapper(schema.asStruct()); + + Row row = Row.of( + 1001, + Row.of("addr-1", 200), + new Long[] {101L, 102L}, + ImmutableMap.of(1001, "1001-value"), + DateTimeUtil.microsFromTimestamp(DateTimeUtil.timestampFromMicros(0L)) + ); + + PartitionSpec spec = PartitionSpec.builderFor(schema) + .identity("structType.innerStringType") + .build(); + PartitionKey partitionKey = new PartitionKey(spec, schema); + partitionKey.partition(rowWrapper.wrap(row)); + Assert.assertEquals(partitionKey.size(), 1); + Assert.assertEquals(partitionKey.get(0, String.class), "addr-1"); + Assert.assertEquals(partitionKey.toPath(), "structType.innerStringType=addr-1"); + + PartitionSpec spec2 = PartitionSpec.builderFor(schema) + .identity("structType.innerIntegerType") + .build(); + PartitionKey partitionKey2 = new PartitionKey(spec2, schema); + partitionKey2.partition(rowWrapper.wrap(row)); + Assert.assertEquals(1, partitionKey2.size()); + Assert.assertEquals(200, (int) partitionKey2.get(0, Integer.class)); + Assert.assertEquals(partitionKey2.toPath(), "structType.innerIntegerType=200"); Review comment: Can you split each of these blocks into a separate test case? There are lots of different cases mixed together in this method. Mixing cases together makes it harder to see what is broken when tests fail because you don't get a picture of what is common across failed cases since many of them don't run. ---------------------------------------------------------------- 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]
