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

yiguolei 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 6bf0700ae58 [fix](nereids)SimplifyComparisonPredicate rule create 
wrong result when simplify int compare with decimal literal (#28341)
6bf0700ae58 is described below

commit 6bf0700ae58457c6bac791ef7d61225bbcc7b310
Author: starocean999 <[email protected]>
AuthorDate: Wed Dec 13 18:18:24 2023 +0800

    [fix](nereids)SimplifyComparisonPredicate rule create wrong result when 
simplify int compare with decimal literal (#28341)
---
 .../rules/expression/rules/SimplifyComparisonPredicate.java      | 9 +++++----
 .../data/nereids_syntax_p0/test_simplify_comparison.out          | 7 +++++++
 .../suites/nereids_syntax_p0/test_simplify_comparison.groovy     | 5 ++++-
 3 files changed, 16 insertions(+), 5 deletions(-)

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 b1c54d82a74..ab72b023afa 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 00000000000..5507066c148
--- /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 9a0a2c5bf59..5b83af29c13 100644
--- a/regression-test/suites/nereids_syntax_p0/test_simplify_comparison.groovy
+++ b/regression-test/suites/nereids_syntax_p0/test_simplify_comparison.groovy
@@ -37,7 +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);"
         notContains "CAST"
@@ -269,4 +269,7 @@ 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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to