dongjoon-hyun opened a new pull request, #60696: URL: https://github.com/apache/spark/pull/60696
### What changes were proposed in this pull request? This PR fixes `PartitionPruning` so that it does not insert a dynamic partition pruning (DPP) filter on a pruning key that has a UDF which only the "Extract UDFs" batch can plan: a scalar Python UDF or an external UDF (`ExternalUserDefinedFunction`). - `getFilterableTableScan` now returns no scan when the pruning key, followed down to the scan (`resExp`), has such a UDF. Following it down also covers a key that is an alias of such a UDF in a subquery. - `needsUDFExtraction` now takes the expressions to check, so that the existing filtering side check and the new pruning key check share it. ### Why are the changes needed? `PartitionPruning` inserts `Filter(DynamicPruningSubquery(pruningKey, ...), factScan)` when the pruning key refers only to partition columns. It checks the filtering side for such UDFs (SPARK-61824), but not the pruning key. For example: ```sql SELECT f.date_id, f.store_id FROM fact_sk f JOIN dim_store s ON pyudf(f.store_id) = s.store_id WHERE s.country = 'NL' ``` The pruning key is `pyudf(store_id)`. `CleanupDynamicPruningFilters` keeps the DPP filter, as it is directly over the scan. Then the "Extract UDFs" batch, which runs after both rules, evaluates the UDF of the DPP filter in an evaluation node between the filter and the scan, and the UDF of the join key in another one: ``` BroadcastHashJoin [cast(pythonUDF0#5264 as int)], [store_id#5256], Inner, BuildRight, false, false :- BatchEvalPython [pyudf(cast(store_id#5255 as string))], [date_id#5252, store_id#5255], [pythonUDF0#5264] : +- Project [date_id#5252, store_id#5255] : +- Filter dynamicpruningexpression(cast(pythonUDF0#5263 as int) IN dynamicpruning#5262) : : +- SubqueryBroadcast dynamicpruning#5262, [0], [store_id#5256], [id=#399] : +- BatchEvalPython [pyudf(cast(store_id#5255 as string))], [date_id#5252, store_id#5255], [pythonUDF0#5263] : +- ColumnarToRow : +- FileScan parquet spark_catalog.default.fact_sk[date_id#5252,store_id#5255] ..., PartitionFilters: [], ... +- ReusedExchange [store_id#5256], BroadcastExchange ... ``` As a result: - The V1 and Hive scans and the V2 runtime filters no longer see the DPP filter, so it prunes no partition. - The DPP filter runs as a row-level `FilterExec`. - The UDF is evaluated in two evaluation nodes. - Without a broadcast to reuse, and with `spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnly=false`, a standalone DPP subquery reads the filtering side again just for that row-level filter. The query results are correct. This has been so since DPP was added in Apache Spark 3.0.0. The behavior above was confirmed with PySpark on Apache Spark 4.0.5, 4.1.4, 4.2.1 and 4.3.0. With this PR, the plan has no DPP filter and one evaluation node. ### Does this PR introduce _any_ user-facing change? No in terms of query results. A query that joins on a partition column wrapped in a scalar Python UDF or an external UDF no longer gets a DPP filter that prunes no partition, so the UDF is evaluated once instead of twice, and no standalone DPP subquery runs for it. ### How was this patch tested? Pass the CIs with the newly added test cases in `DynamicPartitionPruningSuiteBase`, which run in the V1, V2 and Hive scan suites with and without AQE. - `SPARK-62052: no DPP filter on a pruning key with a Python UDF` checks a pruning key with a Python UDF, used directly in the join condition and through an alias in a subquery. It checks that no DPP filter is left above the scan and that the plan has one evaluation node. - `SPARK-62052: no DPP filter on a pruning key with an external UDF` checks that the optimized plan has no `DynamicPruningSubquery`. Both fail without the fix in `DynamicPartitionPruningV1SuiteAEOff`, `DynamicPartitionPruningV1SuiteAEOn`, `DynamicPartitionPruningV2SuiteAEOff`, `DynamicPartitionPruningV2FilterSuiteAEOff` and `DynamicPartitionPruningV2CatalystFilterSuiteAEOn`. ``` $ build/sbt 'sql/testOnly org.apache.spark.sql.DynamicPartitionPruning* org.apache.spark.sql.InjectRuntimeFilterSuite' $ build/sbt -Phive 'hive/testOnly org.apache.spark.sql.hive.DynamicPartitionPruningHiveScanSuite*' ``` ### 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]
