2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
2022-06-23 20:04:06 +00:00
|
|
|
#include <Storages/checkAndGetLiteralArgument.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#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
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-13 03:22:16 +00:00
|
|
|
std::string getClusterName(const IAST & node)
|
2022-12-10 23:43:42 +00:00
|
|
|
{
|
|
|
|
auto name = tryGetClusterName(node);
|
|
|
|
if (!name)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Illegal expression instead of cluster name.");
|
2022-12-10 23:43:42 +00:00
|
|
|
return std::move(name).value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::optional<std::string> tryGetClusterName(const IAST & node)
|
2016-05-13 03:22:16 +00:00
|
|
|
{
|
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>())
|
2022-12-10 23:43:42 +00:00
|
|
|
{
|
|
|
|
if (ast_lit->value.getType() != Field::Types::String)
|
|
|
|
return {};
|
|
|
|
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>())
|
2016-05-13 03:22:16 +00:00
|
|
|
{
|
2019-01-18 16:30:35 +00:00
|
|
|
if (ast_func->name != "minus" || !ast_func->arguments || ast_func->arguments->children.size() < 2)
|
2022-12-10 23:43:42 +00:00
|
|
|
return {};
|
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;
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 23:43:42 +00:00
|
|
|
return {};
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 13:44:44 +00:00
|
|
|
|
2021-01-26 18:45:36 +00:00
|
|
|
std::string getClusterNameAndMakeLiteral(ASTPtr & node)
|
2020-02-21 13:44:44 +00:00
|
|
|
{
|
|
|
|
String cluster_name = getClusterName(*node);
|
|
|
|
node = std::make_shared<ASTLiteral>(cluster_name);
|
|
|
|
return cluster_name;
|
|
|
|
}
|
|
|
|
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|