peter-toth commented on code in PR #60437:
URL: https://github.com/apache/spark/pull/60437#discussion_r4561789353


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/patterns.scala:
##########
@@ -74,8 +74,14 @@ trait OperationHelper extends AliasHelper with 
PredicateHelper {
         //   1) no Project collected so far or the collected Projects are all 
deterministic
         //   2) this filter does not repeat any expensive expressions from the 
collected
         //      projects.
+        //   3) this filter does not use the result of a Python UDF of the 
collected projects.
+        //      The projects still evaluate the UDF, and `ExtractPythonUDFs` 
shares an evaluation
+        //      only within one operator, so the filter would evaluate it a 
second time.
+        val usesPythonUDF = condition.references.exists { attr =>

Review Comment:
   **Finding 1.** The description lists `V2ScanRelationPushDown`, 
`PruneFileSourcePartitions` and `SchemaPruning` as the rules that rebuild the 
plan from these patterns before `ExtractPythonUDFs`. Two more do, and the 
change applies to them too:
   
   - `PushVariantIntoScan` (`PhysicalOperation`), for a V1 Parquet relation 
with a variant extraction. `rewritePlan` rebuilds `Project(Filter(relation))`.
   - `PruneHiveTablePartitions` (`PhysicalOperation`), for a partitioned Hive 
table with a partition filter. It rebuilds `Project(projections, 
Filter(filters, newRelation))`, like `PruneFileSourcePartitions`. Its partition 
filter check excludes Python UDFs too, so it also belongs in the sentence about 
`PushDownUtils`, `FileScanBuilder` and `PruneFileSourcePartitions`.
   
   `BatchEvalPython` count, base and head:
   
   | Shape | Base | Head |
   |---|---|---|
   | V1 Parquet, `variant_get(v, '$', 'int')` selected, filter on the UDF | 2 | 
1 |
   | Hive `TEXTFILE` table partitioned by `p`, `WHERE u AND p = 1` | 2 | 1, 
still `Pruned Partitions: [(p=1)]` |
   
   `PushdownPredicatesAndPruneColumnsForCTEDef` also matches a 
`PhysicalOperation`, over a `CTERelationRef`. It does not rebuild a scan, but 
its plan changes too. It pushes the OR of every reference's predicates into the 
CTE definition. I ran `WITH c AS MATERIALIZED (SELECT a, b FROM ct) SELECT b 
FROM (SELECT pyudf(a) AS u, b FROM c) WHERE u UNION ALL SELECT b FROM c WHERE b 
> 5`:
   
   - Base pushes `(isnotnull(pythonUDF0) AND pythonUDF0) OR (isnotnull(b) AND 
(b > 5))` into the definition, over a `BatchEvalPython` of its own.
   - The head pushes nothing into the definition. The UDF reference now has no 
predicate, so the other reference's `b > 5` is not pushed either.
   
   That looks like the direction this PR wants, since the definition no longer 
evaluates the UDF. It is still a plan change, so please mention it next to the 
other user-facing changes.
   



-- 
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