2021-04-12 17:33:55 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
2021-03-16 18:41:29 +00:00
|
|
|
#include <Common/config.h>
|
2021-04-12 17:33:55 +00:00
|
|
|
#endif
|
2021-03-16 18:41:29 +00:00
|
|
|
|
|
|
|
#if USE_AWS_S3
|
|
|
|
|
2021-04-12 17:07:01 +00:00
|
|
|
#include <Storages/StorageS3Distributed.h>
|
2021-04-06 19:18:45 +00:00
|
|
|
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
2021-04-12 17:07:01 +00:00
|
|
|
#include <DataStreams/RemoteBlockInputStream.h>
|
2021-03-16 18:41:29 +00:00
|
|
|
#include <IO/S3Common.h>
|
|
|
|
#include <Storages/StorageS3.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>
|
|
|
|
#include <TableFunctions/TableFunctionS3Distributed.h>
|
|
|
|
#include <TableFunctions/parseColumnsListForTableFunction.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
2021-04-12 17:07:01 +00:00
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/IAST_fwd.h>
|
|
|
|
#include <Processors/Sources/SourceFromInputStream.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
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2021-04-06 19:18:45 +00:00
|
|
|
|
2021-04-12 19:35:26 +00:00
|
|
|
void TableFunctionS3Distributed::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;
|
|
|
|
|
2021-04-06 19:18:45 +00:00
|
|
|
const auto message = fmt::format(
|
|
|
|
"The signature of table function {} could be the following:\n" \
|
|
|
|
" - cluster, url, format, structure\n" \
|
|
|
|
" - cluster, url, format, structure, compression_method\n" \
|
|
|
|
" - cluster, url, access_key_id, secret_access_key, format, structure\n" \
|
|
|
|
" - cluster, url, access_key_id, secret_access_key, format, structure, compression_method",
|
|
|
|
getName());
|
|
|
|
|
2021-03-19 21:49:18 +00:00
|
|
|
if (args.size() < 4 || 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
|
|
|
|
|
|
|
for (auto & arg : args)
|
|
|
|
arg = evaluateConstantExpressionOrIdentifierAsLiteral(arg, context);
|
|
|
|
|
2021-03-19 21:49:18 +00:00
|
|
|
cluster_name = args[0]->as<ASTLiteral &>().value.safeGet<String>();
|
2021-04-06 19:18:45 +00:00
|
|
|
filename = args[1]->as<ASTLiteral &>().value.safeGet<String>();
|
2021-03-16 18:41:29 +00:00
|
|
|
|
2021-04-06 19:18:45 +00:00
|
|
|
if (args.size() == 4)
|
|
|
|
{
|
|
|
|
format = args[2]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
structure = args[3]->as<ASTLiteral &>().value.safeGet<String>();
|
2021-04-12 19:35:26 +00:00
|
|
|
}
|
2021-04-06 19:18:45 +00:00
|
|
|
else if (args.size() == 5)
|
2021-03-16 18:41:29 +00:00
|
|
|
{
|
2021-03-19 21:49:18 +00:00
|
|
|
format = args[2]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
structure = args[3]->as<ASTLiteral &>().value.safeGet<String>();
|
2021-04-06 19:18:45 +00:00
|
|
|
compression_method = args[4]->as<ASTLiteral &>().value.safeGet<String>();
|
2021-03-16 18:41:29 +00:00
|
|
|
}
|
2021-04-06 19:18:45 +00:00
|
|
|
else if (args.size() == 6)
|
2021-03-16 18:41:29 +00:00
|
|
|
{
|
2021-03-19 21:49:18 +00:00
|
|
|
access_key_id = args[2]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
secret_access_key = args[3]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
format = args[4]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
structure = args[5]->as<ASTLiteral &>().value.safeGet<String>();
|
2021-03-16 18:41:29 +00:00
|
|
|
}
|
2021-04-06 19:18:45 +00:00
|
|
|
else if (args.size() == 7)
|
|
|
|
{
|
|
|
|
access_key_id = args[2]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
secret_access_key = args[3]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
format = args[4]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
structure = args[5]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
compression_method = args[4]->as<ASTLiteral &>().value.safeGet<String>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception(message, ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2021-03-16 18:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-12 19:35:26 +00:00
|
|
|
ColumnsDescription TableFunctionS3Distributed::getActualTableStructure(ContextPtr context) const
|
2021-03-16 18:41:29 +00:00
|
|
|
{
|
|
|
|
return parseColumnsListFromString(structure, context);
|
|
|
|
}
|
|
|
|
|
2021-03-19 21:49:18 +00:00
|
|
|
StoragePtr TableFunctionS3Distributed::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-03-22 17:12:31 +00:00
|
|
|
StoragePtr storage = StorageS3Distributed::create(
|
2021-04-08 19:00:39 +00:00
|
|
|
filename, access_key_id, secret_access_key, StorageID(getDatabaseName(), table_name),
|
2021-04-12 19:35:26 +00:00
|
|
|
cluster_name, format, context->getSettingsRef().s3_max_connections,
|
2021-04-08 19:00:39 +00:00
|
|
|
getActualTableStructure(context), ConstraintsDescription{},
|
|
|
|
context, compression_method);
|
2021-03-16 18:41:29 +00:00
|
|
|
|
|
|
|
storage->startup();
|
|
|
|
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void registerTableFunctionS3Distributed(TableFunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<TableFunctionS3Distributed>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerTableFunctionCOSDistributed(TableFunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<TableFunctionCOSDistributed>();
|
|
|
|
}
|
|
|
|
|
2021-04-06 19:18:45 +00:00
|
|
|
|
2021-03-16 18:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|