viirya opened a new pull request, #60469:
URL: https://github.com/apache/spark/pull/60469

   ### What changes were proposed in this pull request?
   
   `ExprUtils.isTotalOperator` decides whether an expression can raise an error 
by itself. It was one hand-maintained pattern match over expression classes. 
This PR moves that decision to the expressions:
   
   - `Expression.isTotal` is the declaration. It is false by default, so an 
expression that says nothing is only left out of some optimizations.
   - An expression that is always total mixes in the existing marker trait 
`NoThrow`, which now extends `Expression` and fixes `isTotal` to true. 
`NoThrow` moves from `complexTypeCreator.scala` to `Expression.scala`, and its 
scaladoc states the contract that mixing it in promises.
   - The expressions that are total only for some input types or in some eval 
modes override `isTotal`: `Cast` (through `Cast.isTotalCast`), `Add`, 
`Subtract`, `Multiply`, `Divide`, `Remainder`, `IntegralDivide`, `Pmod`, 
`UnaryMinus` and `Abs`. Each result is a union of independent conditions, so it 
no longer depends on the order of `case` clauses.
   - `ExprUtils.isTotalOperator(e)` is now `e.isTotal`. None of its callers 
changes, nor do the callers of `ExprUtils.canEvaluateUnconditionally`.
   
   The classes that now mix in `NoThrow` are the ones the list accepted 
whatever their state: `GetStructField`, `GetArrayStructFields`, `GetMapValue`, 
`Collate`, `And`, `Or`, `Not`, `BinaryComparison`, `IsNull`, `IsNotNull`, 
`IsNaN`, `NullIf`, `Coalesce`, `In`, `InSet`, `Size`, `StartsWith`, `EndsWith`, 
`Contains`, `Length`, `OctetLength`, `Substring`, `GetDateField` and 
`GetTimeField`. `CreateArray` and `CreateNamedStruct` already did.
   
   `ConstantFolding` also reads `NoThrow`, to fold `size(array(...))` without 
evaluating the elements. So that it folds exactly what it did, it now names 
`CreateArray` and `CreateNamedStruct`, the two expressions that were marked 
before this PR.
   
   ### Why are the changes needed?
   
   The list was hard to keep right:
   
   - It lives in `ExprUtils`, far from the expressions it describes. An 
expression that is missing from it is silently left out of some optimizations, 
and nothing ties a change in how an expression evaluates to its entry in the 
list.
   - The cases for `Cast` and the arithmetic expressions depend on the input 
types and the eval mode, and their results relied on the order of the `case` 
clauses, e.g. the check for a decimal of a negative scale had to come after the 
numeric up-casts and before the checks by eval mode.
   
   With the declaration on the expression, the condition sits next to the 
`eval` it describes, and the conditional ones read as one boolean expression 
each.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. The set of expressions `ExprUtils.isTotalOperator` accepts is the same 
as before in every eval mode, and `ConstantFolding` folds the same expressions.
   
   ### How was this patch tested?
   
   New tests in `ExprUtilsSuite`:
   
   - an instance of every expression that is always total is marked `NoThrow` 
and accepted, and expressions that declare nothing are not;
   - the expressions that override `isTotal` are accepted in exactly the 
expected eval modes, for representative input types;
   - a scan of every expression class of catalyst checks the classes that are 
marked `NoThrow` and the ones that override `isTotal` against explicit lists, 
so a class that becomes total by extending another one is noticed.
   
   A new test in `ConstantFoldingSuite` checks that the size of an array is 
still folded only over `CreateArray` and `CreateNamedStruct`.
   
   To check that no result changes, I also compared the new `isTotalOperator` 
with a verbatim copy of the old one, in a suite that is not part of this PR: 
over 29,403 casts (every pair of 99 atomic and nested types in the three eval 
modes), 68,445 binary arithmetic expressions, 396 unary ones, and by class over 
all 888 expression classes of catalyst. All results are equal.
   
   The existing tests of `ExprUtilsSuite` and of the optimizer rules that call 
it pass unchanged.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (claude-fable-5-2)
   
   This pull request and its description were written by Isaac.
   


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