peter-toth opened a new pull request, #60123:
URL: https://github.com/apache/spark/pull/60123

   ### What changes were proposed in this pull request?
   
   Backport of #60030 to branch-4.x, tailored for it:
   - `BatchScanExec` has no storage filter fields here, since SPARK-60465 is 
master only. So the constructor change and the positional test patterns are on 
its 6-field form.
   - `DataSourceV2Strategy` passes `relation.keyGroupedPartitioning` as before, 
since SPARK-60260 is not on branch-4.x.
   - The comment fix in the SPARK-60260 test is left out, since that test is 
not here.
   
   `BatchScanExec` now takes a `PlannedScan` in place of its `Scan`. A 
`PlannedScan` holds the `Scan`, with the `Batch`, its reader factory and the 
input partitions planned from it once, when a node first asks for them. 
`DataSourceV2Strategy` builds one for each node, and the copies later rules 
make of the node share it. So they read the partitions the original node 
planned, with the reader factory of the `Batch` that planned them. A pushed 
runtime filter still replans from a new `Batch`, as before. A node over another 
`Scan` needs another `PlannedScan`.
   
   Nothing is planned earlier than before.
   - The first node that asks plans the partitions, as `inputPartitions` did.
   - A copy reuses them instead of planning again, so a connector's 
`planInputPartitions()` runs less often.
   
   This follows SPARK-59289, which made the derived values of 
`GroupPartitionsExec` constructor fields for the same reason. A lazy val 
belongs to one instance, and a copy computes it again. Sharing a holder between 
copies is what `InMemoryRelation` does with its `CachedRDDBuilder`.
   
   ### Why are the changes needed?
   
   A storage-partitioned join can fail when the build side of a DPP subquery 
has a runtime filter of its own. With AQE off:
   1. A DPP subquery that does not reuse a broadcast plans its build side 
again. Its `BatchScanExec` wraps the same `Scan` instance as the join's scan on 
that side.
   2. The build side has a runtime filter of its own. It can be a DPP filter 
from another join, or a scalar subquery filter routed there by SPARK-56467. The 
DPP subquery runs first, and its `filter()` call prunes the shared `Scan`. The 
runtime filtering contracts allow that.
   3. The join's `BatchScanExec` is a copy made after planning. 
`ReuseExchangeAndSubquery` made it when it replaced a subquery in the node's 
runtime filters with a reused one. The copy plans its input partitions again, 
from the already pruned `Scan`.
   4. So the copy reports fewer partitions than the join was planned for:
      - without a `GroupPartitionsExec` on that side, the join fails with 
`Can't zip RDDs with unequal numbers of partitions`;
      - with one, it fails with `[INTERNAL_ERROR] GroupPartitionsExec's child 
no longer reports the partitioning it was planned over`.
   
   For example, take a connector whose `filter()` drops the partitions an `=` 
predicate rules out, with `items` partitioned by `id` and `purchases` 
partitioned by `item_id`, ids 1 to 7 with one split per key. With DPP on, 
`spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnly=false`, 
`spark.sql.optimizer.dynamicPartitionPruning.fallbackFilterRatio=10`, 
`spark.sql.autoBroadcastJoinThreshold=-1` and AQE off:
   
   ```sql
   SELECT i.id, i.name, p.price FROM items i JOIN purchases p ON i.id = 
p.item_id
   WHERE i.id = (SELECT max(item_id) FROM purchases WHERE price > 4.0)
   ```
   
   fails with `Can't zip RDDs with unequal numbers of partitions: List(7, 1)`.
   
   The same happens when `purchases` is first joined with a broadcast 
dimension, whose DPP filter is then the build side's own runtime filter. A 
connector that prunes on `IN` only is enough for that.
   
   With AQE on, a scan in the default `PARTITION_DEFINED` columnar support mode 
is not affected. AQE asks the copies `supportsColumnar` when it creates the 
result stage, and in that mode this plans their partitions before the DPP 
subquery runs. A scan that declares `SUPPORTED` or `UNSUPPORTED` plans nothing 
there, so it fails the same way with AQE on.
   
   Affected releases:
   - 4.2.0 and later (branch-4.2, branch-4.3, branch-4.x and master), as above. 
The `GroupPartitionsExec` internal error comes from SPARK-59289, which only 
branch-4.x and master have.
   - 4.1 and 4.0, with AQE off, only with 
`spark.sql.sources.v2.bucketing.pushPartValues.enabled=false` or when the build 
side's filter prunes every split. Otherwise their `BatchScanExec` pads to the 
partition values in `spjParams`, which a copy keeps.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Such a query now returns its rows instead of failing.
   
   For code that builds or rebuilds `BatchScanExec`, the second parameter is 
now a `PlannedScan`. A new node takes `new PlannedScan(scan)`. A rule that 
rebuilds a node over the same `Scan` passes on its `plannedScan`, or uses 
`copy`, so the rebuilt node keeps the planned partitions. Code that matches the 
node reads the scan through `BatchScanExec.scan`, or with `case 
BatchScanExec(_, PlannedScan(scan), ...)`.
   
   ### How was this patch tested?
   
   New tests in `KeyGroupedPartitioningRuntimeFilterTests`, with AQE off. Each 
checks the rows and that the join plans no shuffle. With AQE on, the fixtures' 
scans keep the default columnar support mode, so those runs cannot fail without 
the fix.
   - A scalar subquery filter, with one split per key. Without the fix it fails 
in `KeyGroupedPartitioningCatalystRuntimeFilterSuite` with `List(7, 1)`. 
`KeyGroupedPartitioningRuntimeFilterSuite` runs it too, but its fixture prunes 
on `IN` only, so it passes there without the fix.
   - A DPP filter from a broadcast dimension, with two splits per key, once 
keeping 3 keys and once pruning every split. Without the fix each fails in both 
suites with the `GroupPartitionsExec` internal error.
   
   A new test in `KeyGroupedPartitioningSuite` runs the DPP filter case over a 
scan that declares `UNSUPPORTED`, with AQE on and off. Without the fix it fails 
with the `GroupPartitionsExec` internal error with AQE on too.
   
   A new test in `SparkPlanSuite`, over a `Scan` that returns a new `Batch` 
from each `toBatch`. The copies `makeCopy`, `clone` and `copy` make share the 
node's `Batch` and partitions. Without the fix it fails at the first assertion.
   
   The SPARK-60618 test that rebuilds a scan with new partitions now gives the 
rebuilt node a new `PlannedScan`, since a copy keeps the planned ones.
   
   Also ran `KeyGroupedPartitioningSuite`, `GroupPartitionsExecSuite`, 
`SameResultSuite`, `SQLAppStatusListenerSuite`, `EnsureRequirementsSuite`, the 
`DynamicPartitionPruningV2*` suites, `DataSourceV2CatalystRuntimeFilterSuite`, 
`DataSourceV2SQLSuiteV2Filter`, `DSv2PlanMergingSuite`, and the suites whose 
`BatchScanExec` patterns now match the `Scan` through `PlannedScan`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code with 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]

Reply via email to