2019-01-11 16:52:46 +00:00
|
|
|
#include "TableFunctionRemote.h"
|
|
|
|
|
2018-04-09 13:52:39 +00:00
|
|
|
#include <Storages/getStructureOfRemoteTable.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/StorageDistributed.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
|
|
|
#include <Interpreters/Cluster.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2019-01-25 15:42:24 +00:00
|
|
|
#include <Interpreters/IdentifierSemantic.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2019-01-11 16:52:46 +00:00
|
|
|
#include <Common/parseRemoteDescription.h>
|
2017-06-10 09:04:31 +00:00
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
2019-01-17 18:49:01 +00:00
|
|
|
#include <Core/Defines.h>
|
2016-05-13 03:22:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 05:03:28 +00:00
|
|
|
StoragePtr TableFunctionRemote::executeImpl(const ASTPtr & ast_function, const Context & context) const
|
2016-05-13 03:22:16 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTs & args_func = typeid_cast<ASTFunction &>(*ast_function).children;
|
|
|
|
|
|
|
|
if (args_func.size() != 1)
|
2018-02-12 20:27:14 +00:00
|
|
|
throw Exception(help_message, ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
ASTs & args = typeid_cast<ASTExpressionList &>(*args_func.at(0)).children;
|
|
|
|
|
2018-02-12 20:27:14 +00:00
|
|
|
const size_t max_args = is_cluster_function ? 3 : 5;
|
2018-02-12 20:27:14 +00:00
|
|
|
if (args.size() < 2 || args.size() > max_args)
|
|
|
|
throw Exception(help_message, ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-12 20:27:14 +00:00
|
|
|
String cluster_name;
|
|
|
|
String cluster_description;
|
2018-07-25 12:31:47 +00:00
|
|
|
String remote_database;
|
|
|
|
String remote_table;
|
|
|
|
ASTPtr remote_table_function_ptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
String username;
|
|
|
|
String password;
|
|
|
|
|
|
|
|
size_t arg_num = 0;
|
|
|
|
|
|
|
|
auto getStringLiteral = [](const IAST & node, const char * description)
|
|
|
|
{
|
|
|
|
const ASTLiteral * lit = typeid_cast<const ASTLiteral *>(&node);
|
|
|
|
if (!lit)
|
|
|
|
throw Exception(description + String(" must be string literal (in single quotes)."), ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
if (lit->value.getType() != Field::Types::String)
|
|
|
|
throw Exception(description + String(" must be string literal (in single quotes)."), ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
return safeGet<const String &>(lit->value);
|
|
|
|
};
|
|
|
|
|
2018-02-12 20:27:14 +00:00
|
|
|
if (is_cluster_function)
|
|
|
|
{
|
|
|
|
ASTPtr ast_name = evaluateConstantExpressionOrIdentifierAsLiteral(args[arg_num], context);
|
|
|
|
cluster_name = static_cast<const ASTLiteral &>(*ast_name).value.safeGet<const String &>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-14 18:15:04 +00:00
|
|
|
if (!getIdentifierName(args[arg_num], cluster_name))
|
2018-02-12 20:27:14 +00:00
|
|
|
cluster_description = getStringLiteral(*args[arg_num], "Hosts pattern");
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
++arg_num;
|
|
|
|
|
2017-08-18 19:30:55 +00:00
|
|
|
args[arg_num] = evaluateConstantExpressionOrIdentifierAsLiteral(args[arg_num], context);
|
2018-07-27 21:33:30 +00:00
|
|
|
|
|
|
|
const auto function = typeid_cast<const ASTFunction *>(args[arg_num].get());
|
|
|
|
|
|
|
|
if (function && TableFunctionFactory::instance().isTableFunctionName(function->name))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-07-24 13:10:34 +00:00
|
|
|
remote_table_function_ptr = args[arg_num];
|
|
|
|
++arg_num;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-07-27 21:33:30 +00:00
|
|
|
else
|
|
|
|
{
|
2018-07-24 13:10:34 +00:00
|
|
|
remote_database = static_cast<const ASTLiteral &>(*args[arg_num]).value.safeGet<String>();
|
2018-07-27 21:33:30 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
++arg_num;
|
2018-07-24 13:10:34 +00:00
|
|
|
|
|
|
|
size_t dot = remote_database.find('.');
|
|
|
|
if (dot != String::npos)
|
|
|
|
{
|
|
|
|
/// NOTE Bad - do not support identifiers in backquotes.
|
|
|
|
remote_table = remote_database.substr(dot + 1);
|
|
|
|
remote_database = remote_database.substr(0, dot);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (arg_num >= args.size())
|
2018-07-27 21:33:30 +00:00
|
|
|
{
|
2018-07-24 13:10:34 +00:00
|
|
|
throw Exception(help_message, ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2018-07-27 21:33:30 +00:00
|
|
|
}
|
2018-07-24 13:10:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
args[arg_num] = evaluateConstantExpressionOrIdentifierAsLiteral(args[arg_num], context);
|
|
|
|
remote_table = static_cast<const ASTLiteral &>(*args[arg_num]).value.safeGet<String>();
|
|
|
|
++arg_num;
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 20:27:14 +00:00
|
|
|
/// Username and password parameters are prohibited in cluster version of the function
|
|
|
|
if (!is_cluster_function)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-02-12 20:27:14 +00:00
|
|
|
if (arg_num < args.size())
|
|
|
|
{
|
|
|
|
username = getStringLiteral(*args[arg_num], "Username");
|
|
|
|
++arg_num;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
username = "default";
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-12 20:27:14 +00:00
|
|
|
if (arg_num < args.size())
|
|
|
|
{
|
|
|
|
password = getStringLiteral(*args[arg_num], "Password");
|
|
|
|
++arg_num;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_num < args.size())
|
2018-02-12 20:27:14 +00:00
|
|
|
throw Exception(help_message, ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// ExpressionAnalyzer will be created in InterpreterSelectQuery that will meet these `Identifier` when processing the request.
|
|
|
|
/// We need to mark them as the name of the database or table, because the default value is column.
|
2019-01-14 18:15:04 +00:00
|
|
|
for (auto ast : args)
|
|
|
|
setIdentifierSpecial(ast);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-12 20:27:14 +00:00
|
|
|
ClusterPtr cluster;
|
|
|
|
if (!cluster_name.empty())
|
|
|
|
{
|
|
|
|
/// Use an existing cluster from the main config
|
|
|
|
cluster = context.getCluster(cluster_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Create new cluster from the scratch
|
2018-02-12 20:27:14 +00:00
|
|
|
size_t max_addresses = context.getSettingsRef().table_function_remote_max_addresses;
|
2018-12-05 13:24:45 +00:00
|
|
|
std::vector<String> shards = parseRemoteDescription(cluster_description, 0, cluster_description.size(), ',', max_addresses);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-12 20:27:14 +00:00
|
|
|
std::vector<std::vector<String>> names;
|
|
|
|
for (size_t i = 0; i < shards.size(); ++i)
|
2018-12-05 13:24:45 +00:00
|
|
|
names.push_back(parseRemoteDescription(shards[i], 0, shards[i].size(), '|', max_addresses));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-12 20:27:14 +00:00
|
|
|
if (names.empty())
|
|
|
|
throw Exception("Shard list is empty after parsing first argument", ErrorCodes::BAD_ARGUMENTS);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-01-17 17:55:44 +00:00
|
|
|
auto maybe_secure_port = context.getTCPPortSecure();
|
2019-01-17 18:49:01 +00:00
|
|
|
cluster = std::make_shared<Cluster>(context.getSettings(), names, username, password, (secure ? (maybe_secure_port ? *maybe_secure_port : DBMS_DEFAULT_SECURE_PORT) : context.getTCPPort()), false, secure);
|
2018-02-12 20:27:14 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-07-25 12:31:47 +00:00
|
|
|
auto structure_remote_table = getStructureOfRemoteTable(*cluster, remote_database, remote_table, context, remote_table_function_ptr);
|
2018-07-27 21:33:30 +00:00
|
|
|
|
|
|
|
StoragePtr res = remote_table_function_ptr
|
|
|
|
? StorageDistributed::createWithOwnCluster(
|
2018-07-25 12:31:47 +00:00
|
|
|
getName(),
|
|
|
|
structure_remote_table,
|
|
|
|
remote_table_function_ptr,
|
|
|
|
cluster,
|
|
|
|
context)
|
2018-07-25 18:17:54 +00:00
|
|
|
: StorageDistributed::createWithOwnCluster(
|
2018-07-25 12:31:47 +00:00
|
|
|
getName(),
|
|
|
|
structure_remote_table,
|
|
|
|
remote_database,
|
|
|
|
remote_table,
|
|
|
|
cluster,
|
|
|
|
context);
|
|
|
|
|
2017-06-06 17:06:14 +00:00
|
|
|
res->startup();
|
|
|
|
return res;
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:33:30 +00:00
|
|
|
|
2019-01-17 17:55:44 +00:00
|
|
|
TableFunctionRemote::TableFunctionRemote(const std::string & name_, bool secure)
|
|
|
|
: name{name_}, secure{secure}
|
2018-02-12 20:27:14 +00:00
|
|
|
{
|
|
|
|
is_cluster_function = name == "cluster";
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Table function '" << name + "' requires from 2 to " << (is_cluster_function ? 3 : 5) << " parameters"
|
|
|
|
<< ": <addresses pattern or cluster name>, <name of remote database>, <name of remote table>"
|
|
|
|
<< (is_cluster_function ? "" : ", [username, [password]].");
|
|
|
|
help_message = ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-10 09:04:31 +00:00
|
|
|
void registerTableFunctionRemote(TableFunctionFactory & factory)
|
|
|
|
{
|
2018-02-12 20:27:14 +00:00
|
|
|
factory.registerFunction("remote", [] () -> TableFunctionPtr { return std::make_shared<TableFunctionRemote>("remote"); });
|
2019-01-17 17:55:44 +00:00
|
|
|
factory.registerFunction("remoteSecure", [] () -> TableFunctionPtr { return std::make_shared<TableFunctionRemote>("remote", /* secure = */ true); });
|
2018-02-12 20:27:14 +00:00
|
|
|
factory.registerFunction("cluster", [] () -> TableFunctionPtr { return std::make_shared<TableFunctionRemote>("cluster"); });
|
2017-06-10 09:04:31 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|