This is an automated email from the ASF dual-hosted git repository.

huajianlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 57051d3591 [fix](Nereids)cast StringType to DateType failed when bind 
TimestampArithmetic function (#12198)
57051d3591 is described below

commit 57051d3591c3c832e8264f5c4a6b47aa2d63b2ec
Author: morrySnow <[email protected]>
AuthorDate: Wed Aug 31 19:52:03 2022 +0800

    [fix](Nereids)cast StringType to DateType failed when bind 
TimestampArithmetic function (#12198)
    
    When bind TimestampArithmetic, we always want to cast left child to 
DateTimeType. But sometimes, we need to cast it to DateType, this PR fix this 
problem.
---
 .../doris/nereids/rules/analysis/BindFunction.java     | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.java
index 384f57f78c..df2174caf3 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindFunction.java
@@ -35,6 +35,7 @@ import 
org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewri
 import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
 import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
 import org.apache.doris.nereids.types.DateTimeType;
+import org.apache.doris.nereids.types.DateType;
 import org.apache.doris.nereids.types.IntegerType;
 
 import com.google.common.collect.ImmutableList;
@@ -143,7 +144,7 @@ public class BindFunction implements AnalysisRuleFactory {
 
         @Override
         public Expression visitTimestampArithmetic(TimestampArithmetic 
arithmetic, Void context) {
-            String funcOpName = null;
+            String funcOpName;
             if (arithmetic.getFuncName() == null) {
                 funcOpName = String.format("%sS_%s", arithmetic.getTimeUnit(),
                         (arithmetic.getOp() == Operator.ADD) ? "ADD" : "SUB");
@@ -154,11 +155,18 @@ public class BindFunction implements AnalysisRuleFactory {
             Expression left = arithmetic.left();
             Expression right = arithmetic.right();
 
-            if (!arithmetic.left().getDataType().isDateType()) {
-                left = arithmetic.left().castTo(DateTimeType.INSTANCE);
+            if (!left.getDataType().isDateType()) {
+                try {
+                    left = left.castTo(DateTimeType.INSTANCE);
+                } catch (Exception e) {
+                    // ignore
+                }
+                if (!left.getDataType().isDateType() && 
arithmetic.getTimeUnit().isDateTimeUnit()) {
+                    left = arithmetic.left().castTo(DateType.INSTANCE);
+                }
             }
-            if (!arithmetic.right().getDataType().isIntType()) {
-                right = arithmetic.right().castTo(IntegerType.INSTANCE);
+            if (!right.getDataType().isIntType()) {
+                right = right.castTo(IntegerType.INSTANCE);
             }
             return 
arithmetic.withFuncName(funcOpName).withChildren(ImmutableList.of(left, right));
         }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to