diff --git a/core/trino-main/src/main/java/io/trino/type/DecimalOperators.java b/core/trino-main/src/main/java/io/trino/type/DecimalOperators.java index cae5720c84a4..2312fa194e11 100644 --- a/core/trino-main/src/main/java/io/trino/type/DecimalOperators.java +++ b/core/trino-main/src/main/java/io/trino/type/DecimalOperators.java @@ -274,7 +274,13 @@ public static Int128 multiplyShortShortLong(long a, long b) public static Int128 multiplyLongLongLong(Int128 a, Int128 b) { try { - return multiply(a, b); + Int128 result = multiply(a, b); + + if (Decimals.overflows(result)) { + throw new TrinoException(NUMERIC_VALUE_OUT_OF_RANGE, "Decimal overflow"); + } + + return result; } catch (ArithmeticException e) { throw new TrinoException(NUMERIC_VALUE_OUT_OF_RANGE, "Decimal overflow", e); @@ -291,7 +297,13 @@ public static Int128 multiplyShortLongLong(long a, Int128 b) public static Int128 multiplyLongShortLong(Int128 a, long b) { try { - return multiply(a, b); + Int128 result = multiply(a, b); + + if (Decimals.overflows(result)) { + throw new TrinoException(NUMERIC_VALUE_OUT_OF_RANGE, "Decimal overflow"); + } + + return result; } catch (ArithmeticException e) { throw new TrinoException(NUMERIC_VALUE_OUT_OF_RANGE, "Decimal overflow", e); diff --git a/core/trino-main/src/test/java/io/trino/type/TestDecimalOperators.java b/core/trino-main/src/test/java/io/trino/type/TestDecimalOperators.java index c6297ca74be5..8453587f9bb5 100644 --- a/core/trino-main/src/test/java/io/trino/type/TestDecimalOperators.java +++ b/core/trino-main/src/test/java/io/trino/type/TestDecimalOperators.java @@ -14,6 +14,7 @@ package io.trino.type; import io.trino.operator.scalar.AbstractTestFunctions; +import io.trino.spi.type.Int128; import org.testng.annotations.Test; import static io.trino.spi.StandardErrorCode.DIVISION_BY_ZERO; @@ -21,6 +22,7 @@ import static io.trino.spi.function.OperatorType.INDETERMINATE; import static io.trino.spi.type.BooleanType.BOOLEAN; import static io.trino.spi.type.DecimalType.createDecimalType; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class TestDecimalOperators extends AbstractTestFunctions @@ -195,6 +197,15 @@ public void testMultiply() assertInvalidFunction("DECIMAL '.12345678901234567890123456789012345678' * DECIMAL '9'", NUMERIC_VALUE_OUT_OF_RANGE); assertInvalidFunction("DECIMAL '12345678901234567890123456789012345678' * DECIMAL '-9'", NUMERIC_VALUE_OUT_OF_RANGE); assertInvalidFunction("DECIMAL '.12345678901234567890123456789012345678' * DECIMAL '-9'", NUMERIC_VALUE_OUT_OF_RANGE); + + assertThatThrownBy(() -> DecimalOperators.multiplyLongShortLong(Int128.valueOf("12345678901234567890123456789012345678"), 9)) + .hasMessage("Decimal overflow"); + + assertThatThrownBy(() -> DecimalOperators.multiplyShortLongLong(9, Int128.valueOf("12345678901234567890123456789012345678"))) + .hasMessage("Decimal overflow"); + + assertThatThrownBy(() -> DecimalOperators.multiplyLongLongLong(Int128.valueOf("12345678901234567890123456789012345678"), Int128.valueOf("9"))) + .hasMessage("Decimal overflow"); } @Test