Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch overflow in decimal multiplication #10737

Merged
merged 1 commit into from
Jan 24, 2022
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
16 changes: 14 additions & 2 deletions core/trino-main/src/main/java/io/trino/type/DecimalOperators.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
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;
import static io.trino.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE;
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
Expand Down Expand Up @@ -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
Expand Down