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 b6c05fa83c448f..562881708a70ba 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 @@ -72,7 +72,8 @@ public LargeIntLiteral(String value) throws AnalysisException { // 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) { @@ -91,7 +92,8 @@ public LargeIntLiteral(BigDecimal value) throws AnalysisException { // 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 e624ee4e85a3d3..b9469dba76f4ed 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 @@ -33,8 +33,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 index 17d9cedcc5116d..d8a33c6901aebc 100644 --- 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 @@ -17,14 +17,18 @@ 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.exceptions.CastException; import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.types.BigIntType; +import org.apache.doris.nereids.types.LargeIntType; import org.apache.doris.nereids.types.TinyIntType; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.math.BigDecimal; import java.math.BigInteger; public class LargeIntLiteralTest { @@ -47,4 +51,39 @@ void testUncheckedCastTo() { LargeIntLiteral finalD1 = d1; Assertions.assertThrows(CastException.class, () -> finalD1.checkedCastTo(BigIntType.INSTANCE)); } + + @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 371f74d755d1db..ce3b0c6a0c85e5 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 @@ -611,8 +611,8 @@ public void subtractDecimalV2Test() throws AnalysisException { @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); diff --git a/regression-test/suites/query_p0/sql_functions/conditional_functions/test_conditional_function.groovy b/regression-test/suites/query_p0/sql_functions/conditional_functions/test_conditional_function.groovy index a6c9f6b28cf703..8f1b659588c162 100644 --- a/regression-test/suites/query_p0/sql_functions/conditional_functions/test_conditional_function.groovy +++ b/regression-test/suites/query_p0/sql_functions/conditional_functions/test_conditional_function.groovy @@ -116,7 +116,7 @@ suite("test_conditional_function") { """ sql """ insert into t1 values - (1, null, '2023-01-02', '2023-01-01 00:00:00', '2023-01-02 00:00:00',2222222222222222222222222222222222222222222222222222222222222222222222,3333333333333333333333333333333333333333333333333333333333333333333333),(2, '2023-02-01', null, '2023-02-01 00:00:00', '2023-02-02 00:00:00', null,5555555555555555555555555555555555555555555555555555555555555555555555),(3, '2023-03-01', '2023-03-02', null, '2023-03-02 00:00:00', null,666666666666666666666666666666666666666666666666) + (1, null, '2023-01-02', '2023-01-01 00:00:00', '2023-01-02 00:00:00','2222222222222222222222222222222222222222222222222222222222222222222222','3333333333333333333333333333333333333333333333333333333333333333333333'),(2, '2023-02-01', null, '2023-02-01 00:00:00', '2023-02-02 00:00:00', null,'5555555555555555555555555555555555555555555555555555555555555555555555'),(3, '2023-03-01', '2023-03-02', null, '2023-03-02 00:00:00', null,'666666666666666666666666666666666666666666666666') """ sql """ insert into t2 select k1, array_agg(k4) from t1 group by k1;