Merge pull request #23961 from kitaisreal/array-difference-decimal-math-overflow

Function arrayDifference decimal math overflow
This commit is contained in:
Maksim Kita 2021-05-09 14:04:35 +03:00 committed by GitHub
commit e517436ba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 1 deletions

View File

@ -13,6 +13,7 @@ namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int ILLEGAL_COLUMN;
extern const int DECIMAL_OVERFLOW;
}
/** arrayDifference() - returns an array with the difference between all pairs of neighboring elements.
@ -63,7 +64,23 @@ struct ArrayDifferenceImpl
else
{
Element curr = src[pos];
dst[pos] = curr - prev;
if constexpr (IsDecimalNumber<Element>)
{
using ResultNativeType = typename Result::NativeType;
ResultNativeType result_value;
bool overflow = common::subOverflow(static_cast<ResultNativeType>(curr.value), static_cast<ResultNativeType>(prev), result_value);
if (overflow)
throw Exception(ErrorCodes::DECIMAL_OVERFLOW, "Decimal math overflow");
dst[pos] = Result(result_value);
}
else
{
dst[pos] = curr - prev;
}
prev = curr;
}
}

View File

@ -0,0 +1 @@
SELECT arrayDifference([toDecimal32(100.0000991821289, 0), -2147483647]) AS x; --{serverError 407}