diff --git a/src/Interpreters/RequiredSourceColumnsVisitor.cpp b/src/Interpreters/RequiredSourceColumnsVisitor.cpp index 5a265b59414..54883043d30 100644 --- a/src/Interpreters/RequiredSourceColumnsVisitor.cpp +++ b/src/Interpreters/RequiredSourceColumnsVisitor.cpp @@ -123,8 +123,12 @@ void RequiredSourceColumnsMatcher::visit(const ASTSelectQuery & select, const AS std::vector out; for (const auto & node : select.children) - if (node != select.select()) + { + // We should not go into WITH statement because all needed aliases are already expanded to + // the right place after normalization. And it might contain unused unknown columns. + if (node != select.select() && node != select.with()) Visitor(data).visit(node); + } /// revisit select_expression_list (with children) when all the aliases are set Visitor(data).visit(select.select());