diff --git a/src/DataTypes/DataTypeVariant.cpp b/src/DataTypes/DataTypeVariant.cpp index cc8d04e94da..3ec1c994f5e 100644 --- a/src/DataTypes/DataTypeVariant.cpp +++ b/src/DataTypes/DataTypeVariant.cpp @@ -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); } diff --git a/src/Functions/FunctionsConversion.h b/src/Functions/FunctionsConversion.h index e622827e120..582228744e4 100644 --- a/src/Functions/FunctionsConversion.h +++ b/src/Functions/FunctionsConversion.h @@ -4881,6 +4881,14 @@ private: if (const auto * variant_type = typeid_cast(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(); diff --git a/tests/queries/0_stateless/03276_empty_variant_type.reference b/tests/queries/0_stateless/03276_empty_variant_type.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/03276_empty_variant_type.sql b/tests/queries/0_stateless/03276_empty_variant_type.sql new file mode 100644 index 00000000000..b87ebbee764 --- /dev/null +++ b/tests/queries/0_stateless/03276_empty_variant_type.sql @@ -0,0 +1,3 @@ +set allow_experimental_variant_type=1; +create table test (v Variant()) engine=Variant(); -- {serverError BAD_ARGUMENTS} +