Fix bug when default column reference non materialized column

This commit is contained in:
alesapin 2021-07-28 15:04:42 +03:00
parent ccf94f5a76
commit d91380cd71
3 changed files with 28 additions and 0 deletions

View File

@ -53,6 +53,7 @@ void addDefaultRequiredExpressionsRecursively(
NameSet required_columns_names = columns_context.requiredColumns(); NameSet required_columns_names = columns_context.requiredColumns();
auto expr = makeASTFunction("CAST", column_default_expr, std::make_shared<ASTLiteral>(columns.get(required_column_name).type->getName())); auto expr = makeASTFunction("CAST", column_default_expr, std::make_shared<ASTLiteral>(columns.get(required_column_name).type->getName()));
if (is_column_in_query && convert_null_to_default) if (is_column_in_query && convert_null_to_default)
expr = makeASTFunction("ifNull", std::make_shared<ASTIdentifier>(required_column_name), std::move(expr)); expr = makeASTFunction("ifNull", std::make_shared<ASTIdentifier>(required_column_name), std::move(expr));
default_expr_list_accum->children.emplace_back(setAlias(expr, required_column_name)); 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) 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); 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<ASTLiteral>(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) ASTPtr defaultRequiredExpressions(const Block & block, const NamesAndTypesList & required_columns, const ColumnsDescription & columns, bool null_as_default)

View File

@ -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;