dramaticlly commented on code in PR #16750: URL: https://github.com/apache/iceberg/pull/16750#discussion_r4792442448
########## spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/source/TestSupportsReportOrdering.java: ########## @@ -0,0 +1,821 @@ +/* + * 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.spark.source; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.DeleteFile; +import org.apache.iceberg.FileScanTask; +import org.apache.iceberg.Files; +import org.apache.iceberg.NullOrder; +import org.apache.iceberg.ParameterizedTestExtension; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.ReplaceSortOrder; +import org.apache.iceberg.RowLevelOperationMode; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableProperties; +import org.apache.iceberg.TableUtil; +import org.apache.iceberg.TestHelpers; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.data.FileHelpers; +import org.apache.iceberg.data.GenericRecord; +import org.apache.iceberg.data.Record; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.spark.Spark3Util; +import org.apache.iceberg.spark.SparkReadOptions; +import org.apache.iceberg.spark.SparkSQLProperties; +import org.apache.iceberg.spark.TestBaseWithCatalog; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.util.Pair; +import org.apache.spark.sql.connector.expressions.Expression; +import org.apache.spark.sql.connector.expressions.LiteralValue; +import org.apache.spark.sql.connector.expressions.SortOrder; +import org.apache.spark.sql.connector.expressions.filter.Predicate; +import org.apache.spark.sql.connector.read.ScanBuilder; +import org.apache.spark.sql.execution.SortExec; +import org.apache.spark.sql.internal.SQLConf; +import org.apache.spark.sql.types.DataTypes; +import org.apache.spark.sql.types.StructType; +import org.apache.spark.sql.util.CaseInsensitiveStringMap; +import org.apache.spark.unsafe.types.UTF8String; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; + +@ExtendWith(ParameterizedTestExtension.class) +class TestSupportsReportOrdering extends TestBaseWithCatalog { + + private static final String OTHER_TABLE = "other_table"; + private static final String BUCKETED_8 = "bucketed_8"; + private static final String BUCKETED_4 = "bucketed_4"; + private static final Map<String, String> ORDERING_ENABLED = orderingConf(true); + private static final Map<String, String> ORDERING_DISABLED = orderingConf(false); + + private static Map<String, String> orderingConf(boolean preserveOrdering) { + return ImmutableMap.<String, String>builder() + .put(SparkSQLProperties.PRESERVE_DATA_ORDERING, String.valueOf(preserveOrdering)) + .put(SparkSQLProperties.PRESERVE_DATA_GROUPING, "true") + .put(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD().key(), "-1") + .put(SQLConf.ADAPTIVE_EXECUTION_ENABLED().key(), "false") + .put(SQLConf.V2_BUCKETING_ENABLED().key(), "true") + .put(SQLConf.V2_BUCKETING_PUSH_PART_VALUES_ENABLED().key(), "true") + .put(SQLConf.REQUIRE_ALL_CLUSTER_KEYS_FOR_CO_PARTITION().key(), "false") + .put(SQLConf.REPLACE_HASH_WITH_SORT_AGG_ENABLED().key(), "true") + .buildOrThrow(); + } + + private static Map<String, String> orderingEnabledWith(String key, String value) { + Map<String, String> conf = Maps.newHashMap(ORDERING_ENABLED); + conf.put(key, value); + return conf; + } + + @AfterEach + void removeTables() { + sql("DROP TABLE IF EXISTS %s", tableName); + sql("DROP TABLE IF EXISTS %s", tableName(OTHER_TABLE)); + sql("DROP TABLE IF EXISTS %s", tableName(BUCKETED_8)); + sql("DROP TABLE IF EXISTS %s", tableName(BUCKETED_4)); + } + + @TestTemplate + void windowUsesReportedOrdering() { + createPartitionedTable(tableName); + + assertSortRemovedAndResultUnchanged( + "SELECT p, id, row_number() OVER (PARTITION BY p ORDER BY id) AS rn FROM %s", tableName); + } + + @TestTemplate + void partitionSplitAcrossTaskGroupsKeepsSortWithoutSortedMerge() { + createPartitionedTable(tableName); + sql("ALTER TABLE %s SET TBLPROPERTIES ('%s' = '1')", tableName, TableProperties.SPLIT_SIZE); + + assertSortKeptAndResultUnchanged( + "SELECT p, id, row_number() OVER (PARTITION BY p ORDER BY id) AS rn FROM %s", tableName); + } + + @TestTemplate + void partitionSplitAcrossTaskGroupsUsesReportedOrderingWithSortedMerge() { + createPartitionedTable(tableName); + sql("ALTER TABLE %s SET TBLPROPERTIES ('%s' = '1')", tableName, TableProperties.SPLIT_SIZE); Review Comment: ask: can we have a test with bigger split group size, so exercise the path for one key with ≥ 2 task groups that each hold ≥ 2 files ########## spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkPartitioningAwareScan.java: ########## @@ -135,6 +179,12 @@ public Partitioning outputPartitioning() { } } + @Override + public synchronized SortOrder[] outputOrdering() { + taskGroups(); + return reportedOrdering; Review Comment: not a spark expert on this but it seems we might end up with different report ordering for partitioned table even when the feature is off? It seems no way to express empty sort order as it always wrapped in `Some()` in spark 4.2? https://github.com/apache/spark/blob/50c3fda68e2e789ffb4e8033d6cc098258031d5b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanPartitioningAndOrdering.scala#L78 Before this change it reports `outputOrdering=ArraySeq(p#27 ASC NULLS FIRST) ordering=None` and now it reports `REPRO outputOrdering=ArraySeq() ordering=Some(ArraySeq())` ########## spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/SparkSQLProperties.java: ########## @@ -40,6 +40,16 @@ private SparkSQLProperties() {} "spark.sql.iceberg.planning.preserve-data-grouping"; public static final boolean PRESERVE_DATA_GROUPING_DEFAULT = false; + // Controls whether to preserve the sort ordering of data while reading + public static final String PRESERVE_DATA_ORDERING = + "spark.sql.iceberg.planning.preserve-data-ordering"; + public static final boolean PRESERVE_DATA_ORDERING_DEFAULT = false; + + // Controls the max number of files in a task when preserving data ordering Review Comment: task seems a bit too general and ambitious, I think we want to say the limit is max number of files a given read split can merge when preserving the data ordering ########## spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/source/TestSupportsReportOrdering.java: ########## @@ -0,0 +1,821 @@ +/* + * 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.spark.source; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.DeleteFile; +import org.apache.iceberg.FileScanTask; +import org.apache.iceberg.Files; +import org.apache.iceberg.NullOrder; +import org.apache.iceberg.ParameterizedTestExtension; Review Comment: I think we might need to rebase after #19220 as we move toward Junit native. ########## spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkPartitioningAwareScan.java: ########## @@ -103,14 +112,49 @@ abstract class SparkPartitioningAwareScan<T extends PartitionScanTask> extends S this.partitionPredicates = Lists.newArrayList(partitionPredicates); this.partitionPredicateFields = activePartitionFields(table.spec()); this.partitionPredicateType = activePartitionType(table.spec()); + this.preserveDataOrdering = + readConf.preserveDataOrdering() && canPreserveDataOrdering(spark, table); + this.maxFilesPerOrderedTask = + preserveDataOrdering ? readConf.preserveDataOrderingMaxFilesPerTask() : 0; if (scan == null) { this.specs = Collections.emptySet(); this.tasks = Collections.emptyList(); this.taskGroups = Collections.emptyList(); + this.reportedOrdering = NO_ORDERING; } } + private boolean canPreserveDataOrdering(SparkSession spark, Table table) { Review Comment: nit: this can be static if we pass in preserveDataGrouping as a boolean flag ########## spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkPartitioningAwareScan.java: ########## @@ -103,14 +112,49 @@ abstract class SparkPartitioningAwareScan<T extends PartitionScanTask> extends S this.partitionPredicates = Lists.newArrayList(partitionPredicates); this.partitionPredicateFields = activePartitionFields(table.spec()); this.partitionPredicateType = activePartitionType(table.spec()); + this.preserveDataOrdering = + readConf.preserveDataOrdering() && canPreserveDataOrdering(spark, table); + this.maxFilesPerOrderedTask = + preserveDataOrdering ? readConf.preserveDataOrderingMaxFilesPerTask() : 0; if (scan == null) { this.specs = Collections.emptySet(); this.tasks = Collections.emptyList(); this.taskGroups = Collections.emptyList(); + this.reportedOrdering = NO_ORDERING; } } + private boolean canPreserveDataOrdering(SparkSession spark, Table table) { + SQLConf sqlConf = spark.sessionState().conf(); + if (!preserveDataGrouping) { + LOG.warn( + "Ignoring {} for table {}: it requires {}", + SparkSQLProperties.PRESERVE_DATA_ORDERING, + table.name(), + SparkSQLProperties.PRESERVE_DATA_GROUPING); + return false; + + } else if (sqlConf.v2BucketingAllowCompatibleTransforms()) { + LOG.warn( + "Ignoring {} for table {}: incompatible with {}", + SparkSQLProperties.PRESERVE_DATA_ORDERING, + table.name(), + SQLConf.V2_BUCKETING_ALLOW_COMPATIBLE_TRANSFORMS().key()); + return false; + + } else if (sqlConf.v2BucketingAllowJoinKeysSubsetOfPartitionKeys()) { + LOG.warn( + "Ignoring {} for table {}: incompatible with {}", + SparkSQLProperties.PRESERVE_DATA_ORDERING, + table.name(), + SQLConf.V2_BUCKETING_ALLOW_JOIN_KEYS_SUBSET_OF_PARTITION_KEYS().key()); + return false; + } Review Comment: do we still need them for spark 4.2.1? -- 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]
