Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading