ammarchalifah opened a new pull request, #60295: URL: https://github.com/apache/spark/pull/60295
### What changes were proposed in this pull request? Backport of [#59599](https://github.com/apache/spark/pull/59599) ([SPARK-60232](https://issues.apache.org/jira/browse/SPARK-60232), [SPARK-60552](https://issues.apache.org/jira/browse/SPARK-60552)) and [#60058](https://github.com/apache/spark/pull/60058) ([SPARK-60460](https://issues.apache.org/jira/browse/SPARK-60460)) onto `branch-4.0`. When storage-partitioned joins are nested, `EnsureRequirements` pushes join key positions, common partition values, and reducers into the inner join's `BatchScanExec`s first. The outer join then wrote a new set into the same scans and replaced the inner one, so the scan reported a different partitioning than the inner join was planned against. The outer push also continued into a shuffle and a broadcast, and it left a one-side shuffle at the partition count it was planned with. The outer push now composes with parameters an inner join already pushed, and it stops at a `ShuffleExchangeLike` and a `BroadcastExchangeLike`. If the push would change the values or the key positions a nested one-side shuffle routes on, the outer join shuffles instead. `StoragePartitionJoinParams` is still in `org.apache.spark.sql.execution.datasources.v2` on 4.0 ([SPARK-53272](https://issues.apache.org/jira/browse/SPARK-53272) moved it to `execution.joins` in 4.1). The import points at the 4.0 class. The rest matches the 4.1 fix. ### Why are the changes needed? On 4.0 these plans return wrong rows or fail: - The outer join replaces the inner `joinKeyPositions` (for example `[1]` with `[0]`), drops the inner reducers (`bucket(4)` no longer matches `bucket(12)`), or misaligns common partition values (`ArrayIndexOutOfBoundsException`). - [SPARK-60552](https://issues.apache.org/jira/browse/SPARK-60552): an SPJ with a one-side shuffle pushes common values into the partitioned scan below the shuffle, which drops every other partition. Under `ORDER BY` with `spark.sql.sources.v2.bucketing.sorting.enabled`, a LEFT JOIN returns null for every column of the shuffled side. - [SPARK-60460](https://issues.apache.org/jira/browse/SPARK-60460): with `spark.sql.sources.v2.bucketing.shuffle.enabled=true`, the inner shuffle stays at its old partition count after the outer push. AQE off fails with `Can't zip RDDs with unequal numbers of partitions`. AQE on fails with `PartitioningCollection requires all of its partitionings have the same numPartitions`. Reordered or narrowed keys return no rows or drop a row. ### Does this PR introduce _any_ user-facing change? Yes, for storage-partitioned joins on `branch-4.0` (4.0.x). The queries above return the correct rows. Some plans now shuffle where 4.0 kept SPJ: an outer key on a scan an inner join already reduced, a key the check cannot trace back to an unreduced position (such as a cast broadcast build key), a forced skew-join re-run that would apply a reducer twice, and an `ORDER BY` whose sort would reorder values a one-side shuffle already routed. A nested one-side shuffle the outer push would change is shuffled too. A push that leaves that shuffle's values and positions as they are stays SPJ. `branch-4.1` already has this fix. `master` and `branch-4.2+` regroup above the inner join with `GroupPartitionsExec` and are unchanged. ### How was this patch tested? Added 37 tests to `KeyGroupedPartitioningSuite`: 29 for SPARK-60232, one for SPARK-60552, seven for SPARK-60460. Each runs with AQE on and off, except the three skew-join tests, which need AQE. The fallback and skew tests assert the shuffle count. Locally, `KeyGroupedPartitioningSuite` and `EnsureRequirementsSuite` ran 156 tests, 0 failures. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Grok 4.7 -- 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]
