2021-03-16 18:41:29 +00:00
|
|
|
#include <Common/config.h>
|
|
|
|
|
|
|
|
#if USE_AWS_S3
|
|
|
|
|
2021-04-12 21:42:52 +00:00
|
|
|
#include <Storages/StorageS3Cluster.h>
|
2022-06-23 20:04:06 +00:00
|
|
|
#include <Storages/StorageS3.h>
|
|
|
|
#include <Storages/checkAndGetLiteralArgument.h>
|
2021-04-06 19:18:45 +00:00
|
|
|
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
2021-03-16 18:41:29 +00:00
|
|
|
#include <IO/S3Common.h>
|
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2021-04-12 17:07:01 +00:00
|
|
|
#include <Interpreters/ClientInfo.h>
|
2021-03-16 18:41:29 +00:00
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
|
|
|
#include <TableFunctions/TableFunctionS3.h>
|
2021-04-12 21:42:52 +00:00
|
|
|
#include <TableFunctions/TableFunctionS3Cluster.h>
|
2021-03-16 18:41:29 +00:00
|
|
|
#include <TableFunctions/parseColumnsListForTableFunction.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
2021-04-12 17:07:01 +00:00
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/IAST_fwd.h>
|
2021-04-12 17:33:55 +00:00
|
|
|
|
|
|
|
#include "registerTableFunctions.h"
|
2021-04-12 17:07:01 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <thread>
|
2021-03-16 18:41:29 +00:00
|
|
|
|
2021-04-22 01:25:40 +00:00
|
|
|
|
2021-03-16 18:41:29 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2022-03-28 22:46:35 +00:00
|
|
|
extern const int BAD_GET;
|
2021-03-16 18:41:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 19:18:45 +00:00
|
|
|
|
2021-04-12 21:42:52 +00:00
|
|
|
void TableFunctionS3Cluster::parseArguments(const ASTPtr & ast_function, ContextPtr context)
|
2021-03-16 18:41:29 +00:00
|
|
|
{
|
|
|
|
/// Parse args
|
|
|
|
ASTs & args_func = ast_function->children;
|
|
|
|
|
|
|
|
if (args_func.size() != 1)
|
2021-04-12 17:33:55 +00:00
|
|
|
throw Exception("Table function '" + getName() + "' must have arguments.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2021-03-16 18:41:29 +00:00
|
|
|
|
|
|
|
ASTs & args = args_func.at(0)->children;
|
|
|
|
|
2022-03-28 22:46:35 +00:00
|
|
|
for (auto & arg : args)
|
|
|
|
arg = evaluateConstantExpressionAsLiteral(arg, context);
|
|
|
|
|
2021-04-06 19:18:45 +00:00
|
|
|
const auto message = fmt::format(
|
|
|
|
"The signature of table function {} could be the following:\n" \
|
2022-03-28 22:46:35 +00:00
|
|
|
" - cluster, url\n"
|
|
|
|
" - cluster, url, format\n" \
|
2021-04-06 19:18:45 +00:00
|
|
|
" - cluster, url, format, structure\n" \
|
2022-03-28 22:46:35 +00:00
|
|
|
" - cluster, url, access_key_id, secret_access_key\n" \
|
2021-04-06 19:18:45 +00:00
|
|
|
" - cluster, url, format, structure, compression_method\n" \
|
2022-03-28 22:46:35 +00:00
|
|
|
" - cluster, url, access_key_id, secret_access_key, format\n"
|
2021-04-06 19:18:45 +00:00
|
|
|
" - cluster, url, access_key_id, secret_access_key, format, structure\n" \
|
|
|
|
" - cluster, url, access_key_id, secret_access_key, format, structure, compression_method",
|
|
|
|
getName());
|
|
|
|
|
2022-03-28 22:46:35 +00:00
|
|
|
if (args.size() < 2 || args.size() > 7)
|
2021-04-06 19:18:45 +00:00
|
|
|
throw Exception(message, ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2021-03-16 18:41:29 +00:00
|
|
|
|
2021-04-13 19:57:01 +00:00
|
|
|
/// This arguments are always the first
|
2022-06-23 20:04:06 +00:00
|
|
|
configuration.cluster_name = checkAndGetLiteralArgument<String>(args[0], "cluster_name");
|
2021-04-13 19:57:01 +00:00
|
|
|
|
2022-03-28 22:46:35 +00:00
|
|
|
if (!context->tryGetCluster(configuration.cluster_name))
|
|
|
|
throw Exception(ErrorCodes::BAD_GET, "Requested cluster '{}' not found", configuration.cluster_name);
|
2021-04-13 19:57:01 +00:00
|
|
|
|
2022-03-28 22:46:35 +00:00
|
|
|
/// Just cut the first arg (cluster_name) and try to parse s3 table function arguments as is
|
|
|
|
ASTs clipped_args;
|
|
|
|
clipped_args.reserve(args.size());
|
|
|
|
std::copy(args.begin() + 1, args.end(), std::back_inserter(clipped_args));
|
2021-04-13 19:57:01 +00:00
|
|
|
|
2022-03-28 22:46:35 +00:00
|
|
|
/// StorageS3ClusterConfiguration inherints from StorageS3Configuration, so it is safe to upcast it.
|
|
|
|
TableFunctionS3::parseArgumentsImpl(message, clipped_args, context, static_cast<StorageS3Configuration & >(configuration));
|
2021-03-16 18:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-12 21:42:52 +00:00
|
|
|
ColumnsDescription TableFunctionS3Cluster::getActualTableStructure(ContextPtr context) const
|
2021-03-16 18:41:29 +00:00
|
|
|
{
|
2022-03-28 22:46:35 +00:00
|
|
|
if (configuration.structure == "auto")
|
|
|
|
{
|
|
|
|
return StorageS3::getTableStructureFromData(
|
|
|
|
configuration.format,
|
|
|
|
S3::URI(Poco::URI(configuration.url)),
|
2022-04-03 22:33:59 +00:00
|
|
|
configuration.auth_settings.access_key_id,
|
|
|
|
configuration.auth_settings.secret_access_key,
|
2022-03-28 22:46:35 +00:00
|
|
|
configuration.compression_method,
|
|
|
|
false,
|
|
|
|
std::nullopt,
|
|
|
|
context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseColumnsListFromString(configuration.structure, context);
|
2021-03-16 18:41:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 21:42:52 +00:00
|
|
|
StoragePtr TableFunctionS3Cluster::executeImpl(
|
2021-04-12 19:35:26 +00:00
|
|
|
const ASTPtr & /*function*/, ContextPtr context,
|
2021-03-19 21:49:18 +00:00
|
|
|
const std::string & table_name, ColumnsDescription /*cached_columns*/) const
|
2021-03-16 18:41:29 +00:00
|
|
|
{
|
2021-04-13 20:17:25 +00:00
|
|
|
StoragePtr storage;
|
2022-03-28 22:46:35 +00:00
|
|
|
|
|
|
|
ColumnsDescription columns;
|
|
|
|
if (configuration.structure != "auto")
|
|
|
|
columns = parseColumnsListFromString(configuration.structure, context);
|
|
|
|
else if (!structure_hint.empty())
|
|
|
|
columns = structure_hint;
|
|
|
|
|
2021-04-13 20:17:25 +00:00
|
|
|
if (context->getClientInfo().query_kind == ClientInfo::QueryKind::SECONDARY_QUERY)
|
|
|
|
{
|
|
|
|
/// On worker node this filename won't contains globs
|
2022-03-28 22:46:35 +00:00
|
|
|
Poco::URI uri (configuration.url);
|
2021-04-13 20:17:25 +00:00
|
|
|
S3::URI s3_uri (uri);
|
2022-04-19 20:47:29 +00:00
|
|
|
storage = std::make_shared<StorageS3>(
|
2021-04-23 12:18:23 +00:00
|
|
|
s3_uri,
|
2022-04-03 22:33:59 +00:00
|
|
|
configuration.auth_settings.access_key_id,
|
|
|
|
configuration.auth_settings.secret_access_key,
|
2021-04-23 12:18:23 +00:00
|
|
|
StorageID(getDatabaseName(), table_name),
|
2022-03-28 22:46:35 +00:00
|
|
|
configuration.format,
|
2022-04-06 20:27:38 +00:00
|
|
|
configuration.rw_settings,
|
2022-03-28 22:46:35 +00:00
|
|
|
columns,
|
2021-04-23 12:18:23 +00:00
|
|
|
ConstraintsDescription{},
|
|
|
|
String{},
|
|
|
|
context,
|
2021-08-26 18:39:06 +00:00
|
|
|
// No format_settings for S3Cluster
|
2021-08-23 19:05:28 +00:00
|
|
|
std::nullopt,
|
2022-03-28 22:46:35 +00:00
|
|
|
configuration.compression_method,
|
2021-04-23 12:18:23 +00:00
|
|
|
/*distributed_processing=*/true);
|
2021-04-13 20:17:25 +00:00
|
|
|
}
|
2021-04-13 20:19:04 +00:00
|
|
|
else
|
|
|
|
{
|
2022-04-19 20:47:29 +00:00
|
|
|
storage = std::make_shared<StorageS3Cluster>(
|
2022-03-28 22:46:35 +00:00
|
|
|
configuration.url,
|
2022-04-03 22:33:59 +00:00
|
|
|
configuration.auth_settings.access_key_id,
|
|
|
|
configuration.auth_settings.secret_access_key,
|
2022-03-28 22:46:35 +00:00
|
|
|
StorageID(getDatabaseName(), table_name),
|
2022-04-06 20:27:38 +00:00
|
|
|
configuration.cluster_name, configuration.format,
|
2022-03-28 22:46:35 +00:00
|
|
|
columns,
|
|
|
|
ConstraintsDescription{},
|
|
|
|
context,
|
|
|
|
configuration.compression_method);
|
2021-04-13 20:17:25 +00:00
|
|
|
}
|
2021-03-16 18:41:29 +00:00
|
|
|
|
|
|
|
storage->startup();
|
|
|
|
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-12 21:42:52 +00:00
|
|
|
void registerTableFunctionS3Cluster(TableFunctionFactory & factory)
|
2021-03-16 18:41:29 +00:00
|
|
|
{
|
2021-04-12 21:42:52 +00:00
|
|
|
factory.registerFunction<TableFunctionS3Cluster>();
|
2021-03-16 18:41:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 19:18:45 +00:00
|
|
|
|
2021-03-16 18:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|