924060929 commented on PR #68579:
URL: https://github.com/apache/doris/pull/68579#issuecomment-5908763223

   The existing rewrite-time constant folding is too late for functions whose 
signatures depend on argument values. For example, `now(1 + 2)` needs the 
precision value before determining its return type. This does justify a 
preparation step during binding, before signature computation and type coercion.
   
   Could we express this as a default lifecycle method in `ExpressionTrait`, 
alongside the existing legality hooks, instead of introducing the separate 
`FoldLiteralArguments` interface and an `instanceof` dispatch in 
`TypeCoercionUtils`? For example, `prepareBeforeTypeCoercion()` could return 
the prepared expression and do nothing by default. Functions that need it would 
override it, reusing a shared argument-folding helper. Since this method can 
replace children/rebuild the expression, a `prepare...` name would keep it 
distinct from the validation-only `checkLegality...` methods.
   
   The intended order would be:
   
   ```
   bind arguments and function
   -> prepareBeforeTypeCoercion
   -> checkLegalityBeforeTypeCoercion
   -> compute signature / expected input types
   -> insert casts
   -> subsequent rewrites and legality checks
   ```
   
   The preparation must happen before `expectedInputTypes()` computes the 
signature, not just before casts are inserted. It should also be limited to 
arguments whose values are actually needed at that point: precision arguments 
for `now`/`utc_timestamp`/`utc_time`, and the string forms of `date_trunc` that 
need unit values to identify argument roles.
   
   For functions such as `sha2`, `topn`, `tokenize`, and the AI functions, 
needing to validate a constant argument's value does not by itself mean 
signature computation needs that value. Their checks should be placed according 
to their individual requirements rather than bringing all of them into early 
folding. This is not a mechanical move of every check to 
`checkLegalityAfterRewrite`: checks that must report an error before the parent 
expression is folded away need an earlier validation point after argument 
preparation/folding. For example, null propagation can remove a `tokenize` 
expression before a final rewrite-time check sees its properties argument.
   
   This would make the abstraction describe the expression lifecycle and keep 
the early-folding requirement separate from ordinary constant/value validation.
   


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