dongjoon-hyun opened a new pull request, #60565:
URL: https://github.com/apache/spark/pull/60565

   ### What changes were proposed in this pull request?
   
   This PR aims to fix a correctness issue of dynamic partition pruning (DPP). 
DPP no longer runs a standalone subquery over a filtering side that reads a 
DSv2 scan with a row-selecting pushdown, such as `LIMIT`, `OFFSET`, top-N or 
`TABLESAMPLE` (`onlyRowWisePushdowns = false`).
   
   `PartitionPruning.insertPredicate` now treats such a filtering side as 
having no pruning benefit. So its DPP filter only reuses the join's broadcast 
(`onlyInBroadcast = true`), and it is not inserted when exchange reuse is 
disabled. Reusing the broadcast is unchanged because the join and the DPP 
filter share a single read of the filtering side.
   
   ### Why are the changes needed?
   
   When `spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnly` is 
`false`, DPP plans a standalone subquery if it estimates a pruning benefit and 
no broadcast can be reused. The subquery reads the filtering side again, apart 
from the join. A DSv2 scan that took a `LIMIT` without `ORDER BY`, or a top-N 
with ties, may return other rows in that second read. DPP then prunes the fact 
partitions of the rows that the join reads, and the join silently loses rows.
   
   `hasSelectivePredicate` already ignores the pushed filters of such a scan 
for this reason, but a selective `Filter` above the scan still makes the side 
eligible. For example, Apache Spark 4.3.0 plans the following query against a 
JDBC catalog (Derby) with `reuseBroadcastOnly=false`, 
`spark.sql.autoBroadcastJoinThreshold=-1` and a small 
`spark.sql.defaultSizeInBytes`. The DPP subquery and the join read the JDBC 
scan with the pushed top-N separately.
   
   ```sql
   SELECT f.v FROM fact f
   JOIN (SELECT * FROM jdbc.APP.DIM ORDER BY V LIMIT 2) d ON f.p = d.K
   WHERE d.K > 0
   ```
   
   ```
   :        +- FileScan parquet spark_catalog.default.fact[v#7L,p#8] ... 
PartitionFilters: [(p#8 > 0), isnotnull(p#8), dynamicpruningexpression(p#8 IN 
dynamicpruning#11)], ...
   :              +- Subquery dynamicpruning#11, [id=#74]
   :                 ...
   :                          +- *(1) Filter (isnotnull(K#5) AND (K#5 > 0))
   :                             +- *(1) Scan JDBC v1 Relation from v2 scan 
jdbc.APP.DIM[K#5] PushedFilters: [], PushedTopN: ORDER BY [V ASC NULLS FIRST] 
LIMIT 2, ReadSchema: struct<K:int>
   +- *(4) Sort [K#5 ASC NULLS FIRST], false, 0
      +- Exchange hashpartitioning(K#5, 200), ENSURE_REQUIREMENTS, [plan_id=101]
         +- *(3) Filter (isnotnull(K#5) AND (K#5 > 0))
            +- *(3) Scan JDBC v1 Relation from v2 scan jdbc.APP.DIM[K#5] 
PushedFilters: [], PushedTopN: ORDER BY [V ASC NULLS FIRST] LIMIT 2, 
ReadSchema: struct<K:int>
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, this fixes wrong results when the internal configuration 
`spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnly` is `false`. 
With its default value `true`, DPP filters already reuse broadcasts only, so 
there is no behavior change.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test case. It uses a test DSv2 source 
whose reads of a pushed `LIMIT` return other rows. Without this fix, the test 
fails with both AQE on and off.
   
   ```
   !== Correct Answer - 1 ==   == Spark Answer - 1 ==
   ![0]                        [1]
   ```
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: 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