comphead commented on code in PR #7824:
URL: https://github.com/apache/datafusion-comet/pull/7824#discussion_r4778318986
##########
spark/src/main/scala/org/apache/spark/sql/comet/operators.scala:
##########
@@ -1586,6 +1586,30 @@ object CometSortExec extends
CometOperatorSerde[SortExec] {
override def enabledConfig: Option[ConfigEntry[Boolean]] =
Some(CometConf.COMET_EXEC_SORT_ENABLED)
+ /**
+ * Spark's sorter computes only the first sort key for every row and
evaluates a later key only
+ * when the earlier keys tie, while the native sort evaluates every key over
the whole batch
+ * (https://github.com/apache/datafusion-comet/issues/7470). A later key
that the child's
+ * partitioning already computed, such as a key of the range exchange
beneath a global sort, was
+ * evaluated by Spark for every row, so it does not count.
+ */
+ override def getSupportLevel(op: SortExec): SupportLevel = {
+ laterSortKeyThatCanFail(op.sortOrder,
partitionedBy(op.child.outputPartitioning, _)).foreach {
+ s =>
+ return Incompatible(
+ Some(s"sort key ${s.child.sql} can fail on rows that Spark's
ordering skips"))
+ }
+ Compatible()
+ }
+
+ private def partitionedBy(partitioning: Partitioning, key: Expression):
Boolean =
+ partitioning match {
+ case RangePartitioning(ordering, _) =>
ordering.exists(_.child.semanticEquals(key))
+ case HashPartitioning(expressions, _) =>
expressions.exists(_.semanticEquals(key))
+ case PartitioningCollection(partitionings) =>
partitionings.exists(partitionedBy(_, key))
Review Comment:
Neither new fixture reaches the `PartitioningCollection` branch. `git grep
-c JOIN` on `sort_later_key_ansi.sql` gives 0 and the AQE fixture has no join
either. The PR text claims the collection case, which a join output
partitioning produces, so please add a query that hits it, such as a sort over
a join whose output partitioning holds the later key. Otherwise drop the branch.
##########
spark/src/test/resources/sql-tests/operators/sort_later_key_ansi_aqe.sql:
##########
@@ -0,0 +1,40 @@
+-- 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.
+
+-- sort_later_key_ansi.sql with adaptive execution on, and with a hash
exchange beneath the sort.
+-- An exchange that partitions by the later key computed it for every row, so
the sort stays in
+-- Comet (https://github.com/apache/datafusion-comet/issues/7470).
+
+-- Config: spark.sql.ansi.enabled=true
+-- Config: spark.sql.adaptive.enabled=true
Review Comment:
This file repeats the table setup and the `ORDER BY a, b + 1` query of
`sort_later_key_ansi.sql`, and the only new coverage is the two `DISTRIBUTE BY`
queries. `sort_later_key_ansi.sql` already flips `spark.sql.ansi.enabled`
mid-file with a `statement SET`, so the two `DISTRIBUTE BY` queries could sit
at the end of that file after `SET spark.sql.adaptive.enabled=true`, and the
hash exchange case would then also run with AQE off. I did not run this, so
please check that the setting takes effect mid-file before moving them.
##########
spark/src/main/scala/org/apache/spark/sql/comet/operators.scala:
##########
@@ -1586,6 +1586,30 @@ object CometSortExec extends
CometOperatorSerde[SortExec] {
override def enabledConfig: Option[ConfigEntry[Boolean]] =
Some(CometConf.COMET_EXEC_SORT_ENABLED)
+ /**
+ * Spark's sorter computes only the first sort key for every row and
evaluates a later key only
+ * when the earlier keys tie, while the native sort evaluates every key over
the whole batch
+ * (https://github.com/apache/datafusion-comet/issues/7470). A later key
that the child's
+ * partitioning already computed, such as a key of the range exchange
beneath a global sort, was
+ * evaluated by Spark for every row, so it does not count.
+ */
+ override def getSupportLevel(op: SortExec): SupportLevel = {
+ laterSortKeyThatCanFail(op.sortOrder,
partitionedBy(op.child.outputPartitioning, _)).foreach {
+ s =>
+ return Incompatible(
+ Some(s"sort key ${s.child.sql} can fail on rows that Spark's
ordering skips"))
+ }
+ Compatible()
+ }
+
+ private def partitionedBy(partitioning: Partitioning, key: Expression):
Boolean =
+ partitioning match {
+ case RangePartitioning(ordering, _) =>
ordering.exists(_.child.semanticEquals(key))
+ case HashPartitioning(expressions, _) =>
expressions.exists(_.semanticEquals(key))
Review Comment:
With AQE on, a coalesced read of a hash exchange reports
`CoalescedHashPartitioning(from, partitions)` from
`AQEShuffleReadExec.outputPartitioning` (`isCoalescedRead` branch in v3.5.8,
v4.0.1 and v4.1.1). That class is not a `HashPartitioning`, so it reaches `case
_ => false` here and the sort falls back even though the exchange computed the
key for every row. Coalescing is on by default and is what small hash shuffles
hit, so the hash exemption described in the PR would mostly not apply under
AQE. `operators.scala` already special-cases it by class name for the broadcast
join output partitioning (around lines 3564 and 3605). Please add a
`CoalescedHashPartitioning(from, _)` case that checks `from.expressions`.
Matching `HashPartitioningLike` instead would be wrong because
`KeyGroupedPartitioning` extends it and nothing evaluates its keys. I could not
run `sort_later_key_ansi_aqe.sql`, so I do not know whether its `DISTRIBUTE BY
b + 1` exchange is coalesced there. If it is and the
query passes, please say how.
--
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]