Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FLASH-451: Let qualified column reference not resolve to alias #209

Merged
merged 6 commits into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
zanmato1984 marked this conversation as resolved.
Show resolved Hide resolved
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 │
└───┴────┘