Weijun-H opened a new issue, #27280:
URL: https://github.com/apache/datafusion/issues/27280

   ### Is your feature request related to a problem or challenge?
   
   `CASE WHEN f(x) = 'a' THEN 'b' ELSE f(x) END` evaluates `f(x)` for every row 
in the first WHEN, then again for the ELSE rows. For an expensive `f` the 
second call can dominate.
   
   For plans that go through the logical optimizer, `CommonSubexprEliminate` 
can remove the duplicate by moving `f(x)` into a lower projection: with an 
immutable UDF `f`, `EXPLAIN SELECT CASE WHEN f(value) = 1 THEN 100 ELSE 
f(value) END FROM t` shows `Projection: f(t.value) AS __common_expr_1` below 
the CASE (not extracted when `f` is volatile). An engine that builds physical 
plans directly never runs that rule. It also may not be able to use a lower 
projection: that evaluates `f(x)` before the columns preceding the CASE, so 
when those columns and `f` fail on different rows, a different error surfaces 
first. Spark-compatible engines need to keep the original order.
   
   ### Describe the solution you'd like
   
   An opt-in on `CaseExpr`: the caller states that ELSE is the same call as the 
operand evaluated first inside the first WHEN, and `CaseExpr` evaluates it 
once, at that position, and reuses the array for the ELSE rows. The caller 
supplies the evaluation-order rules, since which child a `PhysicalExpr` 
evaluates first is not part of the trait contract.
   
   ### Describe alternatives you've considered
   
   - A lower `ProjectionExec` in the producer: changes which error surfaces 
first, as above.
   - Automatic detection inside `CaseExpr`: needs DataFusion to know evaluation 
order for every expression on the path. A built-in policy for 
`BinaryExpr`/`NotExpr`/`CastExpr` could be a follow-up.
   


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