ClickHouse/src/TableFunctions/TableFunctionS3Distributed.cpp

140 lines
4.6 KiB
C++
Raw Normal View History

2021-04-06 19:18:45 +00:00
#include <memory>
#include <memory>
2021-03-16 18:41:29 +00:00
#include <thread>
#include <Common/config.h>
2021-03-19 21:49:18 +00:00
#include "DataStreams/RemoteBlockInputStream.h"
2021-03-26 15:33:14 +00:00
#include "Interpreters/ClientInfo.h"
2021-03-24 18:36:31 +00:00
#include "Parsers/ASTExpressionList.h"
2021-03-23 17:58:29 +00:00
#include "Parsers/ASTFunction.h"
#include "Parsers/IAST_fwd.h"
2021-03-19 21:49:18 +00:00
#include "Processors/Sources/SourceFromInputStream.h"
#include "Storages/StorageS3Distributed.h"
2021-03-16 18:41:29 +00:00
#if USE_AWS_S3
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 <Storages/StorageS3.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Interpreters/Context.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <TableFunctions/TableFunctionS3.h>
#include <TableFunctions/TableFunctionS3Distributed.h>
#include <TableFunctions/parseColumnsListForTableFunction.h>
#include <Parsers/ASTLiteral.h>
#include "registerTableFunctions.h"
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
2021-04-06 19:18:45 +00:00
extern const int UNEXPECTED_EXPRESSION;
2021-03-16 18:41:29 +00:00
}
2021-04-06 19:18:45 +00:00
2021-03-16 18:41:29 +00:00
void TableFunctionS3Distributed::parseArguments(const ASTPtr & ast_function, const Context & context)
{
/// Parse args
ASTs & args_func = ast_function->children;
if (args_func.size() != 1)
throw Exception("Table function '" + getName() + "' must have arguments.", ErrorCodes::LOGICAL_ERROR);
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>();
}
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
}
ColumnsDescription TableFunctionS3Distributed::getActualTableStructure(const Context & context) const
{
return parseColumnsListFromString(structure, context);
}
2021-03-19 21:49:18 +00:00
StoragePtr TableFunctionS3Distributed::executeImpl(
2021-04-08 19:00:39 +00:00
const ASTPtr & /*function*/, const Context & 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),
cluster_name, format, context.getSettingsRef().s3_max_connections,
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
NamesAndTypesList StorageS3Distributed::getVirtuals() const
{
return NamesAndTypesList{
{"_path", std::make_shared<DataTypeString>()},
{"_file", std::make_shared<DataTypeString>()}
};
}
2021-03-16 18:41:29 +00:00
}
#endif