From 57051d3591c3c832e8264f5c4a6b47aa2d63b2ec Mon Sep 17 00:00:00 2001 From: morrySnow <101034200+morrySnow@users.noreply.github.com> Date: Wed, 31 Aug 2022 19:52:03 +0800 Subject: [PATCH] [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. --- .../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)); }