Skip to content

Commit

Permalink
Merge pull request #6346 from yandex/more-checks-in-cast-function
Browse files Browse the repository at this point in the history
Added more checks in CAST function.
  • Loading branch information
KochetovNicolai committed Aug 6, 2019
2 parents eadb6ef + 50c39e8 commit 6685ccb
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions dbms/src/Functions/FunctionsConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,7 @@ class FunctionCast final : public IFunctionBase
using ToDataType = DataTypeDecimal<FieldType>;

TypeIndex type_index = from_type->getTypeId();
UInt32 precision = to_type->getPrecision();
UInt32 scale = to_type->getScale();

WhichDataType which(type_index);
Expand All @@ -1645,9 +1646,9 @@ class FunctionCast final : public IFunctionBase
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)
return [type_index, precision, scale] (Block & block, const ColumnNumbers & arguments, const size_t result, size_t input_rows_count)
{
callOnIndexAndDataType<ToDataType>(type_index, [&](const auto & types) -> bool
auto res = callOnIndexAndDataType<ToDataType>(type_index, [&](const auto & types) -> bool
{
using Types = std::decay_t<decltype(types)>;
using LeftDataType = typename Types::LeftType;
Expand All @@ -1656,6 +1657,14 @@ class FunctionCast final : public IFunctionBase
ConvertImpl<LeftDataType, RightDataType, NameCast>::execute(block, arguments, result, input_rows_count, scale);
return true;
});

/// Additionally check if callOnIndexAndDataType wasn't called at all.
if (!res)
{
auto to = DataTypeDecimal<FieldType>(precision, scale);
throw Exception{"Conversion from " + std::string(getTypeName(type_index)) + " to " + to.getName() +
" is not supported", ErrorCodes::CANNOT_CONVERT_TYPE};
}
};
}

Expand Down Expand Up @@ -2022,6 +2031,11 @@ class FunctionCast final : public IFunctionBase

const auto & tmp_res = tmp_block.getByPosition(tmp_res_index);

/// May happen in fuzzy tests. For debug purpose.
if (!tmp_res.column)
throw Exception("Couldn't convert " + block.getByPosition(arguments[0]).type->getName() + " to "
+ nested_type->getName() + " in " + " prepareRemoveNullable wrapper.", ErrorCodes::LOGICAL_ERROR);

res.column = wrapInNullable(tmp_res.column, Block({block.getByPosition(arguments[0]), tmp_res}), {0}, 1, input_rows_count);
};
}
Expand Down

0 comments on commit 6685ccb

Please sign in to comment.