rdtr opened a new pull request, #7824: URL: https://github.com/apache/datafusion-comet/pull/7824
## Which issue does this PR close? Closes #7470. ## Rationale for this change Spark's `SortExec` computes only the first sort key for every row and evaluates a later key only for rows whose earlier keys tie. Comet's native sort evaluates every key over the whole batch, so under ANSI mode a later key such as `b + 1` raises `ARITHMETIC_OVERFLOW` on rows Spark never evaluates it for. `SELECT a, b FROM ps SORT BY a, b + 1` returns the rows in Spark and fails in Comet when `b` holds `2147483647` and no two rows tie on `a`. #7457 fixed the same shape for TopK; this is the plain-sort counterpart that the issue describes. ## What changes are included in this PR? `CometSortExec` now has a `getSupportLevel` that returns `Incompatible` when `laterSortKeyThatCanFail` finds a later key that can fail, with the same reason text as the TopK fallback, so `spark.comet.operator.SortExec.allowIncompatible=true` keeps such a sort in Comet. The rules for which keys can fail are #7457's, unchanged. A later key that the child's output partitioning already contains (a range exchange's ordering, or hash partitioning expressions, also inside a `PartitioningCollection`) does not count, because Spark's exchange computed it for every row before the sort, so Spark raises on the same rows. This keeps the common global `ORDER BY a, b + 1` over several partitions native. To support that, `laterSortKeyThatCanFail` takes an optional `evaluatedOnEveryRow` predicate; the TopK call site is unchanged. The Sort section of the compatibility guide replaces the note that pointed at #7470 with the new rule. ## How are these changes tested? New fixture `operators/sort_later_key_ansi.sql`: `SORT BY a, b + 1` and an `ORDER BY` over a `LIMIT` subquery (no exchange beneath either) fall back and return Spark's rows; later keys that cannot fail stay native; a failing first key and rows that tie on the first key raise `ARITHMETIC_OVERFLOW` in both engines; a global `ORDER BY a, b + 1` over a range exchange stays fully native, and raises in both engines on overflow; with ANSI off the sorts stay native. `operators/sort_later_key_ansi_aqe.sql` runs with adaptive execution on: `DISTRIBUTE BY b + 1 SORT BY a, b + 1` stays native over the hash exchange, and `DISTRIBUTE BY a SORT BY a, b + 1` falls back. Mutation checks: without the fallback the fixture fails with `ARITHMETIC_OVERFLOW` from the native sort, and without the partitioning exemption the range-exchange and hash-exchange queries are no longer native. `CometSqlFileTestSuite` passes on Spark 4.1.3 (default profile), and `sort_later_key_ansi.sql` and `topk_later_key_ansi.sql` pass on Spark 3.5 and 4.2 as well. -- 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]
