ulysses-you opened a new pull request, #60553:
URL: https://github.com/apache/spark/pull/60553

   ### What changes were proposed in this pull request?
   
   `PushDownShuffleExchangeThroughUnion` may now pick a V2 keyed layout 
(`KeyedPartitioning`) as the union's output, not only a hash layout. Both the 
exchange it replaces and the target can be keyed.
   
   - A keyed target keeps the union children that already report the picked 
domain, and an unmarked child reporting a subset of its keys keeps its layout 
too. A child carrying the unknown-keys marker has to declare the domain's key 
list in the same order and still route by hash; one that lost its routing is 
laid out instead, which routes its undeclared keys again.
   - A leg laid out on the domain records `HASH_ROUTING`. When the 
concatenation is not grouped, a `GroupPartitionsExec` groups it, and the 
grouped layout keeps the full-key claim only (`ROUTING_LOST`).
   - Candidate admission is `canEvaluatePartitioning && KeyedShuffleSpec(keyed, 
required).canCreatePartitioning`: the rebuilt union's layout is a result and 
cannot answer for the shuffle template, so the question is settled on the 
candidate.
   - After the rebuild the child has to satisfy the requirement as rebuilt and 
pair with the siblings; the V2 co-partition key requirement and the rule's own 
`allowPartitionKeysSubsetOfClusterKeys` are applied there, against the rebuilt 
layout.
   - `KeyLayout` gains `describesSameKeys`, `describesSubsetOfKeys`, 
`describesSameKeySet` and `describesSameLayoutState`, which `reportsDomain` 
compares legs and targets with.
   
   Keyed cases the rule supports, each covered end to end:
   
   1. A bucketed table beside a leg without a layout: the scan keeps its 
partitions, only the other leg is laid out on the domain, and a key the domain 
does not declare is hash-routed and stays whole (1 shuffle). Shape: a union 
over a `VALUES` leg.
   2. An unmarked leg holding a subset of the target's keys keeps its layout; 
the bigger domain is the target (1 shuffle). Shape: domains `{1,2}` and 
`{1,2,3}`.
   3. Legs declaring different domains: one domain is picked and the other legs 
are laid out on it (2 shuffles). Shape: domains `{1,2}` and `{1,3}`.
   4. Two of three legs report one keyed domain and the third is a 
`REPARTITION(2, id)` leg: the keyed domain keeps more rows, so it wins the 
ranking (kept bytes, then kept children), and the repartition leg is laid out 
on it on top of its own exchange, since a repartition is not retargeted to a 
keyed layout (2 shuffles).
   5. A keyed union side in a merge join keeps its own domain and pairs with a 
keyed table as it stands (1 shuffle).
   6. A leg whose layout the planner marked as hash-routed is kept beside an 
unmarked scan on the same domain (2 shuffles: the join's alignment and the 
values leg). Shape: a `RIGHT OUTER` join between a table bucketed by 
`identity(id)` and one bucketed by `bucket(2, id)`, which lays the left side 
out on the right's domain while the outer join reports the right side's marked 
layout. Needs `keepJoinBranches` on, or a join branch is never kept.
   7. A grouped but unsorted domain: the grouping above sorts it, so such a leg 
supplies the target (1 shuffle). Shape: an inner union in child order under an 
aggregate.
   
   New config 
`spark.sql.adaptive.pushDownShuffleThroughUnion.allowV2Bucketing.enabled` (on 
by default) gates the keyed targets. The doc of 
`spark.sql.adaptive.pushDownShuffleThroughUnion.enabled` now says the shuffle 
and the target are hash or keyed.
   
   With every leg reporting the same domain, `EnsureRequirements` groups the 
union in place and adds no exchange, so the rule has no site there.
   
   ### Why are the changes needed?
   
   V2 connectors report `KeyedPartitioning`, and the rule only accepted hash 
layouts. A union over bucketed tables therefore had to shuffle every row even 
when a leg already sat on a keyed domain. Keeping the domain leaves only the 
legs without one to be laid out.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, one new config: 
`spark.sql.adaptive.pushDownShuffleThroughUnion.allowV2Bucketing.enabled` (on 
by default). It only takes effect when a keyed target is reachable, which needs 
all of the following:
   
   - `spark.sql.adaptive.pushDownShuffleThroughUnion.enabled` turned on (off by 
default), otherwise the rule does not run;
   - `spark.sql.unionOutputPartitioning` left on (on by default), since the 
rebuilt union has to report the keyed layout;
   - `spark.sql.sources.v2.bucketing.enabled` left on (on by default);
   - `spark.sql.sources.v2.bucketing.shuffle.enabled` turned on (off by 
default), since a child has to be shuffleable onto the domain;
   - `spark.sql.sources.v2.bucketing.partiallyClusteredDistribution.enabled` 
left off (off by default).
   
   A target naming only part of the consumer's cluster keys also needs 
`spark.sql.sources.v2.bucketing.requireAllClusterKeysForCoPartition.enabled` 
turned off (on by default) and 
`spark.sql.adaptive.pushDownShuffleThroughUnion.allowPartitionKeysSubsetOfClusterKeys.enabled`
 turned on (off by default). Keeping a join branch (case 6) additionally needs 
`spark.sql.adaptive.pushDownShuffleThroughUnion.keepJoinBranches.enabled` on 
(off by default).
   
   No plan changes under the defaults above.
   
   ### How was this patch tested?
   
   - `KeyGroupedPartitioningSuite`: 14 end-to-end tests over V2 bucketed 
tables, asserting the executed plan and its shuffle count.
   - `PushDownShuffleExchangeThroughUnionSuite`: plan-level tests for the keyed 
paths the end-to-end shapes do not reach, such as a marked leg whose key order 
differs, a leg that lost its routing, and a recorded repartition origin.
   - `ShuffleSpecSuite`: the `KeyLayout` comparisons, including the type clause 
that distinguishes two empty key lists.
   - Suites: `ShuffleSpecSuite` 47/47, 
`PushDownShuffleExchangeThroughUnionSuite` 132/132, 
`KeyGroupedPartitioningSuite` 262/262.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: DeepSeek Flash
   


-- 
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]

Reply via email to