[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.
This commit is contained in:
morrySnow
2022-08-31 19:52:03 +08:00
committed by GitHub
parent 1a198b3777
commit 57051d3591

View File

@ -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));
}