From fcc6a2c2be219daf9fae798486785a4c79030c83 Mon Sep 17 00:00:00 2001 From: chertus Date: Fri, 2 Aug 2019 17:31:15 +0300 Subject: [PATCH] fix crash on CAST exotic types to decimal --- dbms/src/DataTypes/IDataType.h | 5 ++--- dbms/src/Functions/FunctionsConversion.h | 11 +++++++++++ .../0_stateless/00879_cast_to_decimal_crash.reference | 0 .../0_stateless/00879_cast_to_decimal_crash.sql | 1 + 4 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 dbms/tests/queries/0_stateless/00879_cast_to_decimal_crash.reference create mode 100644 dbms/tests/queries/0_stateless/00879_cast_to_decimal_crash.sql diff --git a/dbms/src/DataTypes/IDataType.h b/dbms/src/DataTypes/IDataType.h index f4c22ff9ac8..e5020fe19de 100644 --- a/dbms/src/DataTypes/IDataType.h +++ b/dbms/src/DataTypes/IDataType.h @@ -463,9 +463,8 @@ struct WhichDataType { TypeIndex idx; - /// For late initialization. - WhichDataType() - : idx(TypeIndex::Nothing) + WhichDataType(TypeIndex idx_ = TypeIndex::Nothing) + : idx(idx_) {} WhichDataType(const IDataType & data_type) diff --git a/dbms/src/Functions/FunctionsConversion.h b/dbms/src/Functions/FunctionsConversion.h index 7d1d277752f..a83ae586d80 100644 --- a/dbms/src/Functions/FunctionsConversion.h +++ b/dbms/src/Functions/FunctionsConversion.h @@ -1634,6 +1634,17 @@ private: TypeIndex type_index = from_type->getTypeId(); UInt32 scale = to_type->getScale(); + WhichDataType which(type_index); + bool ok = which.isNativeInt() || + which.isNativeUInt() || + which.isDecimal() || + which.isFloat() || + which.isDateOrDateTime() || + which.isStringOrFixedString(); + if (!ok) + throw Exception{"Conversion from " + from_type->getName() + " to " + to_type->getName() + " is not supported", + ErrorCodes::CANNOT_CONVERT_TYPE}; + return [type_index, scale] (Block & block, const ColumnNumbers & arguments, const size_t result, size_t input_rows_count) { callOnIndexAndDataType(type_index, [&](const auto & types) -> bool diff --git a/dbms/tests/queries/0_stateless/00879_cast_to_decimal_crash.reference b/dbms/tests/queries/0_stateless/00879_cast_to_decimal_crash.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/dbms/tests/queries/0_stateless/00879_cast_to_decimal_crash.sql b/dbms/tests/queries/0_stateless/00879_cast_to_decimal_crash.sql new file mode 100644 index 00000000000..d07a54ecd5a --- /dev/null +++ b/dbms/tests/queries/0_stateless/00879_cast_to_decimal_crash.sql @@ -0,0 +1 @@ +select cast(toIntervalDay(1) as Nullable(Decimal(10, 10))); -- { serverError 70 }