2017-04-21 12:39:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Parsers/IParser.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-04-22 11:31:39 +00:00
|
|
|
/// Parameters for rewriting queries without ON CLUSTER, see getRewrittenASTWithoutOnCluster().
|
|
|
|
struct WithoutOnClusterASTRewriteParams
|
|
|
|
{
|
|
|
|
/// Default database from the cluster's configuration.
|
|
|
|
String default_database;
|
|
|
|
|
2022-05-01 13:36:32 +00:00
|
|
|
/// The ID of the current host in the format.
|
|
|
|
String host_id;
|
2022-04-22 11:31:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-04-21 12:39:28 +00:00
|
|
|
/// TODO: Quite messy.
|
|
|
|
class ASTQueryWithOnCluster
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Pos = IParser::Pos;
|
|
|
|
|
|
|
|
/// Should be parsed from ON CLUSTER <cluster> clause
|
|
|
|
String cluster;
|
|
|
|
|
2017-06-18 03:07:03 +00:00
|
|
|
/// new_database should be used by queries that refer to default db
|
2017-04-21 12:39:28 +00:00
|
|
|
/// and default_database is specified for remote server
|
2022-04-22 11:31:39 +00:00
|
|
|
virtual ASTPtr getRewrittenASTWithoutOnCluster(const WithoutOnClusterASTRewriteParams & params = {}) const = 0; /// NOLINT
|
2017-04-21 12:39:28 +00:00
|
|
|
|
|
|
|
/// Returns a query prepared for execution on remote server
|
2022-04-22 11:31:39 +00:00
|
|
|
std::string getRewrittenQueryWithoutOnCluster(const WithoutOnClusterASTRewriteParams & params = {}) const;
|
2017-04-21 12:39:28 +00:00
|
|
|
|
|
|
|
void formatOnCluster(const IAST::FormatSettings & settings) const;
|
|
|
|
|
|
|
|
/// Parses " CLUSTER [cluster|'cluster'] " clause
|
2017-07-10 03:28:12 +00:00
|
|
|
static bool parse(Pos & pos, std::string & cluster_str, Expected & expected);
|
2017-04-21 12:39:28 +00:00
|
|
|
|
|
|
|
virtual ~ASTQueryWithOnCluster() = default;
|
2019-01-04 12:10:00 +00:00
|
|
|
ASTQueryWithOnCluster() = default;
|
|
|
|
ASTQueryWithOnCluster(const ASTQueryWithOnCluster &) = default;
|
|
|
|
ASTQueryWithOnCluster & operator=(const ASTQueryWithOnCluster &) = default;
|
2018-10-24 15:31:07 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
template <typename T>
|
|
|
|
static ASTPtr removeOnCluster(ASTPtr query_ptr, const std::string & new_database)
|
|
|
|
{
|
|
|
|
T & query = static_cast<T &>(*query_ptr);
|
|
|
|
|
2021-09-06 22:13:54 +00:00
|
|
|
query.cluster.clear();
|
2021-11-11 13:28:18 +00:00
|
|
|
if (!query.database)
|
2021-09-06 22:13:54 +00:00
|
|
|
query.setDatabase(new_database);
|
|
|
|
|
|
|
|
return query_ptr;
|
|
|
|
}
|
|
|
|
|
2018-10-24 15:31:07 +00:00
|
|
|
template <typename T>
|
|
|
|
static ASTPtr removeOnCluster(ASTPtr query_ptr)
|
|
|
|
{
|
|
|
|
T & query = static_cast<T &>(*query_ptr);
|
|
|
|
query.cluster.clear();
|
|
|
|
return query_ptr;
|
|
|
|
}
|
2017-04-21 12:39:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|