rekbun opened a new pull request, #60501: URL: https://github.com/apache/spark/pull/60501
### What changes were proposed in this pull request? `DataSourceV2ScanExecBase.outputOrdering` now skips a reported sort order on a transform that is itself a key of the scan's `KeyedPartitioning`, instead of letting it end the leading run of kept sort orders (SPARK-59995). Such a sort order is constant within each partition, like any partition key, so the sort orders after it still hold. A sort order on any other transform still ends the run, and sort orders on transforms are still left out of the output ordering. A transform sort order matches a transform key by `isSameFunction` and semantically equal children, not by expression equality: the two are bound separately, and `BoundFunction` is only asked, not required, to override `equals`. The `SupportsReportOrdering` Javadoc, the doc of `spark.sql.sources.v2.bucketing.partitionKeyOrdering.enabled`, `sql-performance-tuning.md` and the 4.4 migration note are updated to match. ### Why are the changes needed? The sort orders after a transform key were dropped although they hold: | Partition keys | Reported ordering | Output ordering before | After | |---|---|---|---| | `years(ts)` | `[years(ts), id]` | `[]` | `[id]` | | `years(ts), id` | `[years(ts), id, data]` | `[id]` | `[id, data]` | So a sort over those columns, or a sort merge join on them, kept a `SortExec`. For example, two tables that report `[years(t), t]` over the key `years(t)` and are joined on `t`, with `spark.sql.sources.v2.bucketing.preserveOrderingOnCoalesce.enabled`, got a `SortExec` on each side. They now use the k-way merge of `GroupPartitionsExec`, with no sort, and return the same rows. The JIRA's third case, `[days(ts)]` over the keys `days(ts), id`, is already handled on master by SPARK-59981. ### Does this PR introduce _any_ user-facing change? Yes, for V2 sources that report a transform partition key and an ordering that contains it: plans can drop sorts and use the k-way merge. In one case the output ordering changes in another way. With the keys `years(ts), id` and the reported ordering `[years(ts), data]`, the output ordering is now `[data]` instead of `[id]`. A kept sort order takes precedence over the one derived from the keys, as a reported `[data]` over the key `id` already does. The derived `[id]` is new in 4.4 (SPARK-59981), so against released versions this is `[]` → `[data]`. ### How was this patch tested? Two new tests in `KeyGroupedPartitioningSuite`: - The output orderings in the table above and the `[data]` case, with no sort planned for them. It includes a sort order bound to a different function object with the same canonical name. - The k-way-merged join on `t`, with no `SortExec` and no shuffle. Both fail without the fix. `KeyGroupedPartitioning*Suite`, `DataSourceV2Suite`, `WriteDistributionAndOrderingSuite`, `GroupPartitionsExecSuite`, `EnsureRequirementsSuite` and the plan merging suites pass. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Claude Opus 5.5) -- 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]
