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

morrysnow pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new 60a1ffb6e3e [fix](Nereids) simplify airthmetic should not change 
return type (#31237) (#31266)
60a1ffb6e3e is described below

commit 60a1ffb6e3e6a933bb7a3009806ac0ea8cfd84fd
Author: morrySnow <[email protected]>
AuthorDate: Thu Feb 22 17:16:49 2024 +0800

    [fix](Nereids) simplify airthmetic should not change return type (#31237) 
(#31266)
    
    pick from master #31237
    commit id abb98747a6400e007c063e5a17b0d97d8249c8b5
---
 .../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 28e34af5f05..f0fb57c76d5 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 58a979b465f..6f974c481c2 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<>()
@@ -41,6 +42,10 @@ class ExplainAction implements SuiteAction {
         this.sql = sql
     }
 
+    void verbose(boolean verbose) {
+        this.verbose = verbose
+    }
+
     void sql(Closure<String> sqlSupplier) {
         this.sql = sqlSupplier.call()
     }
@@ -59,7 +64,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..bc1fc20ee8f
--- /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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to