dongjoon-hyun opened a new pull request, #60437:
URL: https://github.com/apache/spark/pull/60437
### What changes were proposed in this pull request?
This PR fixes evaluating a Python UDF twice when a filter over a scan uses
the result of the UDF.
SPARK-59693 made Python UDFs expensive. With
`spark.sql.optimizer.avoidDoubleFilterEval=true` (the default),
`PushPredicateThroughNonJoin` keeps a filter on a Python UDF alias above its
`Project`. However, the rules of the batch `Early Filter and Projection
Push-Down` match the projections and the filters over a relation with
`ScanOperation` or `PhysicalOperation`, and rebuild the plan from what they
match. `OperationHelper.collectProjectsAndFilters` pushes the filter below the
`Project` by inlining the alias, while the `Project` still computes it:
```
Filter (isnotnull(u) AND u) Project [udf(a) AS u, b]
+- Project [udf(a) AS u, b] => +- Filter (isnotnull(udf(a)) AND
udf(a))
+- Relation [a, b] +- Relation [a, b]
```
The check for this, `CollapseProject.canCollapseExpressions`, treats a
Python UDF with cheap arguments as cheap, because `ExtractPythonUDFs` evaluates
identical Python UDFs once within one operator. Here the copies are in two
operators, the `Filter` and the `Project`, so `ExtractPythonUDFs` evaluates the
UDF twice.
This PR adds a third condition for pushing a filter through the collected
projects: the filter does not use the result of a Python UDF of the collected
projects. The filter then stays above the `Project`, where the optimizer put it.
The rules that rebuild the plan this way run before `ExtractPythonUDFs`:
- `V2ScanRelationPushDown` (`ScanOperation`), for every DSv2 scan.
- `PruneFileSourcePartitions` (`PhysicalOperation`), for a partitioned V1
file source table with a partition filter.
- `SchemaPruning` (`ScanOperation`), for a V1 file source when nested
columns are pruned.
The planning strategies use the same patterns after `ExtractPythonUDFs`, so
their projections and filters have no Python UDF.
No source pushdown is lost, because a filter on a Python UDF is never pushed
into a source. It has no translation into a source filter, and the partition
filter checks of `PushDownUtils`, `FileScanBuilder` and
`PruneFileSourcePartitions` exclude it.
No configuration is checked. With
`spark.sql.optimizer.avoidDoubleFilterEval=false` or
`spark.sql.optimizer.avoidDoubleFilterEvalForPythonUDF=false`,
`PushPredicateThroughNonJoin` already pushes the filter below the `Project`
before these rules run, so their plans do not change.
### Why are the changes needed?
Without this change, SPARK-59693 does not prevent the double evaluation over
DSv2 scans and the V1 scans above. For example, the query of the new test reads
a Parquet file with DSv2:
```scala
spark.read.parquet(path).select(batchedPythonUDF($"a").as("u"),
$"b").where($"u")
```
Before this PR, the optimized plan evaluates the UDF twice:
```
Project [pythonUDF0#24 AS u#20, b#12L]
+- BatchEvalPython [dummyUDF(a#11L)#19], [b#12L], [pythonUDF0#24]
+- Project [a#11L, b#12L]
+- Filter (isnotnull(pythonUDF0#23) AND pythonUDF0#23)
+- BatchEvalPython [dummyUDF(a#11L)#19], [a#11L, b#12L],
[pythonUDF0#23]
+- RelationV2[a#11L, b#12L] parquet file:/...
```
After this PR, it evaluates the UDF once:
```
Project [pythonUDF0#22 AS u#19, b#12L]
+- Filter (isnotnull(pythonUDF0#22) AND pythonUDF0#22)
+- BatchEvalPython [dummyUDF(a#11L)#18], [b#12L], [pythonUDF0#22]
+- RelationV2[a#11L, b#12L] parquet file:/...
```
### Does this PR introduce _any_ user-facing change?
No change in query results. A Python UDF whose result is used by a filter
over a DSv2 scan, or over the V1 scans above, is evaluated once instead of
twice.
In Spark [4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0)
(2026-10-27), `PushPredicateThroughNonJoin` itself pushes such a filter below
the `Project`, so the UDF is evaluated twice over any relation. SPARK-59693,
which is only in the unreleased `master` and `branch-4.x`, keeps the filter
above the `Project` in the optimizer, and this PR keeps it there over these
scans too.
### How was this patch tested?
New test in `ExtractPythonUDFsSuite`. A DSv2 Parquet scan
(`V2ScanRelationPushDown`) and a partitioned V1 table with a partition filter
(`PruneFileSourcePartitions`) each evaluate the UDF in one `BatchEvalPython`.
Without this change, both have two.
With this change, the `A filter and a projection above` check of
`ArrowColumnarPythonUDFSuite` runs a filter above one columnar evaluation
without pass-through columns, as intended. Before, it ran two evaluations.
Existing tests:
- `catalyst/testOnly *PhysicalOperationSuite`
- `sql/testOnly *ExtractPythonUDFsSuite *ArrowColumnarPythonUDFSuite
*ArrowColumnarPythonUDFPipelinedSuite
org.apache.spark.sql.connector.DataSourceV2Suite
*V2ScanRelationPushDownSubquerySuite *PushDownUtilsSuite
*DataSourceV2StrategySuite *PruneFileSourcePartitionsSuite
*ParquetV1SchemaPruningSuite *ParquetV2SchemaPruningSuite *CTEInlineSuiteAEOff
*CTEInlineSuiteAEOn *StreamingV2ScanPushDownSuite *BatchEvalPythonExecSuite
org.apache.spark.sql.execution.python.PythonUDFSuite *InjectRuntimeFilterSuite`
### 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]