From d91380cd712bb1fbb1c8e0878f8114329344d7e6 Mon Sep 17 00:00:00 2001 From: alesapin Date: Wed, 28 Jul 2021 15:04:42 +0300 Subject: [PATCH] Fix bug when default column reference non materialized column --- src/Interpreters/inplaceBlockConversions.cpp | 10 ++++++++++ ..._default_from_default_empty_column.reference | 1 + .../02000_default_from_default_empty_column.sql | 17 +++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 tests/queries/0_stateless/02000_default_from_default_empty_column.reference create mode 100644 tests/queries/0_stateless/02000_default_from_default_empty_column.sql diff --git a/src/Interpreters/inplaceBlockConversions.cpp b/src/Interpreters/inplaceBlockConversions.cpp index ff16c7b3ff6..26cf6912bc7 100644 --- a/src/Interpreters/inplaceBlockConversions.cpp +++ b/src/Interpreters/inplaceBlockConversions.cpp @@ -53,6 +53,7 @@ void addDefaultRequiredExpressionsRecursively( NameSet required_columns_names = columns_context.requiredColumns(); auto expr = makeASTFunction("CAST", column_default_expr, std::make_shared(columns.get(required_column_name).type->getName())); + if (is_column_in_query && convert_null_to_default) expr = makeASTFunction("ifNull", std::make_shared(required_column_name), std::move(expr)); default_expr_list_accum->children.emplace_back(setAlias(expr, required_column_name)); @@ -62,6 +63,15 @@ void addDefaultRequiredExpressionsRecursively( for (const auto & next_required_column_name : required_columns_names) addDefaultRequiredExpressionsRecursively(block, next_required_column_name, required_column_type, columns, default_expr_list_accum, added_columns, null_as_default); } + else + { + /// This column is required, but doesn't have default expression, so lets use "default default" + auto column = columns.get(required_column_name); + auto default_value = column.type->getDefault(); + auto default_ast = std::make_shared(default_value); + default_expr_list_accum->children.emplace_back(setAlias(default_ast, required_column_name)); + added_columns.emplace(required_column_name); + } } ASTPtr defaultRequiredExpressions(const Block & block, const NamesAndTypesList & required_columns, const ColumnsDescription & columns, bool null_as_default) diff --git a/tests/queries/0_stateless/02000_default_from_default_empty_column.reference b/tests/queries/0_stateless/02000_default_from_default_empty_column.reference new file mode 100644 index 00000000000..bb48d0eda85 --- /dev/null +++ b/tests/queries/0_stateless/02000_default_from_default_empty_column.reference @@ -0,0 +1 @@ +1 diff --git a/tests/queries/0_stateless/02000_default_from_default_empty_column.sql b/tests/queries/0_stateless/02000_default_from_default_empty_column.sql new file mode 100644 index 00000000000..5ca642628d4 --- /dev/null +++ b/tests/queries/0_stateless/02000_default_from_default_empty_column.sql @@ -0,0 +1,17 @@ +DROP TABLE IF EXISTS test; + +CREATE TABLE test (col Int8) ENGINE=MergeTree ORDER BY tuple() +SETTINGS vertical_merge_algorithm_min_rows_to_activate=1, + vertical_merge_algorithm_min_columns_to_activate=1, + min_bytes_for_wide_part = 0; + + +INSERT INTO test VALUES (1); +ALTER TABLE test ADD COLUMN s1 String; +ALTER TABLE test ADD COLUMN s2 String DEFAULT s1; + +OPTIMIZE TABLE test FINAL; + +SELECT * FROM test; + +DROP TABLE IF EXISTS test;