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

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


The following commit(s) were added to refs/heads/branch-2.1 by this push:
     new 60d3c1e31d7 branch-2.1: [fix](nereids) fix compare with date like 
overflow #45868 (#45929)
60d3c1e31d7 is described below

commit 60d3c1e31d76733930bc638ec0eb160585ac5360
Author: yujun <[email protected]>
AuthorDate: Fri Dec 27 10:55:42 2024 +0800

    branch-2.1: [fix](nereids) fix compare with date like overflow #45868 
(#45929)
    
    cherry pick from #45868
---
 .../rules/SimplifyComparisonPredicate.java         | 17 +++++-
 .../expressions/literal/DateTimeV2Literal.java     |  7 ++-
 .../rules/SimplifyComparisonPredicateTest.java     | 65 ++++++++++++++++++++++
 3 files changed, 85 insertions(+), 4 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 a433d8bd221..76f97888587 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
@@ -17,6 +17,7 @@
 
 package org.apache.doris.nereids.rules.expression.rules;
 
+import org.apache.doris.nereids.exceptions.AnalysisException;
 import org.apache.doris.nereids.rules.expression.AbstractExpressionRewriteRule;
 import org.apache.doris.nereids.rules.expression.ExpressionPatternMatcher;
 import org.apache.doris.nereids.rules.expression.ExpressionPatternRuleFactory;
@@ -165,8 +166,13 @@ public class SimplifyComparisonPredicate extends 
AbstractExpressionRewriteRule i
             if (cast.child().getDataType() instanceof DateTimeType
                     || cast.child().getDataType() instanceof DateTimeV2Type) {
                 if (right instanceof DateTimeV2Literal) {
-                    return 
processDateTimeLikeComparisonPredicateDateTimeV2Literal(
-                            cp, cast.child(), (DateTimeV2Literal) right);
+                    try {
+                        return 
processDateTimeLikeComparisonPredicateDateTimeV2Literal(
+                                cp, cast.child(), (DateTimeV2Literal) right);
+                    } catch (AnalysisException e) {
+                        // '9999-12-31 23:59:59.9'.roundCeiling(0) overflow
+                        return cp;
+                    }
                 }
             }
 
@@ -182,7 +188,12 @@ public class SimplifyComparisonPredicate extends 
AbstractExpressionRewriteRule i
                         } else if (cp instanceof NullSafeEqual) {
                             return BooleanLiteral.FALSE;
                         } else if (cp instanceof GreaterThanEqual || cp 
instanceof LessThan) {
-                            right = ((DateV2Literal) right).plusDays(1);
+                            // '9999-12-31' + 1 will overflow
+                            Expression tomorrow = ((DateV2Literal) 
right).plusDays(1);
+                            if (tomorrow.isNullLiteral()) {
+                                return cp;
+                            }
+                            right = tomorrow;
                         }
                     }
                     if (cast.child().getDataType() instanceof DateV2Type) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeV2Literal.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeV2Literal.java
