throw Decimal overflow for Inf and NaN

This commit is contained in:
chertus 2019-05-15 15:28:44 +03:00
parent 98359a6a09
commit 02f01a5104
2 changed files with 16 additions and 0 deletions

View File

@ -1,4 +1,6 @@
#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>
@ -318,7 +320,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::isinf(value) || std::isnan(value))
throw Exception("Decimal convert overflow. Cannot convert infinity or NaN into 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>)

View File

@ -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 }