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;