You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by yi...@apache.org on 2024/02/22 11:52:23 UTC
(doris) 06/19: [fix](Nereids) simplify airthmetic should not change return type (#31237)
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
commit 98c3cb825fee0767002d24d0ea2d07a66f886d52
Author: morrySnow <10...@users.noreply.github.com>
AuthorDate: Thu Feb 22 14:08:32 2024 +0800
[fix](Nereids) simplify airthmetic should not change return type (#31237)
---
.../expression/rules/SimplifyArithmeticRule.java | 3 +-
.../functions/executable/NumericArithmetic.java | 8 ++---
.../test_simplify_arithmetic.out | 3 ++
.../doris/regression/action/ExplainAction.groovy | 7 +++-
.../test_simplify_arithmetic.groovy | 39 ++++++++++++++++++++++
5 files changed, 54 insertions(+), 6 deletions(-)
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 24f96e91053..fc7431a9994 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 0aad2849823..814fe04dfac 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 00000000000..e7c6a5a1dd5
--- /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 1458c78aec8..e6f05c6c765 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<String> containsStrings = new LinkedHashSet<>()
private Set<String> notContainsStrings = new LinkedHashSet<>()
@@ -43,6 +44,10 @@ class ExplainAction implements SuiteAction {
this.sql = sql
}
+ void verbose(boolean verbose) {
+ this.verbose = verbose
+ }
+
void sql(Closure<String> 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 00000000000..e0abc3f16c8
--- /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
+ """
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org