Fixed error with alias substitution [#CLICKHOUSE-2986].

This commit is contained in:
Alexey Milovidov 2017-08-16 23:10:25 +03:00
parent 3a86673aea
commit eb88391655
3 changed files with 19 additions and 7 deletions

View File

@ -1009,8 +1009,10 @@ void ExpressionAnalyzer::normalizeTreeImpl(
/// rewrite rules that act when you go from top to bottom. /// rewrite rules that act when you go from top to bottom.
bool replaced = false; bool replaced = false;
ASTFunction * func_node = typeid_cast<ASTFunction *>(ast.get()); ASTIdentifier * identifier_node = nullptr;
if (func_node) ASTFunction * func_node = nullptr;
if ((func_node = typeid_cast<ASTFunction *>(ast.get())))
{ {
/** Is there a column in the table whose name fully matches the function entry? /** 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). * 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<ASTIdentifier *>(ast.get())) else if ((identifier_node = typeid_cast<ASTIdentifier *>(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"). /// 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); Aliases::const_iterator jt = aliases.find(identifier_node->name);
if (jt != aliases.end() && current_alias != node->name) if (jt != aliases.end() && current_alias != identifier_node->name)
{ {
/// Let's replace it with the corresponding tree node. /// Let's replace it with the corresponding tree node.
if (current_asts.count(jt->second.get())) if (current_asts.count(jt->second.get()))
@ -1116,7 +1118,7 @@ void ExpressionAnalyzer::normalizeTreeImpl(
return; 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 /// 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]). /// 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); normalizeTreeImpl(child, finished_asts, current_asts, current_alias, level + 1);
} }
} }
else if (identifier_node)
{
}
else else
{ {
for (auto & child : ast->children) for (auto & child : ast->children)

View File

@ -0,0 +1,2 @@
1
1

View File

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