From 98c3cb825fee0767002d24d0ea2d07a66f886d52 Mon Sep 17 00:00:00 2001 From: morrySnow <101034200+morrySnow@users.noreply.github.com> Date: Thu, 22 Feb 2024 14:08:32 +0800 Subject: [PATCH] [fix](Nereids) simplify airthmetic should not change return type (#31237) --- .../rules/SimplifyArithmeticRule.java | 3 +- .../executable/NumericArithmetic.java | 8 ++-- .../test_simplify_arithmetic.out | 3 ++ .../regression/action/ExplainAction.groovy | 7 +++- .../test_simplify_arithmetic.groovy | 39 +++++++++++++++++++ 5 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 regression-test/data/nereids_rules_p0/expression/simplify_arithmetic/test_simplify_arithmetic.out create mode 100644 regression-test/suites/nereids_rules_p0/expression/simplify_arithmetic/test_simplify_arithmetic.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyArithmeticRule.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyArithmeticRule.java index 24f96e9105..fc7431a999 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyArithmeticRule.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyArithmeticRule.java @@ -25,6 +25,7 @@ import org.apache.doris.nereids.trees.expressions.Divide; import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.expressions.Multiply; import org.apache.doris.nereids.trees.expressions.Subtract; +import org.apache.doris.nereids.util.TypeCoercionUtils; import org.apache.doris.nereids.util.TypeUtils; import com.google.common.collect.Lists; @@ -119,7 +120,7 @@ public class SimplifyArithmeticRule extends AbstractExpressionRewriteRule { : Operand.of(true, getAddOrMultiply(isAddOrSub, x, y))); if (result.isPresent()) { - return result.get().expression; + return TypeCoercionUtils.castIfNotSameType(result.get().expression, arithmetic.getDataType()); } else { return arithmetic; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java index 0aad284982..814fe04dfa 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java @@ -161,25 +161,25 @@ public class NumericArithmetic { return new LargeIntLiteral(result); } - @ExecFunction(name = "add", argTypes = {"LARGEINT", "TINYINT"}, returnType = "BIGINT") + @ExecFunction(name = "add", argTypes = {"LARGEINT", "TINYINT"}, returnType = "LARGEINT") public static Expression addLargeIntTinyInt(LargeIntLiteral first, TinyIntLiteral second) { BigInteger result = first.getValue().add(new BigInteger(second.getValue().toString())); return new LargeIntLiteral(result); } - @ExecFunction(name = "add", argTypes = {"LARGEINT", "SMALLINT"}, returnType = "BIGINT") + @ExecFunction(name = "add", argTypes = {"LARGEINT", "SMALLINT"}, returnType = "LARGEINT") public static Expression addLargeIntSmallInt(LargeIntLiteral first, SmallIntLiteral second) { BigInteger result = first.getValue().add(new BigInteger(second.getValue().toString())); return new LargeIntLiteral(result); } - @ExecFunction(name = "add", argTypes = {"LARGEINT", "INT"}, returnType = "BIGINT") + @ExecFunction(name = "add", argTypes = {"LARGEINT", "INT"}, returnType = "LARGEINT") public static Expression addLargeIntInt(LargeIntLiteral first, IntegerLiteral second) { BigInteger result = first.getValue().add(new BigInteger(second.getValue().toString())); return new LargeIntLiteral(result); } - @ExecFunction(name = "add", argTypes = {"LARGEINT", "BIGINT"}, returnType = "BIGINT") + @ExecFunction(name = "add", argTypes = {"LARGEINT", "BIGINT"}, returnType = "LARGEINT") public static Expression addLargeIntBigInt(LargeIntLiteral first, BigIntLiteral second) { BigInteger result = first.getValue().add(new BigInteger(second.getValue().toString())); return new LargeIntLiteral(result); diff --git a/regression-test/data/nereids_rules_p0/expression/simplify_arithmetic/test_simplify_arithmetic.out b/regression-test/data/nereids_rules_p0/expression/simplify_arithmetic/test_simplify_arithmetic.out new file mode 100644 index 0000000000..e7c6a5a1dd --- /dev/null +++ b/regression-test/data/nereids_rules_p0/expression/simplify_arithmetic/test_simplify_arithmetic.out @@ -0,0 +1,3 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !return_type_after_projection_should_be_bigint -- + diff --git a/regression-test/framework/src/main/groovy/org/apache/doris/regression/action/ExplainAction.groovy b/regression-test/framework/src/main/groovy/org/apache/doris/regression/action/ExplainAction.groovy index 1458c78aec..e6f05c6c76 100644 --- a/regression-test/framework/src/main/groovy/org/apache/doris/regression/action/ExplainAction.groovy +++ b/regression-test/framework/src/main/groovy/org/apache/doris/regression/action/ExplainAction.groovy @@ -28,6 +28,7 @@ import java.util.stream.Collectors @Slf4j class ExplainAction implements SuiteAction { private String sql + private boolean verbose = false private SuiteContext context private Set containsStrings = new LinkedHashSet<>() private Set notContainsStrings = new LinkedHashSet<>() @@ -43,6 +44,10 @@ class ExplainAction implements SuiteAction { this.sql = sql } + void verbose(boolean verbose) { + this.verbose = verbose + } + void sql(Closure sqlSupplier) { this.sql = sqlSupplier.call() } @@ -61,7 +66,7 @@ class ExplainAction implements SuiteAction { @Override void run() { - String explainSql = "explain\n" + sql + String explainSql = "explain\n" + (verbose ? "verbose\n" : "") + sql def result = doTest(explainSql) String explainString = result.result if (checkFunction != null) { diff --git a/regression-test/suites/nereids_rules_p0/expression/simplify_arithmetic/test_simplify_arithmetic.groovy b/regression-test/suites/nereids_rules_p0/expression/simplify_arithmetic/test_simplify_arithmetic.groovy new file mode 100644 index 0000000000..e0abc3f16c --- /dev/null +++ b/regression-test/suites/nereids_rules_p0/expression/simplify_arithmetic/test_simplify_arithmetic.groovy @@ -0,0 +1,39 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("test_simplify_arithmetic") { + sql "SET enable_nereids_planner=true" + sql "SET enable_fallback_to_original_planner=false" + + sql """ + DROP TABLE IF EXISTS test_simplify_arithmetic + """ + sql """ + create table test_simplify_arithmetic(id smallint) distributed by random properties('replication_num'='1'); + """ + + // return type after projection should be bigint + explain { + sql """ select -3 - (7 + id) from test_simplify_arithmetic""" + verbose true + contains """type=BIGINT""" + } + + qt_return_type_after_projection_should_be_bigint """ + select -3 - (7 + id) as c1 from test_simplify_arithmetic group by c1 + """ +}