1#pragma once
2
3#include <Interpreters/Context.h>
4#include <Parsers/IAST.h>
5#include <Parsers/ASTIdentifier.h>
6#include <Common/typeid_cast.h>
7#include <Interpreters/InDepthNodeVisitor.h>
8#include <Interpreters/IdentifierSemantic.h>
9
10namespace DB
11{
12
13/// If node is ASTIdentifier try to extract external_storage.
14class ExternalTablesMatcher
15{
16public:
17 struct Data
18 {
19 const Context & context;
20 Tables & external_tables;
21 };
22
23 static void visit(ASTPtr & ast, Data & data)
24 {
25 if (const auto * t = ast->as<ASTIdentifier>())
26 visit(*t, ast, data);
27 }
28
29 static bool needChildVisit(ASTPtr &, const ASTPtr &) { return true; }
30
31private:
32 static void visit(const ASTIdentifier & node, ASTPtr &, Data & data)
33 {
34 if (auto opt_name = IdentifierSemantic::getTableName(node))
35 if (StoragePtr external_storage = data.context.tryGetExternalTable(*opt_name))
36 data.external_tables[*opt_name] = external_storage;
37 }
38};
39
40/// Finds in the query the usage of external tables. Fills in external_tables.
41using ExternalTablesVisitor = InDepthNodeVisitor<ExternalTablesMatcher, false>;
42
43}
44