ClickHouse/dbms/src/Storages/StorageURL.cpp

225 lines
6.7 KiB
C++
Raw Normal View History

2018-06-11 12:13:00 +00:00
#include <Storages/StorageFactory.h>
#include <Storages/StorageURL.h>
#include <Interpreters/Context.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Parsers/ASTLiteral.h>
#include <IO/ReadWriteBufferFromHTTP.h>
#include <IO/WriteBufferFromHTTP.h>
2018-06-11 12:13:00 +00:00
#include <Formats/FormatFactory.h>
#include <DataStreams/IBlockOutputStream.h>
#include <DataStreams/IBlockInputStream.h>
#include <DataStreams/AddingDefaultsBlockInputStream.h>
2018-06-11 12:13:00 +00:00
#include <Poco/Net/HTTPRequest.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
2018-06-11 12:13:00 +00:00
2019-08-24 21:20:20 +00:00
IStorageURLBase::IStorageURLBase(
const Poco::URI & uri_,
const Context & context_,
const std::string & database_name_,
2018-06-11 12:13:00 +00:00
const std::string & table_name_,
const String & format_name_,
2019-08-24 21:20:20 +00:00
const ColumnsDescription & columns_,
const ConstraintsDescription & constraints_)
: uri(uri_), context_global(context_), format_name(format_name_), table_name(table_name_), database_name(database_name_)
2018-06-11 12:13:00 +00:00
{
2019-08-24 21:20:20 +00:00
setColumns(columns_);
setConstraints(constraints_);
2018-06-11 12:13:00 +00:00
}
namespace
{
class StorageURLBlockInputStream : public IBlockInputStream
2018-06-11 12:13:00 +00:00
{
public:
StorageURLBlockInputStream(const Poco::URI & uri,
const std::string & method,
std::function<void(std::ostream &)> callback,
2018-06-11 12:13:00 +00:00
const String & format,
const String & name_,
const Block & sample_block,
const Context & context,
2019-02-10 16:55:12 +00:00
UInt64 max_block_size,
2018-06-11 12:13:00 +00:00
const ConnectionTimeouts & timeouts)
: name(name_)
{
read_buf = std::make_unique<ReadWriteBufferFromHTTP>(uri, method, callback, timeouts);
2018-06-11 12:13:00 +00:00
reader = FormatFactory::instance().getInput(format, *read_buf, sample_block, context, max_block_size);
2018-06-11 12:13:00 +00:00
}
String getName() const override
{
return name;
}
Block readImpl() override
{
return reader->read();
}
Block getHeader() const override
{
return reader->getHeader();
}
2018-06-13 18:51:56 +00:00
2018-06-11 12:13:00 +00:00
void readPrefixImpl() override
{
reader->readPrefix();
}
void readSuffixImpl() override
{
reader->readSuffix();
}
private:
String name;
std::unique_ptr<ReadWriteBufferFromHTTP> read_buf;
BlockInputStreamPtr reader;
};
class StorageURLBlockOutputStream : public IBlockOutputStream
{
public:
StorageURLBlockOutputStream(const Poco::URI & uri,
const String & format,
2018-06-11 12:13:00 +00:00
const Block & sample_block_,
const Context & context,
const ConnectionTimeouts & timeouts)
: sample_block(sample_block_)
2018-06-11 12:13:00 +00:00
{
write_buf = std::make_unique<WriteBufferFromHTTP>(uri, Poco::Net::HTTPRequest::HTTP_POST, timeouts);
writer = FormatFactory::instance().getOutput(format, *write_buf, sample_block, context);
2018-06-11 12:13:00 +00:00
}
Block getHeader() const override
{
return sample_block;
}
void write(const Block & block) override
{
writer->write(block);
}
2018-06-16 05:54:06 +00:00
void writePrefix() override
{
writer->writePrefix();
}
2018-06-16 05:54:06 +00:00
void writeSuffix() override
{
writer->writeSuffix();
writer->flush();
write_buf->finalize();
2018-06-11 12:13:00 +00:00
}
private:
Block sample_block;
std::unique_ptr<WriteBufferFromHTTP> write_buf;
BlockOutputStreamPtr writer;
2018-06-11 12:13:00 +00:00
};
}
2018-06-16 05:54:06 +00:00
std::string IStorageURLBase::getReadMethod() const
{
return Poco::Net::HTTPRequest::HTTP_GET;
}
std::vector<std::pair<std::string, std::string>> IStorageURLBase::getReadURIParams(const Names & /*column_names*/,
2018-06-11 12:13:00 +00:00
const SelectQueryInfo & /*query_info*/,
const Context & /*context*/,
2018-06-11 12:13:00 +00:00
QueryProcessingStage::Enum & /*processed_stage*/,
size_t /*max_block_size*/) const
{
return {};
}
std::function<void(std::ostream &)> IStorageURLBase::getReadPOSTDataCallback(const Names & /*column_names*/,
2018-06-11 12:13:00 +00:00
const SelectQueryInfo & /*query_info*/,
const Context & /*context*/,
QueryProcessingStage::Enum & /*processed_stage*/,
size_t /*max_block_size*/) const
{
return nullptr;
}
BlockInputStreams IStorageURLBase::read(const Names & column_names,
const SelectQueryInfo & query_info,
2018-06-11 12:13:00 +00:00
const Context & context,
2018-06-25 12:21:54 +00:00
QueryProcessingStage::Enum processed_stage,
size_t max_block_size,
2018-06-11 12:13:00 +00:00
unsigned /*num_streams*/)
{
auto request_uri = uri;
auto params = getReadURIParams(column_names, query_info, context, processed_stage, max_block_size);
for (const auto & [param, value] : params)
request_uri.addQueryParameter(param, value);
2018-11-12 14:52:30 +00:00
BlockInputStreamPtr block_input = std::make_shared<StorageURLBlockInputStream>(request_uri,
getReadMethod(),
getReadPOSTDataCallback(column_names, query_info, context, processed_stage, max_block_size),
2018-06-11 12:13:00 +00:00
format_name,
getName(),
2018-09-22 14:58:03 +00:00
getHeaderBlock(column_names),
2018-06-11 12:13:00 +00:00
context,
max_block_size,
2019-03-29 18:10:03 +00:00
ConnectionTimeouts::getHTTPTimeouts(context));
2018-11-12 14:52:30 +00:00
auto column_defaults = getColumns().getDefaults();
if (column_defaults.empty())
return {block_input};
return {std::make_shared<AddingDefaultsBlockInputStream>(block_input, column_defaults, context)};
2018-06-11 12:13:00 +00:00
}
2019-08-27 23:47:30 +00:00
void IStorageURLBase::rename(const String & /*new_path_to_db*/, const String & new_database_name, const String & new_table_name, TableStructureWriteLockHolder &)
{
table_name = new_table_name;
database_name = new_database_name;
}
2018-06-11 12:13:00 +00:00
BlockOutputStreamPtr IStorageURLBase::write(const ASTPtr & /*query*/, const Context & /*context*/)
2018-06-11 12:13:00 +00:00
{
return std::make_shared<StorageURLBlockOutputStream>(
2019-03-29 18:10:03 +00:00
uri, format_name, getSampleBlock(), context_global, ConnectionTimeouts::getHTTPTimeouts(context_global));
2018-06-11 12:13:00 +00:00
}
2018-06-16 05:54:06 +00:00
2018-06-11 12:13:00 +00:00
void registerStorageURL(StorageFactory & factory)
{
2018-08-24 00:07:25 +00:00
factory.registerStorage("URL", [](const StorageFactory::Arguments & args)
{
2018-06-11 12:13:00 +00:00
ASTs & engine_args = args.engine_args;
if (!(engine_args.size() == 1 || engine_args.size() == 2))
throw Exception(
"Storage URL requires exactly 2 arguments: url and name of used format.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
engine_args[0] = evaluateConstantExpressionOrIdentifierAsLiteral(engine_args[0], args.local_context);
2019-03-15 17:09:14 +00:00
String url = engine_args[0]->as<ASTLiteral &>().value.safeGet<String>();
2018-06-11 12:13:00 +00:00
Poco::URI uri(url);
engine_args[1] = evaluateConstantExpressionOrIdentifierAsLiteral(engine_args[1], args.local_context);
2019-03-15 17:09:14 +00:00
String format_name = engine_args[1]->as<ASTLiteral &>().value.safeGet<String>();
2018-06-11 12:13:00 +00:00
2019-08-24 21:20:20 +00:00
return StorageURL::create(uri, args.database_name, args.table_name, format_name, args.columns, args.constraints, args.context);
2018-06-11 12:13:00 +00:00
});
}
}