Merge pull request #72454 from Avogar/fix-empty-variant

Don't allow creating empty Variant
This commit is contained in:
Pavel Kruglov 2024-12-05 18:21:56 +00:00 committed by GitHub
commit 03bc631cf2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 14 additions and 0 deletions

View File

@ -43,6 +43,9 @@ DataTypeVariant::DataTypeVariant(const DataTypes & variants_)
for (const auto & [_, type] : name_to_type)
variants.push_back(type);
if (variants.empty())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Variant type should have at least one nested type");
if (variants.size() > ColumnVariant::MAX_NESTED_COLUMNS)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Variant type with more than {} nested types is not allowed", ColumnVariant::MAX_NESTED_COLUMNS);
}

View File

@ -4881,6 +4881,14 @@ private:
if (const auto * variant_type = typeid_cast<const DataTypeVariant *>(from_type.get()))
return createVariantToDynamicWrapper(*variant_type, dynamic_type);
if (from_type->onlyNull())
return [](ColumnsWithTypeAndName &, const DataTypePtr & result_type, const ColumnNullable *, size_t input_rows_count) -> ColumnPtr
{
auto result = result_type->createColumn();
result->insertManyDefaults(input_rows_count);
return result;
};
if (context && context->getSettingsRef()[Setting::cast_string_to_dynamic_use_inference] && isStringOrFixedString(removeNullable(removeLowCardinality(from_type))))
return createStringToDynamicThroughParsingWrapper();

View File

@ -0,0 +1,3 @@
set allow_experimental_variant_type=1;
create table test (v Variant()) engine=Variant(); -- {serverError BAD_ARGUMENTS}