From 650427614f1dbf65ca2bc089816f1b3d32768fd9 Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Mon, 13 Mar 2023 17:57:07 +0000 Subject: [PATCH] Fix block structure mismatch for nullable LowCardinality column with default expression. --- src/Interpreters/inplaceBlockConversions.cpp | 8 +++++++- .../0_stateless/02680_lc_null_as_default.reference | 0 tests/queries/0_stateless/02680_lc_null_as_default.sql | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/queries/0_stateless/02680_lc_null_as_default.reference create mode 100644 tests/queries/0_stateless/02680_lc_null_as_default.sql diff --git a/src/Interpreters/inplaceBlockConversions.cpp b/src/Interpreters/inplaceBlockConversions.cpp index 55d2f7ce5a8..5bbd2667f55 100644 --- a/src/Interpreters/inplaceBlockConversions.cpp +++ b/src/Interpreters/inplaceBlockConversions.cpp @@ -61,11 +61,17 @@ void addDefaultRequiredExpressionsRecursively( RequiredSourceColumnsVisitor::Data columns_context; RequiredSourceColumnsVisitor(columns_context).visit(column_default_expr); NameSet required_columns_names = columns_context.requiredColumns(); + auto required_type = std::make_shared(columns.get(required_column_name).type->getName()); - auto expr = makeASTFunction("_CAST", column_default_expr, std::make_shared(columns.get(required_column_name).type->getName())); + auto expr = makeASTFunction("_CAST", column_default_expr, required_type); if (is_column_in_query && convert_null_to_default) + { expr = makeASTFunction("ifNull", std::make_shared(required_column_name), std::move(expr)); + /// ifNull does not respect LowCardinality. + /// It may be fixed later or re-implemented properly for identical types. + expr = makeASTFunction("_CAST", std::move(expr), required_type); + } default_expr_list_accum->children.emplace_back(setAlias(expr, required_column_name)); added_columns.emplace(required_column_name); diff --git a/tests/queries/0_stateless/02680_lc_null_as_default.reference b/tests/queries/0_stateless/02680_lc_null_as_default.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/02680_lc_null_as_default.sql b/tests/queries/0_stateless/02680_lc_null_as_default.sql new file mode 100644 index 00000000000..f6bfad37771 --- /dev/null +++ b/tests/queries/0_stateless/02680_lc_null_as_default.sql @@ -0,0 +1,6 @@ +drop table if exists test_null_as_default__fuzz_46; +SET allow_suspicious_low_cardinality_types = 1; +CREATE TABLE test_null_as_default__fuzz_46 (a Nullable(DateTime64(3)), b LowCardinality(Float32) DEFAULT a + 1000) ENGINE = Memory; +INSERT INTO test_null_as_default__fuzz_46 SELECT 1, NULL UNION ALL SELECT 2, NULL; +drop table test_null_as_default__fuzz_46; +