From 02f01a5104250670cba9e532ead2d0ad53e4ae08 Mon Sep 17 00:00:00 2001 From: chertus Date: Wed, 15 May 2019 15:28:44 +0300 Subject: [PATCH] throw Decimal overflow for Inf and NaN --- dbms/src/DataTypes/DataTypesDecimal.h | 6 ++++++ dbms/tests/queries/0_stateless/00700_decimal_casts.sql | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/dbms/src/DataTypes/DataTypesDecimal.h b/dbms/src/DataTypes/DataTypesDecimal.h index e001c094af1..32d6549a4c8 100644 --- a/dbms/src/DataTypes/DataTypesDecimal.h +++ b/dbms/src/DataTypes/DataTypesDecimal.h @@ -1,4 +1,6 @@ #pragma once +#include + #include #include #include @@ -318,7 +320,11 @@ convertToDecimal(const typename FromDataType::FieldType & value, UInt32 scale) using FromFieldType = typename FromDataType::FieldType; if constexpr (std::is_floating_point_v) + { + 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); + } else { if constexpr (std::is_same_v) diff --git a/dbms/tests/queries/0_stateless/00700_decimal_casts.sql b/dbms/tests/queries/0_stateless/00700_decimal_casts.sql index 242a244a7fa..db43ea84592 100644 --- a/dbms/tests/queries/0_stateless/00700_decimal_casts.sql +++ b/dbms/tests/queries/0_stateless/00700_decimal_casts.sql @@ -240,3 +240,13 @@ SELECT toUInt64('9223372036854775809') AS x, toDecimal64(x, 0); -- { serverError SELECT toDecimal32(0, rowNumberInBlock()); -- { serverError 44 } SELECT toDecimal64(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 }