Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conversion from Nullable(Enum) to Nullable(String) #56644

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/Functions/FunctionsConversion.h
Expand Up @@ -4172,6 +4172,61 @@ arguments, result_type, input_rows_count); \
};
}

template <typename EnumType>
WrapperType createEnumToStringWrapper() const
{
const char * function_name = cast_name;
return [function_name] (
ColumnsWithTypeAndName & arguments, const DataTypePtr & res_type, const ColumnNullable * nullable_col, size_t /*input_rows_count*/)
{
using ColumnEnumType = EnumType::ColumnType;

const auto & first_col = arguments.front().column.get();
const auto & first_type = arguments.front().type.get();

const ColumnEnumType * enum_col = typeid_cast<const ColumnEnumType *>(first_col);
const EnumType * enum_type = typeid_cast<const EnumType *>(first_type);

if (enum_col && nullable_col && nullable_col->size() != enum_col->size())
throw Exception(ErrorCodes::LOGICAL_ERROR, "ColumnNullable is not compatible with original");

if (enum_col && enum_type)
{
const auto size = enum_col->size();
const auto & enum_data = enum_col->getData();

auto res = res_type->createColumn();

if (nullable_col)
{
for (size_t i = 0; i < size; ++i)
{
if (!nullable_col->isNullAt(i))
{
const auto & value = enum_type->getNameForValue(enum_data[i]);
res->insertData(value.data, value.size);
}
else
res->insertDefault();
}
}
else
{
for (size_t i = 0; i < size; ++i)
{
const auto & value = enum_type->getNameForValue(enum_data[i]);
res->insertData(value.data, value.size);
}
}

return res;
}
else
throw Exception(ErrorCodes::LOGICAL_ERROR, "Unexpected column {} as first argument of function {}",
first_col->getName(), function_name);
};
}

static WrapperType createIdentityWrapper(const DataTypePtr &)
{
return [] (ColumnsWithTypeAndName & arguments, const DataTypePtr &, const ColumnNullable *, size_t /*input_rows_count*/)
Expand Down Expand Up @@ -4559,7 +4614,12 @@ arguments, result_type, input_rows_count); \

if constexpr (WhichDataType(ToDataType::type_id).isStringOrFixedString())
{
if (from_type->getCustomSerialization())
if constexpr (WhichDataType(FromDataType::type_id).isEnum())
{
ret = createEnumToStringWrapper<FromDataType>();
return true;
}
else if (from_type->getCustomSerialization())
{
ret = [](ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, const ColumnNullable *, size_t input_rows_count) -> ColumnPtr
{
Expand Down
4 changes: 4 additions & 0 deletions tests/queries/0_stateless/02910_nullable_enum_cast.reference
@@ -0,0 +1,4 @@
\N
\N
A
A
4 changes: 4 additions & 0 deletions tests/queries/0_stateless/02910_nullable_enum_cast.sql
@@ -0,0 +1,4 @@
SELECT CAST(materialize(CAST(NULL, 'Nullable(Enum(\'A\' = 1, \'B\' = 2))')), 'Nullable(String)');
SELECT CAST(CAST(NULL, 'Nullable(Enum(\'A\' = 1, \'B\' = 2))'), 'Nullable(String)');
SELECT CAST(materialize(CAST(1, 'Nullable(Enum(\'A\' = 1, \'B\' = 2))')), 'Nullable(String)');
SELECT CAST(CAST(1, 'Nullable(Enum(\'A\' = 1, \'B\' = 2))'), 'Nullable(String)');