2018-12-17 16:20:15 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Interpreters/InDepthNodeVisitor.h>
|
2020-03-18 21:38:27 +00:00
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.h>
|
|
|
|
#include <Interpreters/Aliases.h>
|
2018-12-17 16:20:15 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class ASTSelectQuery;
|
2019-04-03 16:06:05 +00:00
|
|
|
class Context;
|
2018-12-17 16:20:15 +00:00
|
|
|
|
|
|
|
/// AST transformer. It replaces multiple joins to (subselect + join) track.
|
2020-06-27 19:05:00 +00:00
|
|
|
/// 'select * from t1 join t2 on ... join t3 on ... join t4 on ...' would be rewritten with
|
2018-12-17 16:20:15 +00:00
|
|
|
/// 'select * from (select * from t1 join t2 on ...) join t3 on ...) join t4 on ...'
|
|
|
|
class JoinToSubqueryTransformMatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct Data
|
|
|
|
{
|
2020-03-18 21:38:27 +00:00
|
|
|
const std::vector<TableWithColumnNamesAndTypes> & tables;
|
|
|
|
const Aliases & aliases;
|
|
|
|
size_t version = 1;
|
2018-12-17 16:20:15 +00:00
|
|
|
bool done = false;
|
|
|
|
};
|
|
|
|
|
2020-03-23 22:23:31 +00:00
|
|
|
static bool needChildVisit(ASTPtr &, const ASTPtr &);
|
2019-02-22 13:33:56 +00:00
|
|
|
static void visit(ASTPtr & ast, Data & data);
|
2018-12-17 16:20:15 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// - combines two source TablesInSelectQueryElement into resulting one (Subquery)
|
|
|
|
/// - adds table hidings to ASTSelectQuery.with_expression_list
|
|
|
|
///
|
|
|
|
/// TablesInSelectQueryElement [result]
|
|
|
|
/// TableExpression
|
|
|
|
/// Subquery (alias __join1)
|
|
|
|
/// SelectWithUnionQuery
|
|
|
|
/// ExpressionList
|
|
|
|
/// SelectQuery
|
|
|
|
/// ExpressionList
|
|
|
|
/// Asterisk
|
|
|
|
/// TablesInSelectQuery
|
|
|
|
/// TablesInSelectQueryElement [source1]
|
|
|
|
/// TablesInSelectQueryElement [source2]
|
|
|
|
///
|
2020-03-18 21:38:27 +00:00
|
|
|
static void visitV1(ASTSelectQuery & select, ASTPtr & ast, Data & data);
|
|
|
|
|
|
|
|
/// V2 uses information about tables' columns to rewrite queries.
|
|
|
|
static void visitV2(ASTSelectQuery & select, ASTPtr & ast, Data & data);
|
2018-12-17 16:20:15 +00:00
|
|
|
|
|
|
|
/// @return combined TablesInSelectQueryElement or nullptr if cannot rewrite
|
2020-03-18 21:38:27 +00:00
|
|
|
static ASTPtr replaceJoin(ASTPtr left, ASTPtr right, ASTPtr subquery_template);
|
2018-12-17 16:20:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using JoinToSubqueryTransformVisitor = InDepthNodeVisitor<JoinToSubqueryTransformMatcher, true>;
|
|
|
|
|
|
|
|
}
|