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

   ### What changes were proposed in this pull request?
   
   This PR fixes two cases where `ExtractPythonUDFs.scala` makes Spark evaluate 
one Python UDF call twice.
   
   1. **A nondeterministic call that an operator holds twice.** 
`ExtractPythonUDFs` collects the UDFs of an operator into an `ExpressionSet`, 
which keeps every nondeterministic expression, including the same one twice. So 
when a `Column` with a nondeterministic Python UDF is selected twice, the 
evaluation node holds the same call (one `resultId`) twice, evaluates it twice 
per row, and both columns read only the second result. This PR drops exact 
duplicates with `.distinct`. Separate calls have different `resultId`s and stay 
apart as before.
   
       ```scala
       val call = nondeterministicPythonUDF(col("a"))
       df.select(call.as("c"), call.as("d"))
       ```
   
   2. **Equal calls over an aggregate function.** 
`ExtractPythonUDFFromAggregate` creates a new `agg` alias for each occurrence 
of an aggregate function or a grouping expression under a Python UDF. So in 
`SELECT pyudf(sum(v)), pyudf(sum(v)) FROM t`, the two calls read different 
attributes (`pyudf(agg#1)`, `pyudf(agg#2)`), SPARK-33303 cannot deduplicate 
them, and the UDF runs twice per group. This PR shares the alias of equal 
deterministic expressions through a map keyed by `canonicalized`, and adds each 
alias to the aggregate once. Nondeterministic expressions keep an alias per 
occurrence as before.
   
   ### Why are the changes needed?
   
   To evaluate each Python UDF call once. Before this PR, a nondeterministic 
Python UDF selected twice runs twice per row, and equal Python UDF calls over 
an aggregate function run once per occurrence for each group. Besides the cost 
of the extra Python execution, a UDF with side effects runs more often than the 
query asks.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A nondeterministic Python UDF call that is selected twice now runs once 
per row, and both columns read that result instead of the second of two 
evaluations. Equal Python UDF calls over an equal aggregate function or 
grouping expression now run once per group. Query results do not change for 
deterministic UDFs.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test cases in `ExtractPythonUDFsSuite`.
   
   ### 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