ClickHouse/dbms/src/Interpreters/ExternalTablesVisitor.h
proller 5678d1ed98 Build fixes (#3545)
* StorageSystemContributors clean

* Fix

* ARM fixes

* Fix arm compile

* fix

* Fix macos?

* Fix includes

* fix

* fix

* Try fix apple build part 1

* Fix identation

* Fix static libc++ in clang

* fix arm build

* better

* fix

* fix

*  better check-include
2018-11-10 23:09:07 +03:00

53 lines
1.2 KiB
C++

#pragma once
#include <Interpreters/Context.h>
#include <Parsers/IAST.h>
#include <Parsers/ASTIdentifier.h>
#include <Common/typeid_cast.h>
namespace DB
{
/// Finds in the query the usage of external tables (as table identifiers). Fills in external_tables.
class ExternalTablesVisitor
{
public:
ExternalTablesVisitor(const Context & context_, Tables & tables)
: context(context_),
external_tables(tables)
{}
void visit(ASTPtr & ast) const
{
/// Traverse from the bottom. Intentionally go into subqueries.
for (auto & child : ast->children)
visit(child);
tryVisit<ASTIdentifier>(ast);
}
private:
const Context & context;
Tables & external_tables;
void visit(const ASTIdentifier & node, ASTPtr &) const
{
if (node.special())
if (StoragePtr external_storage = context.tryGetExternalTable(node.name))
external_tables[node.name] = external_storage;
}
template <typename T>
bool tryVisit(ASTPtr & ast) const
{
if (const T * t = typeid_cast<const T *>(ast.get()))
{
visit(*t, ast);
return true;
}
return false;
}
};
}