Skip to content

Commit

Permalink
fix crash in JoinToSubqueryTransformVisitor (wrong casts)
Browse files Browse the repository at this point in the history
  • Loading branch information
4ertus2 committed Dec 28, 2018
1 parent d16447c commit bae0370
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
21 changes: 11 additions & 10 deletions dbms/src/Interpreters/JoinToSubqueryTransformVisitor.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <Common/typeid_cast.h>
#include <Interpreters/JoinToSubqueryTransformVisitor.h>
#include <Interpreters/SemanticSelectQuery.h>
#include <Parsers/ASTSelectQuery.h>
Expand Down Expand Up @@ -57,7 +58,7 @@ struct RewriteTablesVisitorData

static bool needRewrite(ASTSelectQuery & select)
{
auto tables = static_cast<const ASTTablesInSelectQuery *>(select.tables.get());
auto tables = typeid_cast<const ASTTablesInSelectQuery *>(select.tables.get());
if (!tables)
return false;

Expand All @@ -70,22 +71,22 @@ static bool needRewrite(ASTSelectQuery & select)

static void appendTableNameAndAlias(std::vector<String> & hidden, const ASTPtr & table_element)
{
auto element = static_cast<const ASTTablesInSelectQueryElement *>(table_element.get());
auto element = typeid_cast<const ASTTablesInSelectQueryElement *>(table_element.get());
if (!element || element->children.empty())
throw Exception("Expected TablesInSelectQueryElement with at least one child", ErrorCodes::LOGICAL_ERROR);

auto table_expression = static_cast<const ASTTableExpression *>(element->children[0].get());
auto table_expression = typeid_cast<const ASTTableExpression *>(element->children[0].get());
if (!table_expression || table_expression->children.empty())
throw Exception("Expected TableExpression with at least one child", ErrorCodes::LOGICAL_ERROR);

String alias = table_expression->children[0]->tryGetAlias();
if (!alias.empty())
hidden.push_back(alias);

auto identifier = static_cast<const ASTIdentifier *>(table_expression->children[0].get());
if (!identifier && alias.empty())
if (auto * identifier = typeid_cast<const ASTIdentifier *>(table_expression->children[0].get()))
hidden.push_back(identifier->name);
else if (alias.empty())
throw Exception("Expected Identifier or subquery with alias", ErrorCodes::LOGICAL_ERROR);
hidden.push_back(identifier->name);
}


Expand All @@ -103,7 +104,7 @@ void JoinToSubqueryTransformMatcher::visit(ASTSelectQuery & select, ASTPtr & ast
if (!needRewrite(select))
return;

auto tables = static_cast<const ASTTablesInSelectQuery *>(select.tables.get());
auto tables = typeid_cast<const ASTTablesInSelectQuery *>(select.tables.get());
if (!tables)
throw Exception("TablesInSelectQuery expected", ErrorCodes::LOGICAL_ERROR);

Expand Down Expand Up @@ -141,15 +142,15 @@ ASTPtr JoinToSubqueryTransformMatcher::replaceJoin(ASTSelectQuery & select, ASTP
OneTypeMatcher<AppendSemanticVisitorData>>;
using RewriteVisitor = InDepthNodeVisitor<RewriteMatcher, true>;

auto left = static_cast<const ASTTablesInSelectQueryElement *>(ast_left.get());
auto right = static_cast<const ASTTablesInSelectQueryElement *>(ast_right.get());
auto left = typeid_cast<const ASTTablesInSelectQueryElement *>(ast_left.get());
auto right = typeid_cast<const ASTTablesInSelectQueryElement *>(ast_right.get());
if (!left || !right)
throw Exception("Two TablesInSelectQueryElements expected", ErrorCodes::LOGICAL_ERROR);

if (!right->table_join || right->array_join)
return {};

auto table_join = static_cast<const ASTTableJoin *>(right->table_join.get());
auto table_join = typeid_cast<const ASTTableJoin *>(right->table_join.get());
if (table_join->kind != ASTTableJoin::Kind::Inner)
return {};

Expand Down
6 changes: 1 addition & 5 deletions dbms/tests/queries/0_stateless/00820_multiple_joins.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ SET allow_experimental_multiple_joins_emulation = 1;
-- FIXME: wrong names qualification
select a, b, c from table1 as t1 join table2 as t2 on t1.a = t2.a join table3 as t3 on b = t3.b;
select a, b, c from table1 as t1 join table2 as t2 on t1.a = t2.a join table5 as t5 on a = t5.a AND b = t5.b;
--select a, b, c, d from table1 as t1 join table2 as t2 on t1.a = t2.a join table3 as t3 on b = t3.b join table5 as t5 on c = t5.c;

--select t1.x, t2.y, t3.z from table1 as t1 join table2 as t2 on t1.x = t2.x join table3 as t3 on t2.y = t3.y;
--select t1.x, t2.y, t3.z from table1 as t1 join (select * from table2 as t2 join table3 as t3 on t2.y = t3.y) on t1.x = t2.x;
--select t1.x, j1.y, j1.z from table1 as t1 join (select * from table2 as t2 join table3 as t3 on t2.y = t3.y) as j1 on t1.x = j1.x;
--select a, b, c from table1 as t1 join table2 as t2 on t1.a = t2.a join table3 as t3 on b = t3.b join table5 as t5 on c = t5.c;

DROP TABLE table1;
DROP TABLE table2;
Expand Down

0 comments on commit bae0370

Please sign in to comment.