2018-09-12 05:41:09 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-11-07 12:55:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2018-10-09 14:32:11 +00:00
|
|
|
#include <Parsers/DumpASTNode.h>
|
2018-09-12 05:41:09 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-11-02 15:09:15 +00:00
|
|
|
class ASTSelectWithUnionQuery;
|
|
|
|
class ASTSubquery;
|
|
|
|
struct ASTTableExpression;
|
|
|
|
struct ASTArrayJoin;
|
|
|
|
|
2018-09-12 05:41:09 +00:00
|
|
|
using Aliases = std::unordered_map<String, ASTPtr>;
|
|
|
|
|
2018-11-06 13:28:22 +00:00
|
|
|
/// Visitors consist of functions with unified interface 'void visit(Casted & x, ASTPtr & y)', there x is y, successfully casted to Casted.
|
|
|
|
/// Both types and fuction could have const specifiers. The second argument is used by visitor to replaces AST node (y) if needed.
|
2018-11-02 15:09:15 +00:00
|
|
|
|
2018-10-09 14:32:11 +00:00
|
|
|
/// Visits AST nodes and collect their aliases in one map (with links to source nodes).
|
2018-09-27 19:25:18 +00:00
|
|
|
class QueryAliasesVisitor
|
|
|
|
{
|
|
|
|
public:
|
2018-11-02 15:09:15 +00:00
|
|
|
QueryAliasesVisitor(Aliases & aliases_, std::ostream * ostr_ = nullptr)
|
|
|
|
: aliases(aliases_),
|
|
|
|
visit_depth(0),
|
2018-09-27 19:25:18 +00:00
|
|
|
ostr(ostr_)
|
|
|
|
{}
|
|
|
|
|
2018-11-02 15:09:15 +00:00
|
|
|
void visit(const ASTPtr & ast) const;
|
2018-09-27 19:25:18 +00:00
|
|
|
|
|
|
|
private:
|
2018-11-02 15:09:15 +00:00
|
|
|
Aliases & aliases;
|
2018-09-27 19:25:18 +00:00
|
|
|
mutable size_t visit_depth;
|
|
|
|
std::ostream * ostr;
|
|
|
|
|
2018-11-02 15:09:15 +00:00
|
|
|
void visit(const ASTTableExpression &, const ASTPtr & ) const {}
|
|
|
|
void visit(const ASTSelectWithUnionQuery &, const ASTPtr & ) const {}
|
|
|
|
|
|
|
|
void visit(ASTSubquery & subquery, const ASTPtr & ast) const;
|
|
|
|
void visit(const ASTArrayJoin &, const ASTPtr & ast) const;
|
|
|
|
void visitOther(const ASTPtr & ast) const;
|
|
|
|
void visitChildren(const ASTPtr & ast) const;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool tryVisit(const ASTPtr & ast) const
|
|
|
|
{
|
|
|
|
if (T * t = typeid_cast<T *>(ast.get()))
|
|
|
|
{
|
|
|
|
DumpASTNode dump(*ast, ostr, visit_depth, "getQueryAliases");
|
|
|
|
visit(*t, ast);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String wrongAliasMessage(const ASTPtr & ast, const String & alias) const;
|
2018-09-27 19:25:18 +00:00
|
|
|
};
|
2018-09-12 05:41:09 +00:00
|
|
|
|
|
|
|
}
|