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

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


The following commit(s) were added to refs/heads/branch-3.1 by this push:
     new 525fca83eae branch-3.1: [fix](LargeInt)Fix large int max_value + 1 not 
overflow bug. #56083 (#57351)
525fca83eae is described below

commit 525fca83eae4c4e9fad180f85583bdfc9da611dc
Author: James <[email protected]>
AuthorDate: Tue Oct 28 15:20:07 2025 +0800

    branch-3.1: [fix](LargeInt)Fix large int max_value + 1 not overflow bug. 
#56083 (#57351)
    
    backport: #56083
---
 .../org/apache/doris/analysis/LargeIntLiteral.java |  6 +-
 .../trees/expressions/literal/LargeIntLiteral.java |  8 +++
 .../expressions/literal/LargeIntLiteralTest.java   | 66 ++++++++++++++++++++++
 .../org/apache/doris/rewrite/FEFunctionsTest.java  |  4 +-
 4 files changed, 80 insertions(+), 4 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java
index f323ccc700a..87bfc5c094f 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java
@@ -75,7 +75,8 @@ public class LargeIntLiteral extends NumericLiteralExpr {
             // ATTN: value from 'sql_parser.y' is always be positive. for 
example: '-256' will to be
             // 256, and for int8_t, 256 is invalid, while -256 is valid. So we 
check the right border
             // is LARGE_INT_MAX_ABS
-            if (bigInt.compareTo(LARGE_INT_MIN) < 0 || 
bigInt.compareTo(LARGE_INT_MAX_ABS) > 0) {
+            // if (bigInt.compareTo(LARGE_INT_MIN) < 0 || 
bigInt.compareTo(LARGE_INT_MAX_ABS) > 0) {
+            if (bigInt.compareTo(LARGE_INT_MIN) < 0 || 
bigInt.compareTo(LARGE_INT_MAX) > 0) {
                 throw new AnalysisException("Large int literal is out of 
range: " + value);
             }
         } catch (NumberFormatException e) {
@@ -94,7 +95,8 @@ public class LargeIntLiteral extends NumericLiteralExpr {
             // ATTN: value from 'sql_parser.y' is always be positive. for 
example: '-256' will to be
             // 256, and for int8_t, 256 is invalid, while -256 is valid. So we 
check the right border
             // is LARGE_INT_MAX_ABS
-            if (bigInt.compareTo(LARGE_INT_MIN) < 0 || 
bigInt.compareTo(LARGE_INT_MAX_ABS) > 0) {
+            // if (bigInt.compareTo(LARGE_INT_MIN) < 0 || 
bigInt.compareTo(LARGE_INT_MAX_ABS) > 0) {
+            if (bigInt.compareTo(LARGE_INT_MIN) < 0 || 
bigInt.compareTo(LARGE_INT_MAX) > 0) {
                 throw new AnalysisException("Large int literal is out of 
range: " + value);
             }
         } catch (NumberFormatException e) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/LargeIntLiteral.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/LargeIntLiteral.java
index 5f9d60a2ff7..d78da6ab66e 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/LargeIntLiteral.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/LargeIntLiteral.java
@@ -32,8 +32,16 @@ public class LargeIntLiteral extends IntegerLikeLiteral {
 
     private final BigInteger value;
 
+    /**
+     * LargeIntLiteral
+     * @param value Value.
+     */
     public LargeIntLiteral(BigInteger value) {
         super(LargeIntType.INSTANCE);
+        if (value.compareTo(LargeIntType.MAX_VALUE) > 0 || 
value.compareTo(LargeIntType.MIN_VALUE) < 0) {
+            throw new org.apache.doris.nereids.exceptions.AnalysisException(
+                    "Can not create LargeIntLiteral by value : " + value);
+        }
         this.value = Objects.requireNonNull(value);
     }
 
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/LargeIntLiteralTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/LargeIntLiteralTest.java
new file mode 100644
index 00000000000..08b49459954
--- /dev/null
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/LargeIntLiteralTest.java
@@ -0,0 +1,66 @@
+// 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.
+
+package org.apache.doris.nereids.trees.expressions.literal;
+
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.types.LargeIntType;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+public class LargeIntLiteralTest {
+
+    @Test
+    void testOverflow() throws org.apache.doris.common.AnalysisException {
+        LargeIntLiteral value = new LargeIntLiteral(LargeIntType.MIN_VALUE);
+        Assertions.assertEquals("-170141183460469231731687303715884105728", 
value.getValue().toString());
+        value = new LargeIntLiteral(LargeIntType.MAX_VALUE);
+        Assertions.assertEquals("170141183460469231731687303715884105727", 
value.getValue().toString());
+        Assertions.assertThrows(AnalysisException.class, () -> new 
LargeIntLiteral(LargeIntType.MAX_VALUE.add(new BigInteger("1"))));
+        Assertions.assertThrows(AnalysisException.class, () -> new 
LargeIntLiteral(LargeIntType.MIN_VALUE.subtract(new BigInteger("1"))));
+
+        
Assertions.assertThrows(org.apache.doris.common.AnalysisException.class,
+                () -> new 
org.apache.doris.analysis.LargeIntLiteral("170141183460469231731687303715884105728"));
+        
Assertions.assertThrows(org.apache.doris.common.AnalysisException.class,
+                () -> new 
org.apache.doris.analysis.LargeIntLiteral("-170141183460469231731687303715884105729"));
+        
Assertions.assertThrows(org.apache.doris.common.AnalysisException.class,
+                () -> new org.apache.doris.analysis.LargeIntLiteral(new 
BigDecimal("170141183460469231731687303715884105728")));
+        
Assertions.assertThrows(org.apache.doris.common.AnalysisException.class,
+                () -> new org.apache.doris.analysis.LargeIntLiteral(new 
BigDecimal("-170141183460469231731687303715884105729")));
+        org.apache.doris.analysis.LargeIntLiteral largeIntLiteral = new 
org.apache.doris.analysis.LargeIntLiteral(
+                "170141183460469231731687303715884105727");
+        Assertions.assertEquals("170141183460469231731687303715884105727", 
largeIntLiteral.toString());
+        largeIntLiteral = new org.apache.doris.analysis.LargeIntLiteral(
+                "-170141183460469231731687303715884105728");
+        Assertions.assertEquals("-170141183460469231731687303715884105728", 
largeIntLiteral.toString());
+    }
+
+    @Test
+    void testToLegacyLiteral() {
+        LargeIntLiteral value = new LargeIntLiteral(LargeIntType.MIN_VALUE);
+        LiteralExpr literalExpr = value.toLegacyLiteral();
+        Assertions.assertEquals("-170141183460469231731687303715884105728", 
literalExpr.toString());
+        value = new LargeIntLiteral(LargeIntType.MAX_VALUE);
+        literalExpr = value.toLegacyLiteral();
+        Assertions.assertEquals("170141183460469231731687303715884105727", 
literalExpr.toString());
+    }
+}
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
index 6f4035f9202..109fa8ddce5 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
@@ -610,8 +610,8 @@ public class FEFunctionsTest {
     @Test
     public void subtractBigIntTest() throws AnalysisException {
         LargeIntLiteral actualResult = FEFunctions.subtractBigInt(
-                new LargeIntLiteral("170141183460469231731687303715884105727"),
-                new 
LargeIntLiteral("170141183460469231731687303715884105728"));
+                new LargeIntLiteral("170141183460469231731687303715884105726"),
+                new 
LargeIntLiteral("170141183460469231731687303715884105727"));
         LargeIntLiteral expectedResult = new LargeIntLiteral("-1");
         Assert.assertEquals(expectedResult, actualResult);
 


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

Reply via email to