Merge pull request #7022 from 4ertus2/bugs

Fix "Unknown identifier" in ORDER BY and GROUP BY with Multiple Joins

(cherry picked from commit 2432a68009)
This commit is contained in:
alexey-milovidov 2019-09-20 23:45:51 +03:00 committed by stavrolia
parent 599e63425a
commit d83de5e61f
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>())
return false;
return true;
}
static void visit(ASTPtr & ast, Data & data)
static void visit(const ASTPtr & ast, Data & data)
{
if (auto * t = ast->as<ASTIdentifier>())
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);
}
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())
return;
@ -375,7 +376,7 @@ using RewriteVisitor = InDepthNodeVisitor<RewriteMatcher, true>;
using SetSubqueryAliasMatcher = OneTypeMatcher<SetSubqueryAliasVisitorData>;
using SetSubqueryAliasVisitor = InDepthNodeVisitor<SetSubqueryAliasMatcher, true>;
using ExtractAsterisksVisitor = ExtractAsterisksMatcher::Visitor;
using ColumnAliasesVisitor = InDepthNodeVisitor<ColumnAliasesMatcher, true>;
using ColumnAliasesVisitor = ConstInDepthNodeVisitor<ColumnAliasesMatcher, true>;
using AppendSemanticMatcher = OneTypeMatcher<AppendSemanticVisitorData>;
using AppendSemanticVisitor = InDepthNodeVisitor<AppendSemanticMatcher, true>;
@ -403,15 +404,19 @@ void JoinToSubqueryTransformMatcher::visit(ASTSelectQuery & select, ASTPtr & ast
if (select.select())
{
aliases_data.public_names = true;
ColumnAliasesVisitor(aliases_data).visit(select.refSelect());
ColumnAliasesVisitor(aliases_data).visit(select.select());
aliases_data.public_names = false;
}
if (select.where())
ColumnAliasesVisitor(aliases_data).visit(select.refWhere());
ColumnAliasesVisitor(aliases_data).visit(select.where());
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())
ColumnAliasesVisitor(aliases_data).visit(select.refHaving());
ColumnAliasesVisitor(aliases_data).visit(select.having());
/// JOIN sections
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
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
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
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
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
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
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
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 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;