This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.1 by this push:
new 37f3c8f0c7d [fix](nereids) fix fold constant return wrong scale of
datetime type (#50142) (#50716)
37f3c8f0c7d is described below
commit 37f3c8f0c7d2ee881c55ec9ba5b1ecfcfe6f3c62
Author: 924060929 <[email protected]>
AuthorDate: Fri May 9 11:12:07 2025 +0800
[fix](nereids) fix fold constant return wrong scale of datetime type
(#50142) (#50716)
cherry pick from #50142
---
.../expression/rules/FoldConstantRuleOnFE.java | 20 ++++++++++++++------
.../doris/nereids/util/TypeCoercionUtils.java | 22 ++++++++++++++++++++++
.../nereids/rules/expression/FoldConstantTest.java | 10 ++++++++++
3 files changed, 46 insertions(+), 6 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java
index 0f1e3edb2a9..d168e899a2e 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java
@@ -89,6 +89,7 @@ import org.apache.doris.nereids.types.BooleanType;
import org.apache.doris.nereids.types.DataType;
import org.apache.doris.nereids.types.coercion.DateLikeType;
import org.apache.doris.nereids.util.ExpressionUtils;
+import org.apache.doris.nereids.util.TypeCoercionUtils;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.GlobalVariable;
import org.apache.doris.thrift.TUniqueId;
@@ -521,6 +522,7 @@ public class FoldConstantRuleOnFE extends
AbstractExpressionRewriteRule
@Override
public Expression visitCaseWhen(CaseWhen caseWhen,
ExpressionRewriteContext context) {
+ CaseWhen originCaseWhen = caseWhen;
caseWhen = rewriteChildren(caseWhen, context);
Expression newDefault = null;
boolean foundNewDefault = false;
@@ -546,7 +548,10 @@ public class FoldConstantRuleOnFE extends
AbstractExpressionRewriteRule
defaultResult = newDefault;
}
if (whenClauses.isEmpty()) {
- return defaultResult == null ? new
NullLiteral(caseWhen.getDataType()) : defaultResult;
+ return TypeCoercionUtils.ensureSameResultType(
+ originCaseWhen, defaultResult == null ? new
NullLiteral(caseWhen.getDataType()) : defaultResult,
+ context
+ );
}
if (defaultResult == null) {
if (caseWhen.getDataType().isNullType()) {
@@ -554,21 +559,24 @@ public class FoldConstantRuleOnFE extends
AbstractExpressionRewriteRule
// it's safe to return null literal here
return new NullLiteral();
} else {
- return new CaseWhen(whenClauses);
+ return TypeCoercionUtils.ensureSameResultType(originCaseWhen,
new CaseWhen(whenClauses), context);
}
}
- return new CaseWhen(whenClauses, defaultResult);
+ return TypeCoercionUtils.ensureSameResultType(
+ originCaseWhen, new CaseWhen(whenClauses, defaultResult),
context
+ );
}
@Override
public Expression visitIf(If ifExpr, ExpressionRewriteContext context) {
+ If originIf = ifExpr;
ifExpr = rewriteChildren(ifExpr, context);
if (ifExpr.child(0) instanceof NullLiteral ||
ifExpr.child(0).equals(BooleanLiteral.FALSE)) {
- return ifExpr.child(2);
+ return TypeCoercionUtils.ensureSameResultType(originIf,
ifExpr.child(2), context);
} else if (ifExpr.child(0).equals(BooleanLiteral.TRUE)) {
- return ifExpr.child(1);
+ return TypeCoercionUtils.ensureSameResultType(originIf,
ifExpr.child(1), context);
}
- return ifExpr;
+ return TypeCoercionUtils.ensureSameResultType(originIf, ifExpr,
context);
}
@Override
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/TypeCoercionUtils.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/TypeCoercionUtils.java
index 6c4cafa2517..e6620cb0111 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/TypeCoercionUtils.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/TypeCoercionUtils.java
@@ -24,6 +24,8 @@ import org.apache.doris.common.Config;
import org.apache.doris.nereids.analyzer.ComplexDataType;
import org.apache.doris.nereids.annotation.Developing;
import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext;
+import org.apache.doris.nereids.rules.expression.rules.FoldConstantRuleOnFE;
import org.apache.doris.nereids.trees.expressions.Add;
import org.apache.doris.nereids.trees.expressions.BinaryArithmetic;
import org.apache.doris.nereids.trees.expressions.BinaryOperator;
@@ -151,6 +153,26 @@ public class TypeCoercionUtils {
private static final Logger LOG =
LogManager.getLogger(TypeCoercionUtils.class);
+ /**
+ * ensure the result's data type equals to the originExpr's dataType,
+ * ATTN: this method usually used in fold constant rule
+ */
+ public static Expression ensureSameResultType(
+ Expression originExpr, Expression result, ExpressionRewriteContext
context) {
+ DataType originDataType = originExpr.getDataType();
+ DataType newDataType = result.getDataType();
+ if (originDataType.equals(newDataType)) {
+ return result;
+ }
+ // backend can direct use all string like type without cast
+ if (originDataType.isStringLikeType() &&
newDataType.isStringLikeType()) {
+ return result;
+ }
+ return FoldConstantRuleOnFE.PATTERN_MATCH_INSTANCE.visitCast(
+ new Cast(result, originDataType), context
+ );
+ }
+
/**
* Return Optional.empty() if we cannot do implicit cast.
*/
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java
index 3e136a598da..54213851702 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java
@@ -23,6 +23,7 @@ import org.apache.doris.nereids.analyzer.UnboundRelation;
import org.apache.doris.nereids.parser.NereidsParser;
import org.apache.doris.nereids.rules.analysis.ExpressionAnalyzer;
import org.apache.doris.nereids.rules.expression.rules.FoldConstantRuleOnFE;
+import org.apache.doris.nereids.trees.expressions.CaseWhen;
import org.apache.doris.nereids.trees.expressions.Cast;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.GreaterThan;
@@ -115,6 +116,15 @@ class FoldConstantTest extends ExpressionRewriteTestHelper
{
assertRewriteAfterTypeCoercion("case when null = 2 then 1 else 4 end",
"4");
assertRewriteAfterTypeCoercion("case when null = 2 then 1 end",
"null");
assertRewriteAfterTypeCoercion("case when TA = TB then 1 when TC is
null then 2 end", "CASE WHEN (TA = TB) THEN 1 WHEN TC IS NULL THEN 2 END");
+
+ // make sure the case when return datetime(6)
+ Expression analyzedCaseWhen = ExpressionAnalyzer.analyzeFunction(null,
null, PARSER.parseExpression(
+ "case when true then cast('2025-04-17' as datetime(0)) else
cast('2025-04-18 01:02:03.123456' as datetime(6)) end"));
+ Assertions.assertEquals(DateTimeV2Type.of(6),
analyzedCaseWhen.getDataType());
+ Assertions.assertEquals(DateTimeV2Type.of(6), ((CaseWhen)
analyzedCaseWhen).getWhenClauses().get(0).getResult().getDataType());
+ Assertions.assertEquals(DateTimeV2Type.of(6), ((CaseWhen)
analyzedCaseWhen).getDefaultValue().get().getDataType());
+ Expression foldCaseWhen = executor.rewrite(analyzedCaseWhen, context);
+ Assertions.assertEquals(new DateTimeV2Literal(DateTimeV2Type.of(6),
"2025-04-17"), foldCaseWhen);
}
@Test
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]