mrhhsg commented on code in PR #68579:
URL: https://github.com/apache/doris/pull/68579#discussion_r4122508385
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/DateTrunc.java:
##########
@@ -62,6 +64,21 @@ private DateTrunc(ScalarFunctionParams functionParams) {
super(functionParams);
}
+ @Override
+ public boolean needFoldToLiteral(int index) {
+ // Fold a string argument only when it may be the time unit, i.e. the
other argument is not a literal
+ // or is a string literal that is not a time unit. A string date value
next to a literal time unit is
+ // left unfolded, because folding it would change the derived return
type.
+ if (!getArgument(index).getDataType().isStringLikeType()) {
+ return false;
+ }
+ Expression other = getArgument(1 - index);
+ if (other instanceof StringLikeLiteral) {
+ return !LEGAL_TIME_UNIT.contains(((StringLikeLiteral)
other).getStringValue().toLowerCase());
Review Comment:
Fixed in 72a9c64. `FoldLiteralArguments` gained a default
`acceptFoldedLiteral(index, folded)` hook (true by default), and date_trunc now
decides by the folded value: an argument is replaced only when it folds to a
legal time unit, or when the other argument is date-typed (then an illegal unit
gets the "only support argument is ..." error). A string date value is never
folded, and `needFoldToLiteral` still skips an argument next to a literal time
unit, so every shape the merge base accepted keeps its acceptance and type.
- `date_trunc(DATE '2024-03-15', concat('mon','th'))` returns `2024-03-01`
(DATEV2, same as the literal unit). `date_trunc(concat('ye','ar'), TIMESTAMP
'...')` also works.
- With `concat('2024-01-01 01:02:03+08:00','')` as the date, only the unit
is folded in both argument orders, and the CTAS type is `datetime(6)` for the
literal `'month'` and for both `concat('mon','th')` orders (no TIMESTAMPTZ).
Covered by new cases in `FoldLiteralArgumentsTest` (typed DATE/TIMESTAMP,
both-foldable in both orders with type equality, illegal unit next to DATE) and
regression `date_trunc_typed_date` / `date_trunc_both_foldable_type`.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/OrthogonalBitmapExprCalculate.java:
##########
@@ -64,6 +66,20 @@ private
OrthogonalBitmapExprCalculate(AggregateFunctionParams functionParams) {
super(functionParams);
}
+ @Override
+ public boolean needFoldToLiteral(int index) {
+ // the calculation expression
+ return index == 2;
+ }
Review Comment:
Fixed in 72a9c64. Both `OrthogonalBitmapExprCalculate` and
`OrthogonalBitmapExprCalculateCount` `withDistinctAndChildren` now only require
a `CharacterType` formula, so the pre-coercion `StringLiteral` produced by
folding `concat(cast('1' as string), cast('|2' as string))` can be rebuilt. The
`StringLikeLiteral` check in `checkLegalityBeforeTypeCoercion` still rejects
non-literal formulas. BE resolves these functions by name and reads all string
types as ColumnString. Added STRING-formula cases for both variants to
`FoldLiteralArgumentsTest` and to regression
(`orthogonal_bitmap_expr_calculate_string` / `_count_string`, results `1,2` /
`2`, same as the VARCHAR formula).
--
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]