naveenp2708 opened a new pull request, #60530:
URL: https://github.com/apache/spark/pull/60530

   ### What changes were proposed in this pull request?
   
   Backport of #59789 to branch-4.x.
   
   `DataSourceV2Strategy` now decides the storage-partitioned join once, when 
it plans the scan: it keeps the scan's `keyGroupedPartitioning` only if 
`spark.sql.sources.v2.bucketing.enabled` is on, and passes that into the 
`BatchScanExec` it builds. `DataSourceV2ScanExecBase.reportedKeyedPartitioning` 
doesn't read the config anymore. So when the planned node gets copied — like 
the copy `PlanAdaptiveDynamicPruningFilters` makes when it rebuilds the scan at 
stage creation — it keeps the keyed layout it was planned with instead of 
working it out again from the live config.
   
   The pick needed one adjustment for branch-4.x: `DataSourceV2Strategy` 
conflicted because 4.x doesn't have SPARK-60465, so the incoming change arrived 
wrapped in that commit's checked-column arguments. Resolved by applying the 
same gate at 4.x's `BatchScanExec` call site, without those arguments. The 
`DataSourceV2ScanExecBase` change and the `DSv2PlanMergingSuite` comment tweak 
applied as on master. The new test landed next to the existing SPARK-60752 
tests in `KeyGroupedPartitioningSuite`; both are kept.
   
   ### Why are the changes needed?
   
   A storage-partitioned join can give wrong results, or fail, when 
`spark.sql.sources.v2.bucketing.enabled` gets turned off between planning and 
execution. The case is DPP on the keyed scan of a join with no 
`GroupPartitionsExec` above it — two sources that already report one split per 
key.
   
   `PlanAdaptiveDynamicPruningFilters` builds a fresh `BatchScanExec` when the 
stage is created. Before this fix that new node read the config again, reported 
no `KeyedPartitioning`, and `PushDownUtils.replanWithRuntimeFilters` handed 
back the splits in the connector's order, without the empty partitions for the 
pruned keys. The other side still had the sorted, padded layout the join was 
planned with, so the sort-merge join lined up the wrong partitions. With 
`p.price > 0.0` you get 2 rows instead of 7; once a runtime filter prunes 
splits (`p.price > 5.0`) it fails with `Can't zip RDDs with unequal numbers of 
partitions`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A storage-partitioned join whose 
`spark.sql.sources.v2.bucketing.enabled` is turned off between planning and 
execution used to give wrong results or fail. Now it's correct.
   
   - Before: `SELECT i.id, i.name, p.price FROM items i JOIN purchases p ON 
i.id = p.item_id WHERE p.price > 0.0`, planned with bucketing on and run with 
it off, returned 2 of the 7 matching rows; the same join with `p.price > 5.0` 
failed with `Can't zip RDDs with unequal numbers of partitions`.
   - After: both return the right rows (7 and 2).
   
   ### How was this patch tested?
   
   The `SPARK-60260` test in `KeyGroupedPartitioningSuite`, four shapes, each 
planned with bucketing on and then executed with it off: (1) one split per key 
on both sides, no `GroupPartitionsExec` above the join; (2) two rows per id so 
a `GroupPartitionsExec` sits above the pruned scan; (3) the other side 
unpartitioned with the SPJ shuffle on; (4) a scalar subquery over the keyed 
column, which becomes a runtime filter of the keyed scan the same way DPP does 
(DPP off, AQE on and off). Each checks the rows and that the rebuilt keyed scan 
kept its `KeyedPartitioning`. Confirmed it fails on branch-4.x without the fix 
and passes with it; reran the existing `SPARK-60752` tests in the same suite; 
scalastyle clean.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   No
   


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