index c6ec4b99d4f..69a75e001ab 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeV2Literal.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeV2Literal.java
@@ -250,7 +250,12 @@ public class DateTimeV2Literal extends DateTimeLiteral {
         }
         if (newMicroSecond > MAX_MICROSECOND) {
             newMicroSecond %= newMicroSecond;
-            DateTimeV2Literal result = (DateTimeV2Literal) this.plusSeconds(1);
+            Expression plus1Second = this.plusSeconds(1);
+            if (plus1Second.isNullLiteral()) {
+                throw new AnalysisException("round ceil datetime literal (" + 
toString() + ", "
+                        + newScale + ") is out of range");
+            }
+            DateTimeV2Literal result = (DateTimeV2Literal) plus1Second;
             newSecond = result.second;
             newMinute = result.minute;
             newHour = result.hour;
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicateTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicateTest.java
index 210408f80dc..e0b7302d89f 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicateTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/SimplifyComparisonPredicateTest.java
@@ -257,6 +257,62 @@ class SimplifyComparisonPredicateTest extends 
ExpressionRewriteTestHelper {
                 new LessThan(datetime2, new DateTimeV2Literal("2020-01-01 
00:00:00.13")));
         assertRewrite(new LessThanEqual(new Cast(datetime2, 
DateTimeV2Type.of(3)), new DateTimeV2Literal("2020-01-01 00:00:00.123")),
                 new LessThanEqual(datetime2, new DateTimeV2Literal("2020-01-01 
00:00:00.12")));
+
+        // test with low bound
+        assertRewrite(new LessThan(new Cast(date, DateTimeType.INSTANCE), new 
DateTimeLiteral("0000-01-01 00:00:00")),
+                new LessThan(date, new DateV2Literal("0000-01-01")));
+        assertRewrite(new LessThan(new Cast(date, 
DateTimeV2Type.SYSTEM_DEFAULT), new DateTimeV2Literal("0000-01-01 00:00:00")),
+                new LessThan(date, new DateV2Literal("0000-01-01")));
+        assertRewrite(new LessThan(new Cast(datev1, DateTimeType.INSTANCE), 
new DateTimeLiteral("0000-01-01 00:00:00")),
+                new LessThan(datev1, new DateLiteral("0000-01-01")));
+        assertRewrite(new LessThan(new Cast(datev1, 
DateTimeV2Type.SYSTEM_DEFAULT), new DateTimeV2Literal("0000-01-01 00:00:00")),
+                new LessThan(datev1, new DateLiteral("0000-01-01")));
+        assertRewrite(new LessThan(new Cast(datetime0, DateTimeV2Type.of(1)), 
new DateTimeV2Literal("0000-01-01 00:00:00.1")),
+                new LessThan(datetime0, new DateTimeV2Literal("0000-01-01 
00:00:01")));
+        assertRewrite(new LessThan(new Cast(datetime2, DateTimeV2Type.of(3)), 
new DateTimeV2Literal("0000-01-01 00:00:00.991")),
+                new LessThan(datetime2, new 
DateTimeV2Literal(DateTimeV2Type.of(2), "0000-01-01 00:00:01.00")));
+        assertRewrite(new LessThan(new Cast(datetime2, DateTimeV2Type.of(6)), 
new DateTimeV2Literal("0000-01-01 00:00:00.999999")),
+                new LessThan(datetime2, new 
DateTimeV2Literal(DateTimeV2Type.of(2), "0000-01-01 00:00:01.00")));
+        assertRewrite(new LessThan(new Cast(datetimev1, DateTimeV2Type.of(1)), 
new DateTimeV2Literal(DateTimeV2Type.of(1), "0000-01-01 00:00:00.0")),
+                new LessThan(datetimev1, new DateTimeLiteral("0000-01-01 
00:00:00")));
+        assertRewrite(new LessThan(new Cast(datetimev1, DateTimeV2Type.of(1)), 
new DateTimeV2Literal("0000-01-01 00:00:00.1")),
+                new LessThan(datetimev1, new DateTimeLiteral("0000-01-01 
00:00:01")));
+        assertRewrite(new GreaterThan(new Cast(date, DateTimeType.INSTANCE), 
new DateTimeLiteral("0000-01-01 00:00:00")),
+                new GreaterThan(date, new DateV2Literal("0000-01-01")));
+        assertRewrite(new GreaterThan(new Cast(date, 
DateTimeV2Type.SYSTEM_DEFAULT), new DateTimeV2Literal("0000-01-01 00:00:00")),
+                new GreaterThan(date, new DateV2Literal("0000-01-01")));
+        assertRewrite(new GreaterThan(new Cast(datev1, DateTimeType.INSTANCE), 
new DateTimeLiteral("0000-01-01 00:00:00")),
+                new GreaterThan(datev1, new DateLiteral("0000-01-01")));
+        assertRewrite(new GreaterThan(new Cast(datev1, 
DateTimeV2Type.SYSTEM_DEFAULT), new DateTimeV2Literal("0000-01-01 00:00:00")),
+                new GreaterThan(datev1, new DateLiteral("0000-01-01")));
+        assertRewrite(new GreaterThan(new Cast(datetime0, 
DateTimeV2Type.of(1)), new DateTimeV2Literal("0000-01-01 00:00:00.1")),
+                new GreaterThan(datetime0, new DateTimeV2Literal("0000-01-01 
00:00:00")));
+        assertRewrite(new GreaterThan(new Cast(datetime2, 
DateTimeV2Type.of(3)), new DateTimeV2Literal("0000-01-01 00:00:00.991")),
+                new GreaterThan(datetime2, new DateTimeV2Literal("0000-01-01 
00:00:00.99")));
+        assertRewrite(new GreaterThan(new Cast(datetime2, 
DateTimeV2Type.of(6)), new DateTimeV2Literal("0000-01-01 00:00:00.999999")),
+                new GreaterThan(datetime2, new DateTimeV2Literal("0000-01-01 
00:00:00.99")));
+        assertRewrite(new GreaterThan(new Cast(datetimev1, 
DateTimeV2Type.of(1)), new DateTimeV2Literal(DateTimeV2Type.of(1), "0000-01-01 
00:00:00.0")),
+                new GreaterThan(datetimev1, new DateTimeLiteral("0000-01-01 
00:00:00")));
+        assertRewrite(new GreaterThan(new Cast(datetimev1, 
DateTimeV2Type.of(1)), new DateTimeV2Literal("0000-01-01 00:00:00.1")),
+                new GreaterThan(datetimev1, new DateTimeLiteral("0000-01-01 
00:00:00")));
+
+        // test overflow, not cast
+        assertRewrite(new LessThan(new Cast(date, DateTimeType.INSTANCE), new 
DateTimeLiteral("9999-12-31 23:59:59")),
+                new LessThan(new Cast(date, DateTimeType.INSTANCE), new 
DateTimeLiteral("9999-12-31 23:59:59")));
+        assertRewrite(new LessThan(new Cast(date, 
DateTimeV2Type.SYSTEM_DEFAULT), new DateTimeV2Literal("9999-12-31 23:59:59")),
+                new LessThan(new Cast(date, DateTimeV2Type.SYSTEM_DEFAULT), 
new DateTimeV2Literal("9999-12-31 23:59:59")));
+        assertRewrite(new LessThan(new Cast(datev1, DateTimeType.INSTANCE), 
new DateTimeLiteral("9999-12-31 23:59:59")),
+                new LessThan(new Cast(datev1, DateTimeType.INSTANCE), new 
DateTimeLiteral("9999-12-31 23:59:59")));
+        assertRewrite(new LessThan(new Cast(datev1, 
DateTimeV2Type.SYSTEM_DEFAULT), new DateTimeV2Literal("9999-12-31 23:59:59")),
+                new LessThan(new Cast(datev1, DateTimeV2Type.SYSTEM_DEFAULT), 
new DateTimeV2Literal("9999-12-31 23:59:59")));
+        assertRewrite(new LessThan(new Cast(datetime0, DateTimeV2Type.of(1)), 
new DateTimeV2Literal("9999-12-31 23:59:59.1")),
+                new LessThan(new Cast(datetime0, DateTimeV2Type.of(1)), new 
DateTimeV2Literal("9999-12-31 23:59:59.1")));
+        assertRewrite(new LessThan(new Cast(datetime2, DateTimeV2Type.of(3)), 
new DateTimeV2Literal("9999-12-31 23:59:59.991")),
+                new LessThan(new Cast(datetime2, DateTimeV2Type.of(3)), new 
DateTimeV2Literal("9999-12-31 23:59:59.991")));
+        assertRewrite(new LessThan(new Cast(datetime2, DateTimeV2Type.of(6)), 
new DateTimeV2Literal("9999-12-31 23:59:59.999999")),
+                new LessThan(new Cast(datetime2, DateTimeV2Type.of(6)), new 
DateTimeV2Literal("9999-12-31 23:59:59.999999")));
+        assertRewrite(new LessThan(new Cast(datetimev1, DateTimeV2Type.of(1)), 
new DateTimeV2Literal("9999-12-31 23:59:59.1")),
+                new LessThan(new Cast(datetimev1, DateTimeV2Type.of(1)), new 
DateTimeV2Literal("9999-12-31 23:59:59.1")));
     }
 
     @Test
@@ -445,6 +501,15 @@ class SimplifyComparisonPredicateTest extends 
ExpressionRewriteTestHelper {
                 new LessThan(bigIntSlot, new BigIntLiteral(13L)));
         assertRewrite(new LessThanEqual(new Cast(bigIntSlot, 
DecimalV3Type.createDecimalV3Type(3, 1)), new DecimalV3Literal(new 
BigDecimal("12.3"))),
                 new LessThanEqual(bigIntSlot, new BigIntLiteral(12L)));
+
+        assertRewrite(new LessThan(new Cast(bigIntSlot, 
DecimalV3Type.createDecimalV3Type(20, 1)), new DecimalV3Literal(new 
BigDecimal("-9223372036854775808.1"))),
+                new LessThan(new Cast(bigIntSlot, 
DecimalV3Type.createDecimalV3Type(20, 1)), new DecimalV3Literal(new 
BigDecimal("-9223372036854775808.1"))));
+        assertRewrite(new LessThan(new Cast(bigIntSlot, 
DecimalV3Type.createDecimalV3Type(20, 1)), new DecimalV3Literal(new 
BigDecimal("-9223372036854775807.1"))),
+                new LessThan(bigIntSlot, new 
BigIntLiteral(-9223372036854775807L)));
+        assertRewrite(new GreaterThanEqual(new Cast(bigIntSlot, 
DecimalV3Type.createDecimalV3Type(20, 1)), new DecimalV3Literal(new 
BigDecimal("9223372036854775807.1"))),
+                new GreaterThanEqual(new Cast(bigIntSlot, 
DecimalV3Type.createDecimalV3Type(20, 1)), new DecimalV3Literal(new 
BigDecimal("9223372036854775807.1"))));
+        assertRewrite(new LessThan(new Cast(bigIntSlot, 
DecimalV3Type.createDecimalV3Type(20, 1)), new DecimalV3Literal(new 
BigDecimal("9223372036854775806.1"))),
+                new LessThan(bigIntSlot, new 
BigIntLiteral(9223372036854775807L)));
     }
 
     @Test


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

Reply via email to