diff --git a/dbms/src/Interpreters/ExpressionAnalyzer.cpp b/dbms/src/Interpreters/ExpressionAnalyzer.cpp index bce55a70a89..9532f6678ac 100644 --- a/dbms/src/Interpreters/ExpressionAnalyzer.cpp +++ b/dbms/src/Interpreters/ExpressionAnalyzer.cpp @@ -1009,8 +1009,10 @@ void ExpressionAnalyzer::normalizeTreeImpl( /// rewrite rules that act when you go from top to bottom. bool replaced = false; - ASTFunction * func_node = typeid_cast(ast.get()); - if (func_node) + ASTIdentifier * identifier_node = nullptr; + ASTFunction * func_node = nullptr; + + if ((func_node = typeid_cast(ast.get()))) { /** Is there a column in the table whose name fully matches the function entry? * For example, in the table there is a column "domain(URL)", and we requested domain(URL). @@ -1048,13 +1050,13 @@ void ExpressionAnalyzer::normalizeTreeImpl( } } } - else if (ASTIdentifier * node = typeid_cast(ast.get())) + else if ((identifier_node = typeid_cast(ast.get()))) { - if (node->kind == ASTIdentifier::Column) + if (identifier_node->kind == ASTIdentifier::Column) { /// If it is an alias, but not a parent alias (for constructs like "SELECT column + 1 AS column"). - Aliases::const_iterator jt = aliases.find(node->name); - if (jt != aliases.end() && current_alias != node->name) + Aliases::const_iterator jt = aliases.find(identifier_node->name); + if (jt != aliases.end() && current_alias != identifier_node->name) { /// Let's replace it with the corresponding tree node. if (current_asts.count(jt->second.get())) @@ -1116,7 +1118,7 @@ void ExpressionAnalyzer::normalizeTreeImpl( return; } - /// Recurring calls. Don't go into subqueries. + /// Recurring calls. Don't go into subqueries. Don't go into components of compound identifiers. /// We also do not go to the left argument of lambda expressions, so as not to replace the formal parameters /// on aliases in expressions of the form 123 AS x, arrayMap(x -> 1, [2]). @@ -1134,6 +1136,9 @@ void ExpressionAnalyzer::normalizeTreeImpl( normalizeTreeImpl(child, finished_asts, current_asts, current_alias, level + 1); } } + else if (identifier_node) + { + } else { for (auto & child : ast->children) diff --git a/dbms/tests/queries/0_stateless/00494_alias_substitution_bug.reference b/dbms/tests/queries/0_stateless/00494_alias_substitution_bug.reference new file mode 100644 index 00000000000..6ed281c757a --- /dev/null +++ b/dbms/tests/queries/0_stateless/00494_alias_substitution_bug.reference @@ -0,0 +1,2 @@ +1 +1 diff --git a/dbms/tests/queries/0_stateless/00494_alias_substitution_bug.sql b/dbms/tests/queries/0_stateless/00494_alias_substitution_bug.sql new file mode 100644 index 00000000000..81c4b1a39a4 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00494_alias_substitution_bug.sql @@ -0,0 +1,5 @@ +DROP TABLE IF EXISTS test.nested; +CREATE TABLE test.nested (n Nested(x UInt8)) ENGINE = Memory; +INSERT INTO test.nested VALUES ([1, 2]); +SELECT 1 AS x FROM remote('127.0.0.1', test.nested) ARRAY JOIN n.x; +DROP TABLE test.nested;