2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/getClusterName.h>
|
2016-05-13 03:22:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-01-06 17:41:19 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2017-01-06 17:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-13 03:22:16 +00:00
|
|
|
std::string getClusterName(const IAST & node)
|
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
if (const auto * ast_id = node.as<ASTIdentifier>())
|
2020-10-24 18:46:10 +00:00
|
|
|
return ast_id->name();
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2019-03-11 13:22:51 +00:00
|
|
|
if (const auto * ast_lit = node.as<ASTLiteral>())
|
2017-04-01 07:20:54 +00:00
|
|
|
return ast_lit->value.safeGet<String>();
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2019-01-18 16:30:35 +00:00
|
|
|
/// A hack to support hyphens in cluster names.
|
2019-03-11 13:22:51 +00:00
|
|
|
if (const auto * ast_func = node.as<ASTFunction>())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-01-18 16:30:35 +00:00
|
|
|
if (ast_func->name != "minus" || !ast_func->arguments || ast_func->arguments->children.size() < 2)
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal expression instead of cluster name.", ErrorCodes::BAD_ARGUMENTS);
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2019-01-18 16:30:35 +00:00
|
|
|
String name;
|
|
|
|
for (const auto & arg : ast_func->arguments->children)
|
|
|
|
{
|
|
|
|
if (name.empty())
|
|
|
|
name += getClusterName(*arg);
|
|
|
|
else
|
|
|
|
name += "-" + getClusterName(*arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal expression instead of cluster name.", ErrorCodes::BAD_ARGUMENTS);
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 13:44:44 +00:00
|
|
|
|
|
|
|
String getClusterNameAndMakeLiteral(ASTPtr & node)
|
|
|
|
{
|
|
|
|
String cluster_name = getClusterName(*node);
|
|
|
|
node = std::make_shared<ASTLiteral>(cluster_name);
|
|
|
|
return cluster_name;
|
|
|
|
}
|
|
|
|
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|