[fix](Nereids) simplify airthmetic should not change return type (#31237)

This commit is contained in:
morrySnow
2024-02-22 14:08:32 +08:00
committed by yiguolei
parent fa83a8f86a
commit 98c3cb825f
5 changed files with 54 additions and 6 deletions

View File

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

View File

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

View File

@ -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 --

View File

@ -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) {

View File

@ -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
"""
}