未验证 提交 1e141865 编写于 作者: A Artem Zuikov 提交者: GitHub

Merge pull request #5282 from 4ertus2/decimal

Throw Decimal overflow for Inf and NaN
#pragma once #pragma once
#include <cmath>
#include <common/arithmeticOverflow.h> #include <common/arithmeticOverflow.h>
#include <Common/typeid_cast.h> #include <Common/typeid_cast.h>
#include <Columns/ColumnDecimal.h> #include <Columns/ColumnDecimal.h>
...@@ -319,7 +321,11 @@ convertToDecimal(const typename FromDataType::FieldType & value, UInt32 scale) ...@@ -319,7 +321,11 @@ convertToDecimal(const typename FromDataType::FieldType & value, UInt32 scale)
using FromFieldType = typename FromDataType::FieldType; using FromFieldType = typename FromDataType::FieldType;
if constexpr (std::is_floating_point_v<FromFieldType>) if constexpr (std::is_floating_point_v<FromFieldType>)
{
if (!std::isfinite(value))
throw Exception("Decimal convert overflow. Cannot convert infinity or NaN to decimal", ErrorCodes::DECIMAL_OVERFLOW);
return value * ToDataType::getScaleMultiplier(scale); return value * ToDataType::getScaleMultiplier(scale);
}
else else
{ {
if constexpr (std::is_same_v<FromFieldType, UInt64>) if constexpr (std::is_same_v<FromFieldType, UInt64>)
......
...@@ -240,3 +240,13 @@ SELECT toUInt64('9223372036854775809') AS x, toDecimal64(x, 0); -- { serverError ...@@ -240,3 +240,13 @@ SELECT toUInt64('9223372036854775809') AS x, toDecimal64(x, 0); -- { serverError
SELECT toDecimal32(0, rowNumberInBlock()); -- { serverError 44 } SELECT toDecimal32(0, rowNumberInBlock()); -- { serverError 44 }
SELECT toDecimal64(0, rowNumberInBlock()); -- { serverError 44 } SELECT toDecimal64(0, rowNumberInBlock()); -- { serverError 44 }
SELECT toDecimal128(0, rowNumberInBlock()); -- { serverError 44 } SELECT toDecimal128(0, rowNumberInBlock()); -- { serverError 44 }
SELECT toDecimal32(1/0, 0); -- { serverError 407 }
SELECT toDecimal64(1/0, 1); -- { serverError 407 }
SELECT toDecimal128(0/0, 2); -- { serverError 407 }
SELECT CAST(1/0, 'Decimal(9, 0)'); -- { serverError 407 }
SELECT CAST(1/0, 'Decimal(18, 1)'); -- { serverError 407 }
SELECT CAST(1/0, 'Decimal(38, 2)'); -- { serverError 407 }
SELECT CAST(0/0, 'Decimal(9, 3)'); -- { serverError 407 }
SELECT CAST(0/0, 'Decimal(18, 4)'); -- { serverError 407 }
SELECT CAST(0/0, 'Decimal(38, 5)'); -- { serverError 407 }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册