dongjoon-hyun opened a new pull request, #60575:
URL: https://github.com/apache/spark/pull/60575
### What changes were proposed in this pull request?
This PR fixes `PartitionPruning` not to plan a standalone dynamic partition
pruning (DPP) subquery when the filtering side or its join key has a UDF that
only the `Extract UDFs` optimizer batch can plan: a scalar Python UDF, a Python
UDTF or a scalar external UDF. For such a filtering side, `PartitionPruning`
now treats the standalone subquery as having no benefit. So it inserts the DPP
filter with `onlyInBroadcast = true`, as it does with
`spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnly=true` (the
default), and the planner uses the filter only when it can reuse a broadcast
for it.
Non-scalar Python UDFs, such as grouped aggregate Pandas UDFs, are not
affected. Their own physical operators run them, so a standalone DPP subquery
over them works.
### Why are the changes needed?
When `spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnly` is
`false` and DPP is estimated to be beneficial, `PartitionPruning` inserts a
`DynamicPruningSubquery` with `onlyInBroadcast = false`. If the planner cannot
reuse a broadcast for it, `PlanDynamicPruningFilters` (or
`PlanAdaptiveDynamicPruningFilters` with AQE) plans a subquery that evaluates
the join key over the filtering side, without the optimizer. That filtering
side is the copy which `PartitionPruning` took before the `Extract UDFs` batch,
and that batch rewrites only the main plan. So a Python UDF in the filtering
side or in its join key reaches execution as it is, and the query fails. For
example, in PySpark:
```python
spark.udf.register("pyudf", lambda s: s, "string")
spark.range(100).selectExpr("id AS date_id", "id % 10 AS store_id") \
.write.partitionBy("store_id").saveAsTable("fact")
spark.createDataFrame([(1, "NL"), (2, "NL"), (3, "DE")], "store_id long,
country string") \
.coalesce(1).write.saveAsTable("dim")
spark.conf.set("spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnly",
"false")
spark.sql("SELECT count(*) FROM fact f JOIN dim d ON f.store_id = d.store_id
"
"WHERE pyudf(d.country) = 'NL'").collect()
```
```
org.apache.spark.SparkException: [INTERNAL_ERROR] Cannot generate code for
expression: pyudf(input[1, string, true])#12 SQLSTATE: XX000
```
It fails with and without AQE, even when the join is a broadcast hash join.
The filtering side of the DPP subquery still has the Python UDF, while the
build side of the join has it extracted, so the planner cannot reuse the
broadcast and falls back to the standalone subquery. A deterministic Python
UDTF and a scalar external UDF (SPARK-58467, unreleased) fail the same way.
This has happened since Apache Spark 3.2.0
([v3.2.0](https://github.com/apache/spark/releases/tag/v3.2.0), 2021-10-06).
Before that, the `PartitionPruning` batch ran `OptimizeSubqueries`, which
optimized the filtering side of a DPP subquery with the whole optimizer,
including the Python UDF extraction. SPARK-36444 removed it from the batch. The
same failure reproduces on Apache Spark 3.5.9, 4.0.5, 4.1.4, 4.2.1 and 4.3.0
([v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0), 2026-10-27).
### Does this PR introduce _any_ user-facing change?
Yes. With
`spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnly=false`, a query
whose DPP filtering side has a scalar Python UDF or a deterministic Python UDTF
no longer fails with `INTERNAL_ERROR`. As with the default
`reuseBroadcastOnly=true`, DPP uses such a filtering side only when the planner
can reuse a broadcast for it.
### How was this patch tested?
Pass the CIs with the newly added test cases in
`DynamicPartitionPruningV1Suite`, which run with and without AQE.
- With a Python UDF in the filtering side, a Python UDF in its join key, and
a deterministic Python UDTF as the filtering side, the query returns the
expected rows, and DPP plans no standalone subquery. All of them fail with
`INTERNAL_ERROR` without this change.
- With a scalar external UDF in the filtering side, `PartitionPruning`
inserts the DPP filter with `onlyInBroadcast = true`. It is `false` without
this change.
### 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]