Skip to content

Commit

Permalink
FLASH-451: Let qualified column reference not resolve to alias (#209)
Browse files Browse the repository at this point in the history
* Let qualified column reference not resolve to alias, partial based on ClickHouse #4351

* Add alias test

* Address comments
  • Loading branch information
zanmato1984 authored Aug 29, 2019
1 parent 9512e10 commit 6536d62
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 3 additions & 1 deletion dbms/src/Interpreters/ExpressionAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ void ExpressionAnalyzer::translateQualifiedNamesImpl(ASTPtr & ast, const String
{
String node_alias = ast->tryGetAlias();
ast = ast->children.back();
static_cast<ASTIdentifier &>(*ast.get()).can_be_alias = false;
if (!node_alias.empty())
ast->setAlias(node_alias);
}
Expand All @@ -356,6 +357,7 @@ void ExpressionAnalyzer::translateQualifiedNamesImpl(ASTPtr & ast, const String
new_name += '.';
new_name += static_cast<const ASTIdentifier &>(*child.get()).name;
}
ident->can_be_alias = false;
ident->name = new_name;
}
}
Expand Down Expand Up @@ -1007,7 +1009,7 @@ void ExpressionAnalyzer::normalizeTreeImpl(
}
else if ((identifier_node = typeid_cast<ASTIdentifier *>(ast.get())))
{
if (identifier_node->kind == ASTIdentifier::Column)
if (identifier_node->kind == ASTIdentifier::Column && identifier_node->can_be_alias)
{
/// If it is an alias, but not a parent alias (for constructs like "SELECT column + 1 AS column").
auto it_alias = aliases.find(identifier_node->name);
Expand Down
5 changes: 4 additions & 1 deletion dbms/src/Parsers/ASTIdentifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ class ASTIdentifier : public ASTWithAlias
/// what this identifier identifies
Kind kind;

/// if it's a cropped name it could not be an alias
bool can_be_alias;

ASTIdentifier(const String & name_, const Kind kind_ = Column)
: name(name_), kind(kind_) {}
: name(name_), kind(kind_), can_be_alias(true) {}

/** Get the text that identifies this element. */
String getID() const override { return "Identifier_" + name; }
Expand Down
17 changes: 17 additions & 0 deletions tests/mutable-test/bugs/flash-451.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
>> DBGInvoke __enable_schema_sync_service('false')

# Unqualified identifiers resolve to alias in select list.
>> select 0 as c, c + 1 as c1 from (select 1 as c) as t where c = 1;

# Qualified identifiers resolve to column in table.
>> select 0 as c, t.c + 1 as c1 from (select 1 as c) as t where t.c = 1;
┌─c─┬─c1─┐
│ 0 │ 2 │
└───┴────┘

# Combine the above two.
>> select 0 as c, c + 1 as c1 from (select 1 as c) as t where t.c = 1;
┌─c─┬─c1─┐
│ 0 │ 1 │
└───┴────┘

0 comments on commit 6536d62

Please sign in to comment.