ahmedabu98 commented on code in PR #38269: URL: https://github.com/apache/beam/pull/38269#discussion_r3148295223
########## sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/SortOrderUtils.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.beam.sdk.io.iceberg; + +import java.util.List; +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.iceberg.NullOrder; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.expressions.Term; +import org.checkerframework.checker.nullness.qual.Nullable; + +class SortOrderUtils { Review Comment: Can you add a class-level java doc? ########## sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/SortOrderUtils.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.beam.sdk.io.iceberg; + +import java.util.List; +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.iceberg.NullOrder; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.expressions.Term; +import org.checkerframework.checker.nullness.qual.Nullable; + +class SortOrderUtils { + private static final Pattern MODIFIERS = + Pattern.compile( + "^(?<dir>asc|desc)?\\s*(nulls\\s+(?<nulls>first|last))?\\s*$", Pattern.CASE_INSENSITIVE); + + static SortOrder toSortOrder( + @Nullable List<String> fields, org.apache.beam.sdk.schemas.Schema beamSchema) { + return toSortOrder(fields, IcebergUtils.beamSchemaToIcebergSchema(beamSchema)); + } + + static SortOrder toSortOrder(@Nullable List<String> fields, Schema schema) { + if (fields == null || fields.isEmpty()) { + return SortOrder.unsorted(); + } + SortOrder.Builder builder = SortOrder.builderFor(schema); + for (String field : fields) { + ParsedSortField parsed = parse(field); + if (parsed.ascending) { + builder.asc(parsed.term, parsed.nullOrder); + } else { + builder.desc(parsed.term, parsed.nullOrder); + } + } + return builder.build(); + } + + private static ParsedSortField parse(String field) { + int splitAt = findTopLevelWhitespace(field); Review Comment: helper method will return immediately if there's whitespace in the beginning ```suggestion field = field.trim(); int splitAt = findTopLevelWhitespace(field); ``` ########## sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/IcebergTableCreateConfig.java: ########## @@ -41,6 +42,15 @@ public PartitionSpec getPartitionSpec() { @Pure public abstract @Nullable List<String> getPartitionFields(); + /** Sort order to apply when the table is dynamically created. */ + @Pure + public SortOrder getSortOrder() { Review Comment: Also annotate with `@SchemaIgnore` to ensure AutoValue ignores it when inferring the Config's schema ########## sdks/java/io/iceberg/src/test/java/org/apache/beam/sdk/io/iceberg/SortOrderUtilsTest.java: ########## @@ -0,0 +1,111 @@ +/* + * 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.beam.sdk.io.iceberg; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Collections; +import org.apache.iceberg.NullOrder; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortDirection; +import org.apache.iceberg.SortField; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.types.Types; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class SortOrderUtilsTest { + private static final Schema SCHEMA = + new Schema( + Types.NestedField.required(1, "id", Types.LongType.get()), + Types.NestedField.optional(2, "name", Types.StringType.get()), + Types.NestedField.optional(3, "ts", Types.TimestampType.withoutZone())); + + @Test + public void testNullOrEmptyFieldsYieldsUnsorted() { + assertTrue(SortOrderUtils.toSortOrder(null, SCHEMA).isUnsorted()); + assertTrue(SortOrderUtils.toSortOrder(Collections.emptyList(), SCHEMA).isUnsorted()); + } + + @Test + public void testIdentityDefaultsToAscNullsFirst() { + SortOrder order = SortOrderUtils.toSortOrder(Collections.singletonList("id"), SCHEMA); + assertEquals(1, order.fields().size()); + SortField field = order.fields().get(0); + assertEquals(SortDirection.ASC, field.direction()); + assertEquals(NullOrder.NULLS_FIRST, field.nullOrder()); + } + + @Test + public void testDescDefaultsToNullsLast() { + SortOrder order = SortOrderUtils.toSortOrder(Collections.singletonList("id desc"), SCHEMA); + SortField field = order.fields().get(0); + assertEquals(SortDirection.DESC, field.direction()); + assertEquals(NullOrder.NULLS_LAST, field.nullOrder()); + } + + @Test + public void testExplicitNullOrder() { + SortOrder order = + SortOrderUtils.toSortOrder(Collections.singletonList("name asc nulls last"), SCHEMA); + SortField field = order.fields().get(0); + assertEquals(SortDirection.ASC, field.direction()); + assertEquals(NullOrder.NULLS_LAST, field.nullOrder()); + } + + @Test + public void testTransformTerms() { + SortOrder order = + SortOrderUtils.toSortOrder( + Arrays.asList("bucket(id, 4) desc", "day(ts)", "truncate(name, 3) asc nulls first"), + SCHEMA); Review Comment: Also assert the transforms got parsed correctly (check `order.fields().get(N).transform().toString()`) ########## sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/AddFiles.java: ########## @@ -522,12 +531,18 @@ private Table getOrCreateTable(String filePath, FileFormat format) throws IOExce try { org.apache.iceberg.Schema schema = getSchema(filePath, format); PartitionSpec spec = PartitionUtils.toPartitionSpec(partitionFields, schema); + SortOrder sortOrder = SortOrderUtils.toSortOrder(sortFields, schema); - return tableProps == null - ? catalogConfig.catalog().createTable(TableIdentifier.parse(identifier), schema, spec) - : catalogConfig + Catalog.TableBuilder builder = + catalogConfig .catalog() - .createTable(TableIdentifier.parse(identifier), schema, spec, tableProps); + .buildTable(tableId, schema) + .withPartitionSpec(spec) + .withSortOrder(sortOrder); Review Comment: Great use of TableBuilder! I didn't know we could do that 😁 ########## sdks/java/io/iceberg/src/test/java/org/apache/beam/sdk/io/iceberg/SortOrderUtilsTest.java: ########## @@ -0,0 +1,111 @@ +/* + * 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.beam.sdk.io.iceberg; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Collections; +import org.apache.iceberg.NullOrder; +import org.apache.iceberg.Schema; +import org.apache.iceberg.SortDirection; +import org.apache.iceberg.SortField; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.types.Types; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class SortOrderUtilsTest { + private static final Schema SCHEMA = Review Comment: Also include a test to see how it handles extra whitespace? like `" id desc "` -- 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]
