fix multiple joins aliasing for order by and group by

This commit is contained in:
chertus 2019-09-20 19:01:19 +03:00
parent cffc254922
commit 7726130303
4 changed files with 77 additions and 15 deletions

View File

@ -194,14 +194,14 @@ struct ColumnAliasesMatcher
} }
}; };
static bool needChildVisit(ASTPtr & node, const ASTPtr &) static bool needChildVisit(const ASTPtr & node, const ASTPtr &)
{ {
if (node->as<ASTQualifiedAsterisk>()) if (node->as<ASTQualifiedAsterisk>())
return false; return false;
return true; return true;
} }
static void visit(ASTPtr & ast, Data & data) static void visit(const ASTPtr & ast, Data & data)
{ {
if (auto * t = ast->as<ASTIdentifier>()) if (auto * t = ast->as<ASTIdentifier>())
visit(*t, ast, data); visit(*t, ast, data);
@ -210,8 +210,9 @@ struct ColumnAliasesMatcher
throw Exception("Multiple JOIN do not support asterisks for complex queries yet", ErrorCodes::NOT_IMPLEMENTED); throw Exception("Multiple JOIN do not support asterisks for complex queries yet", ErrorCodes::NOT_IMPLEMENTED);
} }
static void visit(ASTIdentifier & node, ASTPtr &, Data & data) static void visit(const ASTIdentifier & const_node, const ASTPtr &, Data & data)
{ {
ASTIdentifier & node = const_cast<ASTIdentifier &>(const_node); /// we know it's not const
if (node.isShort()) if (node.isShort())
return; return;
@ -375,7 +376,7 @@ using RewriteVisitor = InDepthNodeVisitor<RewriteMatcher, true>;
using SetSubqueryAliasMatcher = OneTypeMatcher<SetSubqueryAliasVisitorData>; using SetSubqueryAliasMatcher = OneTypeMatcher<SetSubqueryAliasVisitorData>;
using SetSubqueryAliasVisitor = InDepthNodeVisitor<SetSubqueryAliasMatcher, true>; using SetSubqueryAliasVisitor = InDepthNodeVisitor<SetSubqueryAliasMatcher, true>;
using ExtractAsterisksVisitor = ExtractAsterisksMatcher::Visitor; using ExtractAsterisksVisitor = ExtractAsterisksMatcher::Visitor;
using ColumnAliasesVisitor = InDepthNodeVisitor<ColumnAliasesMatcher, true>; using ColumnAliasesVisitor = ConstInDepthNodeVisitor<ColumnAliasesMatcher, true>;
using AppendSemanticMatcher = OneTypeMatcher<AppendSemanticVisitorData>; using AppendSemanticMatcher = OneTypeMatcher<AppendSemanticVisitorData>;
using AppendSemanticVisitor = InDepthNodeVisitor<AppendSemanticMatcher, true>; using AppendSemanticVisitor = InDepthNodeVisitor<AppendSemanticMatcher, true>;
@ -403,15 +404,19 @@ void JoinToSubqueryTransformMatcher::visit(ASTSelectQuery & select, ASTPtr & ast
if (select.select()) if (select.select())
{ {
aliases_data.public_names = true; aliases_data.public_names = true;
ColumnAliasesVisitor(aliases_data).visit(select.refSelect()); ColumnAliasesVisitor(aliases_data).visit(select.select());
aliases_data.public_names = false; aliases_data.public_names = false;
} }
if (select.where()) if (select.where())
ColumnAliasesVisitor(aliases_data).visit(select.refWhere()); ColumnAliasesVisitor(aliases_data).visit(select.where());
if (select.prewhere()) if (select.prewhere())
ColumnAliasesVisitor(aliases_data).visit(select.refPrewhere()); ColumnAliasesVisitor(aliases_data).visit(select.prewhere());
if (select.orderBy())
ColumnAliasesVisitor(aliases_data).visit(select.orderBy());
if (select.groupBy())
ColumnAliasesVisitor(aliases_data).visit(select.groupBy());
if (select.having()) if (select.having())
ColumnAliasesVisitor(aliases_data).visit(select.refHaving()); ColumnAliasesVisitor(aliases_data).visit(select.having());
/// JOIN sections /// JOIN sections
for (auto & child : select.tables()->children) for (auto & child : select.tables()->children)

View File

@ -16,30 +16,44 @@ left join y on (y.a = s.a and y.b = s.b) format Vertical;
select t.a, s.b, s.a, s.b, y.a, y.b from t select t.a, s.b, s.a, s.b, y.a, y.b from t
left join s on (t.a = s.a and s.b = t.b) left join s on (t.a = s.a and s.b = t.b)
left join y on (y.a = s.a and y.b = s.b) format PrettyCompactNoEscapes; left join y on (y.a = s.a and y.b = s.b)
order by t.a
format PrettyCompactNoEscapes;
select t.a as t_a from t select t.a as t_a from t
left join s on s.a = t_a format PrettyCompactNoEscapes; left join s on s.a = t_a
order by t.a
format PrettyCompactNoEscapes;
select t.a, s.a as s_a from t select t.a, s.a as s_a from t
left join s on s.a = t.a left join s on s.a = t.a
left join y on y.b = s.b format PrettyCompactNoEscapes; left join y on y.b = s.b
order by t.a
format PrettyCompactNoEscapes;
select t.a, t.a, t.b as t_b from t select t.a, t.a, t.b as t_b from t
left join s on t.a = s.a left join s on t.a = s.a
left join y on y.b = s.b format PrettyCompactNoEscapes; left join y on y.b = s.b
order by t.a
format PrettyCompactNoEscapes;
select s.a, s.a, s.b as s_b, s.b from t select s.a, s.a, s.b as s_b, s.b from t
left join s on s.a = t.a left join s on s.a = t.a
left join y on s.b = y.b format PrettyCompactNoEscapes; left join y on s.b = y.b
order by t.a
format PrettyCompactNoEscapes;
select y.a, y.a, y.b as y_b, y.b from t select y.a, y.a, y.b as y_b, y.b from t
left join s on s.a = t.a left join s on s.a = t.a
left join y on y.b = s.b format PrettyCompactNoEscapes; left join y on y.b = s.b
order by t.a
format PrettyCompactNoEscapes;
select t.a, t.a as t_a, s.a, s.a as s_a, y.a, y.a as y_a from t select t.a, t.a as t_a, s.a, s.a as s_a, y.a, y.a as y_a from t
left join s on t.a = s.a left join s on t.a = s.a
left join y on y.b = s.b format PrettyCompactNoEscapes; left join y on y.b = s.b
order by t.a
format PrettyCompactNoEscapes;
drop table t; drop table t;
drop table s; drop table s;

View File

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

View File

@ -0,0 +1,35 @@
drop table if exists t;
drop table if exists s;
drop table if exists y;
create table t(a Int64, b Int64) engine = Memory;
create table s(a Int64, b Int64) engine = Memory;
create table y(a Int64, b Int64) engine = Memory;
insert into t values (1,1), (2,2);
insert into s values (1,1);
insert into y values (1,1);
select s.a, s.a, s.b as s_b, s.b from t
left join s on s.a = t.a
left join y on s.b = y.b
order by t.a;
select max(s.a) from t
left join s on s.a = t.a
left join y on s.b = y.b
group by t.a;
select t.a, t.a as t_a, s.a, s.a as s_a, y.a, y.a as y_a from t
left join s on t.a = s.a
left join y on y.b = s.b
order by t.a;
select t.a, t.a as t_a, max(s.a) from t
left join s on t.a = s.a
left join y on y.b = s.b
group by t.a;
drop table t;
drop table s;
drop table y;