From 8ac52aeda7a482ce058ac46a2cc8e26e8559bba3 Mon Sep 17 00:00:00 2001 From: starocean999 <40539150+starocean999@users.noreply.github.com> Date: Wed, 13 Dec 2023 16:13:42 +0800 Subject: [PATCH] [fix](nereids) wrong result when simplify int compare with decimal literal (#28195) SimplifyComparisonPredicate rule create wrong result when simplify int compare with decimal literal --- .../expression/rules/SimplifyComparisonPredicate.java | 9 +++++---- .../data/nereids_syntax_p0/test_simplify_comparison.out | 7 +++++++ .../nereids_syntax_p0/test_simplify_comparison.groovy | 3 +++ 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 regression-test/data/nereids_syntax_p0/test_simplify_comparison.out diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicate.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicate.java index b1c54d82a7..ab72b023af 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicate.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicate.java @@ -267,6 +267,7 @@ public class SimplifyComparisonPredicate extends AbstractExpressionRewriteRule { ComparisonPredicate comparisonPredicate, Expression left, BigDecimal literal) { // we only process isIntegerLikeType, which are tinyint, smallint, int, bigint if (literal.compareTo(new BigDecimal(Long.MAX_VALUE)) <= 0) { + literal = literal.stripTrailingZeros(); if (literal.scale() > 0) { if (comparisonPredicate instanceof EqualTo) { if (left.nullable()) { @@ -303,14 +304,14 @@ public class SimplifyComparisonPredicate extends AbstractExpressionRewriteRule { private IntegerLikeLiteral convertDecimalToIntegerLikeLiteral(BigDecimal decimal) { Preconditions.checkArgument( - decimal.scale() == 0 && decimal.compareTo(new BigDecimal(Long.MAX_VALUE)) <= 0, + decimal.scale() <= 0 && decimal.compareTo(new BigDecimal(Long.MAX_VALUE)) <= 0, "decimal literal must have 0 scale and smaller than Long.MAX_VALUE"); long val = decimal.longValue(); - if (val <= Byte.MAX_VALUE) { + if (val >= Byte.MIN_VALUE && val <= Byte.MAX_VALUE) { return new TinyIntLiteral((byte) val); - } else if (val <= Short.MAX_VALUE) { + } else if (val >= Short.MIN_VALUE && val <= Short.MAX_VALUE) { return new SmallIntLiteral((short) val); - } else if (val <= Integer.MAX_VALUE) { + } else if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) { return new IntegerLiteral((int) val); } else { return new BigIntLiteral(val); diff --git a/regression-test/data/nereids_syntax_p0/test_simplify_comparison.out b/regression-test/data/nereids_syntax_p0/test_simplify_comparison.out new file mode 100644 index 0000000000..5507066c14 --- /dev/null +++ b/regression-test/data/nereids_syntax_p0/test_simplify_comparison.out @@ -0,0 +1,7 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select1 -- +10 100 1000 10000 100000 + +-- !select2 -- +1 1.00 3 + diff --git a/regression-test/suites/nereids_syntax_p0/test_simplify_comparison.groovy b/regression-test/suites/nereids_syntax_p0/test_simplify_comparison.groovy index 9a0a2c5bf5..a5b3ef28e4 100644 --- a/regression-test/suites/nereids_syntax_p0/test_simplify_comparison.groovy +++ b/regression-test/suites/nereids_syntax_p0/test_simplify_comparison.groovy @@ -37,6 +37,7 @@ suite("test_simplify_comparison") { "in_memory" = "false", "compression" = "LZ4" );""" + sql """insert into simple_test_table_t values( 10, 100, 1000, 10000, 100000);""" explain { sql "verbose select * from simple_test_table_t where a = cast(1.0 as double) and b = cast(1.0 as double) and c = cast(1.0 as double) and d = cast(1.0 as double);" @@ -269,4 +270,6 @@ suite("test_simplify_comparison") { sql "verbose select * from simple_test_table_t where e <= 1.1;" contains "CAST(e[#4] AS DOUBLE) <= 1.1" } + qt_select1 """select * from simple_test_table_t where cast(a as decimal(5,1)) = 10.0;""" + qt_select2 """select a.col1, cast(a.col1 as decimal(7,2)) col3, case when a.col1 is null then 15 when cast(a.col1 as decimal(7,2)) < -99997.99 then 18 when cast(a.col1 as decimal(7,2)) < 1.001 then 3 else -55 end col2 from (select 1 as col1) a;""" } \ No newline at end of file