1#include <Parsers/IAST.h>
2#include <Parsers/ASTIdentifier.h>
3#include <Parsers/ASTLiteral.h>
4#include <Parsers/ASTFunction.h>
5#include <Common/typeid_cast.h>
6
7#include <Interpreters/getClusterName.h>
8
9
10namespace DB
11{
12
13namespace ErrorCodes
14{
15 extern const int BAD_ARGUMENTS;
16}
17
18
19std::string getClusterName(const IAST & node)
20{
21 if (const auto * ast_id = node.as<ASTIdentifier>())
22 return ast_id->name;
23
24 if (const auto * ast_lit = node.as<ASTLiteral>())
25 return ast_lit->value.safeGet<String>();
26
27 /// A hack to support hyphens in cluster names.
28 if (const auto * ast_func = node.as<ASTFunction>())
29 {
30 if (ast_func->name != "minus" || !ast_func->arguments || ast_func->arguments->children.size() < 2)
31 throw Exception("Illegal expression instead of cluster name.", ErrorCodes::BAD_ARGUMENTS);
32
33 String name;
34 for (const auto & arg : ast_func->arguments->children)
35 {
36 if (name.empty())
37 name += getClusterName(*arg);
38 else
39 name += "-" + getClusterName(*arg);
40 }
41
42 return name;
43 }
44
45 throw Exception("Illegal expression instead of cluster name.", ErrorCodes::BAD_ARGUMENTS);
46}
47
48}
49