1#pragma once
2
3#include <Parsers/IAST.h>
4#include <Parsers/IParser.h>
5
6namespace DB
7{
8
9/// TODO: Quite messy.
10class ASTQueryWithOnCluster
11{
12public:
13 using Pos = IParser::Pos;
14
15 /// Should be parsed from ON CLUSTER <cluster> clause
16 String cluster;
17
18 /// new_database should be used by queries that refer to default db
19 /// and default_database is specified for remote server
20 virtual ASTPtr getRewrittenASTWithoutOnCluster(const std::string & new_database = {}) const = 0;
21
22 /// Returns a query prepared for execution on remote server
23 std::string getRewrittenQueryWithoutOnCluster(const std::string & new_database = {}) const;
24
25 void formatOnCluster(const IAST::FormatSettings & settings) const;
26
27 /// Parses " CLUSTER [cluster|'cluster'] " clause
28 static bool parse(Pos & pos, std::string & cluster_str, Expected & expected);
29
30 virtual ~ASTQueryWithOnCluster() = default;
31 ASTQueryWithOnCluster() = default;
32 ASTQueryWithOnCluster(const ASTQueryWithOnCluster &) = default;
33 ASTQueryWithOnCluster & operator=(const ASTQueryWithOnCluster &) = default;
34
35protected:
36 template <typename T>
37 static ASTPtr removeOnCluster(ASTPtr query_ptr, const std::string & new_database)
38 {
39 T & query = static_cast<T &>(*query_ptr);
40
41 query.cluster.clear();
42 if (query.database.empty())
43 query.database = new_database;
44
45 return query_ptr;
46 }
47
48 template <typename T>
49 static ASTPtr removeOnCluster(ASTPtr query_ptr)
50 {
51 T & query = static_cast<T &>(*query_ptr);
52 query.cluster.clear();
53 return query_ptr;
54 }
55};
56
57}
58