diff --git a/dbms/src/Interpreters/inplaceBlockConversions.cpp b/dbms/src/Interpreters/inplaceBlockConversions.cpp index a60efb1e397..38f961306ff 100644 --- a/dbms/src/Interpreters/inplaceBlockConversions.cpp +++ b/dbms/src/Interpreters/inplaceBlockConversions.cpp @@ -37,14 +37,23 @@ ASTPtr defaultRequiredExpressions(Block & block, const NamesAndTypesList & requi { /// expressions must be cloned to prevent modification by the ExpressionAnalyzer auto column_default_expr = it->second.expression->clone(); + + /// Our default may depend on columns with ALIAS as default expr which not present in block + /// we can easily add them from column_defaults struct RequiredSourceColumnsVisitor::Data columns_context; RequiredSourceColumnsVisitor(columns_context).visit(column_default_expr); NameSet required_columns_names = columns_context.requiredColumns(); for (const auto & required_column_name : required_columns_names) + { + /// If we have such default column and it's alias than we should + /// add it into default_expression_list if (auto rit = column_defaults.find(required_column_name); rit != column_defaults.end() && rit->second.kind == ColumnDefaultKind::Alias) + { default_expr_list->children.emplace_back(setAlias(rit->second.expression->clone(), required_column_name)); + } + } auto cast_func = makeASTFunction("CAST", column_default_expr, std::make_shared(column.type->getName())); default_expr_list->children.emplace_back(setAlias(cast_func, it->first)); diff --git a/dbms/tests/queries/0_stateless/01093_cyclic_defaults_filimonov.reference b/dbms/tests/queries/0_stateless/01093_cyclic_defaults_filimonov.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/dbms/tests/queries/0_stateless/01093_cyclic_defaults_filimonov.sql b/dbms/tests/queries/0_stateless/01093_cyclic_defaults_filimonov.sql new file mode 100644 index 00000000000..548cd794ba3 --- /dev/null +++ b/dbms/tests/queries/0_stateless/01093_cyclic_defaults_filimonov.sql @@ -0,0 +1,19 @@ +CREATE TABLE test +( + `a1` UInt64 DEFAULT a + 1, + `a1` UInt64 DEFAULT a + 1, + `a2` UInt64 DEFAULT a3 + a4, + `a3` UInt64 DEFAULT a2 + 1, + `a4` UInt64 ALIAS a3 + 1 +) +ENGINE = Log; -- { serverError 174 } + +CREATE TABLE pythagoras +( + `a` Float64 DEFAULT sqrt((c * c) - (b * b)), + `b` Float64 DEFAULT sqrt((c * c) - (a * a)), + `c` Float64 DEFAULT sqrt((a * a) + (b * b)) +) +ENGINE = Log; -- { serverError 174 } + +-- TODO: It works but should not: CREATE TABLE test (a DEFAULT b, b DEFAULT a) ENGINE = Memory