ulysses-you opened a new pull request, #60658:
URL: https://github.com/apache/spark/pull/60658
### What changes were proposed in this pull request?
AQE preparation gains a rule that removes one shuffle from a
storage-partitioned join whose shuffled side aggregates or windows on the join
keys. With `spark.sql.sources.v2.bucketing.shuffle.enabled`,
`EnsureRequirements` plans two shuffles there: a hash shuffle under the
aggregate and a keyed shuffle above it that lays the aggregate's output out on
the storage side's keys. A keyed layout that serves the aggregate's clustering
serves it as well as the hash layout did, so the rule retargets the hash
shuffle to the keyed layout and drops the keyed shuffle:
```
Before: After:
SortMergeJoin SortMergeJoin
:- Sort :- Sort
: +- Exchange(keyed [item_id]) : +- HashAggregate(key =
[item_id])
: +- HashAggregate(key = [item_id]) : +- Exchange(keyed [item_id])
: +- Exchange(hash [item_id]) : +- HashAggregate(partial)
: +- HashAggregate(partial) : +- ...
+- BatchScan(items, keyed [item_id]) +- BatchScan(items, keyed
[item_id])
```
The walk crosses grouped aggregates (hash, sort, object hash, and the batch
session merge step), partitioned windows, final window group limits,
deterministic projections and filters, local sorts, `GenerateExec`, and a
broadcast join's streamed side. A plain rename in an operator's output
(`item_id AS k`) is rewritten into its child's attribute space.
Admission:
- both shuffles are the ones `EnsureRequirements` inserted, and the lower
one is a hash shuffle with at least the keyed layout's partition count;
- the plan reuses neither shuffle;
- a crossed projection, filter or aggregate is deterministic;
- the shuffle can evaluate the layout on the rows it receives;
- the rebuilt subtree reports the removed shuffle's layout, and the whole
plan passes `ValidateRequirements` under the preparation's root distribution
and keeps every recorded `PartitioningOrigin`.
Supporting changes:
- `KeyedPartitioning.canTemplateShuffle` (layout grouped, undeclared rows
still hash-routed, expressions still describe the keys) is extracted and
`KeyedShuffleSpec.canCreatePartitioning` now calls it.
- `ValidateRequirements.validateWithOrigins` (root distribution plus
recorded user repartition layouts) is extracted and
`PushDownShuffleExchangeThroughUnion` uses it too.
- The rule runs in `queryStagePreparationRules` after `EnsureRequirements`
and before `StampUnionDecisions`.
- Two internal configs in `SQLConf` (`4.5.0`).
### Why are the changes needed?
The one-side shuffle of a storage-partitioned join leaves two shuffles on
the shuffled side, and the keyed one exists only to re-lay-out the aggregate's
output. When the keyed layout also serves the aggregate's own clustering, the
aggregate can run on it directly and the extra shuffle is redundant. The join
above no longer reads a shuffle stage directly, but it was not a skew-join
candidate before either: `OptimizeSkewedJoin` splits a shuffled join only when
both of its children are shuffle stages, and the other side is the layout's
source and is not shuffled.
### Does this PR introduce _any_ user-facing change?
Yes, plans change for queries that enable
`spark.sql.sources.v2.bucketing.shuffle.enabled` (non-default): the shapes
above lose one shuffle. The new config
`spark.sql.adaptive.pushDownKeyedShuffle.enabled` is internal, defaults to
true, and is inert unless the one-side shuffle is enabled;
`spark.sql.adaptive.pushDownKeyedShuffle.allowWithSubqueries.enabled` (default
false) opts a query containing subqueries into the rewrite. No API or signature
changes, and no change for users without the one-side shuffle.
### How was this patch tested?
New `PushDownKeyedShuffleExchangeSuite` (32 tests) covers:
- operators on the walk: hash / sort / object hash / session aggregate,
window (plain and aliased key), final window group limit, filter, generate,
broadcast hash join and broadcast nested loop join (both build sides);
- join shapes: inner, left, right, full outer, aliased keys, computed keys,
differing key orders, bucketed storage, subset keys, rows of one key with
declared and undeclared keys;
- declines: no keyed shuffle planned, user repartition below the operator,
build-side column as the join key, a non-deterministic projection or filter, a
window without a partition spec, a window on other keys, a join key the
aggregate computes, and a reused shuffle (both reuse-on and reuse-off arms);
- config gates: rule off, one-side shuffle off, partially clustered on,
subquery default and opt-in;
- plan-level cases for gates that no SQL query reaches: the
`canTemplateShuffle` clauses, the partition count, and a shuffle that cannot
evaluate the layout (including a third-party-inserted shuffle under a
projection), each with a positive control.
Existing suites run: `KeyGroupedPartitioningSuite`,
`PushDownShuffleExchangeThroughUnionSuite`, `PushDownLocalSortSuite`, and
`AdaptiveQueryExecSuite` (rule ordering), 410 + 156 tests, plus scalastyle on
`catalyst` and `sql`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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]