2019-12-20 14:46:00 +00:00
|
|
|
#include "StorageXDBC.h"
|
2020-11-05 11:28:20 +00:00
|
|
|
|
|
|
|
#include <DataStreams/IBlockOutputStream.h>
|
|
|
|
#include <Formats/FormatFactory.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
2020-12-10 22:05:02 +00:00
|
|
|
#include <IO/ConnectionTimeoutsContext.h>
|
2018-09-25 14:29:44 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
2020-11-05 11:28:20 +00:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Processors/Pipe.h>
|
2018-09-25 14:29:44 +00:00
|
|
|
#include <Storages/StorageFactory.h>
|
2020-04-28 00:56:44 +00:00
|
|
|
#include <Storages/StorageURL.h>
|
2018-09-25 14:29:44 +00:00
|
|
|
#include <Storages/transformQueryForExternalDatabase.h>
|
|
|
|
#include <common/logger_useful.h>
|
2021-04-17 08:09:22 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
|
|
|
|
2020-02-17 15:33:16 +00:00
|
|
|
|
2018-09-25 14:29:44 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2018-10-03 10:44:43 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
2018-09-25 14:29:44 +00:00
|
|
|
|
|
|
|
|
2019-07-09 15:40:21 +00:00
|
|
|
StorageXDBC::StorageXDBC(
|
2019-12-04 16:06:55 +00:00
|
|
|
const StorageID & table_id_,
|
2018-10-03 10:44:43 +00:00
|
|
|
const std::string & remote_database_name_,
|
|
|
|
const std::string & remote_table_name_,
|
|
|
|
const ColumnsDescription & columns_,
|
2021-04-23 12:18:23 +00:00
|
|
|
const String & comment,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context_,
|
2018-10-03 10:44:43 +00:00
|
|
|
const BridgeHelperPtr bridge_helper_)
|
2019-08-24 21:20:20 +00:00
|
|
|
/// Please add support for constraints as soon as StorageODBC or JDBC will support insertion.
|
2021-04-23 12:18:23 +00:00
|
|
|
: IStorageURLBase(
|
|
|
|
Poco::URI(),
|
|
|
|
context_,
|
|
|
|
table_id_,
|
|
|
|
IXDBCBridgeHelper::DEFAULT_FORMAT,
|
|
|
|
getFormatSettings(context_),
|
|
|
|
columns_,
|
|
|
|
ConstraintsDescription{},
|
|
|
|
comment,
|
|
|
|
"" /* CompressionMethod */)
|
2018-10-03 10:44:43 +00:00
|
|
|
, bridge_helper(bridge_helper_)
|
|
|
|
, remote_database_name(remote_database_name_)
|
|
|
|
, remote_table_name(remote_table_name_)
|
|
|
|
{
|
|
|
|
log = &Poco::Logger::get("Storage" + bridge_helper->getName());
|
|
|
|
uri = bridge_helper->getMainURI();
|
|
|
|
}
|
2018-09-25 14:29:44 +00:00
|
|
|
|
2018-10-03 10:44:43 +00:00
|
|
|
std::string StorageXDBC::getReadMethod() const
|
|
|
|
{
|
|
|
|
return Poco::Net::HTTPRequest::HTTP_POST;
|
|
|
|
}
|
2018-09-25 14:29:44 +00:00
|
|
|
|
2020-06-17 16:39:58 +00:00
|
|
|
std::vector<std::pair<std::string, std::string>> StorageXDBC::getReadURIParams(
|
2021-04-17 08:09:22 +00:00
|
|
|
const Names & /* column_names */,
|
|
|
|
const StorageMetadataPtr & /* metadata_snapshot */,
|
2018-10-03 10:44:43 +00:00
|
|
|
const SelectQueryInfo & /*query_info*/,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr /*context*/,
|
2018-10-03 10:44:43 +00:00
|
|
|
QueryProcessingStage::Enum & /*processed_stage*/,
|
2019-02-19 00:48:52 +00:00
|
|
|
size_t max_block_size) const
|
2018-10-03 10:44:43 +00:00
|
|
|
{
|
2021-04-17 08:09:22 +00:00
|
|
|
return bridge_helper->getURLParams(max_block_size);
|
2018-10-03 10:44:43 +00:00
|
|
|
}
|
2018-09-25 14:29:44 +00:00
|
|
|
|
2020-06-17 16:39:58 +00:00
|
|
|
std::function<void(std::ostream &)> StorageXDBC::getReadPOSTDataCallback(
|
2021-04-17 08:09:22 +00:00
|
|
|
const Names & column_names,
|
2020-06-17 16:39:58 +00:00
|
|
|
const StorageMetadataPtr & metadata_snapshot,
|
2018-10-03 10:44:43 +00:00
|
|
|
const SelectQueryInfo & query_info,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr local_context,
|
2018-10-03 10:44:43 +00:00
|
|
|
QueryProcessingStage::Enum & /*processed_stage*/,
|
|
|
|
size_t /*max_block_size*/) const
|
|
|
|
{
|
2020-03-31 10:52:26 +00:00
|
|
|
String query = transformQueryForExternalDatabase(query_info,
|
2020-06-17 16:39:58 +00:00
|
|
|
metadata_snapshot->getColumns().getOrdinary(),
|
2018-10-03 10:44:43 +00:00
|
|
|
bridge_helper->getIdentifierQuotingStyle(),
|
|
|
|
remote_database_name,
|
|
|
|
remote_table_name,
|
2021-04-10 23:33:54 +00:00
|
|
|
local_context);
|
2018-10-03 10:44:43 +00:00
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
NamesAndTypesList cols;
|
|
|
|
for (const String & name : column_names)
|
|
|
|
{
|
|
|
|
auto column_data = metadata_snapshot->getColumns().getPhysical(name);
|
|
|
|
cols.emplace_back(column_data.name, column_data.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto write_body_callback = [query, cols](std::ostream & os)
|
|
|
|
{
|
|
|
|
os << "sample_block=" << escapeForFileName(cols.toString());
|
|
|
|
os << "&";
|
|
|
|
os << "query=" << escapeForFileName(query);
|
|
|
|
};
|
|
|
|
|
|
|
|
return write_body_callback;
|
2018-10-03 10:44:43 +00:00
|
|
|
}
|
2018-09-25 14:29:44 +00:00
|
|
|
|
2020-08-03 13:54:14 +00:00
|
|
|
Pipe StorageXDBC::read(
|
2020-06-15 19:08:58 +00:00
|
|
|
const Names & column_names,
|
|
|
|
const StorageMetadataPtr & metadata_snapshot,
|
2020-09-20 17:52:17 +00:00
|
|
|
SelectQueryInfo & query_info,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr local_context,
|
2018-10-03 10:44:43 +00:00
|
|
|
QueryProcessingStage::Enum processed_stage,
|
2019-02-18 23:38:44 +00:00
|
|
|
size_t max_block_size,
|
2018-10-03 10:44:43 +00:00
|
|
|
unsigned num_streams)
|
|
|
|
{
|
2021-05-04 23:02:54 +00:00
|
|
|
check(metadata_snapshot, column_names);
|
2018-09-25 14:29:44 +00:00
|
|
|
|
2018-10-03 10:44:43 +00:00
|
|
|
bridge_helper->startBridgeSync();
|
2021-04-10 23:33:54 +00:00
|
|
|
return IStorageURLBase::read(column_names, metadata_snapshot, query_info, local_context, processed_stage, max_block_size, num_streams);
|
2018-10-03 10:44:43 +00:00
|
|
|
}
|
2018-09-25 14:29:44 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
BlockOutputStreamPtr StorageXDBC::write(const ASTPtr & /*query*/, const StorageMetadataPtr & metadata_snapshot, ContextPtr local_context)
|
2020-05-05 23:42:44 +00:00
|
|
|
{
|
2020-04-28 00:56:44 +00:00
|
|
|
bridge_helper->startBridgeSync();
|
|
|
|
|
|
|
|
Poco::URI request_uri = uri;
|
|
|
|
request_uri.setPath("/write");
|
2021-04-17 08:09:22 +00:00
|
|
|
|
|
|
|
auto url_params = bridge_helper->getURLParams(65536);
|
2020-04-28 00:56:44 +00:00
|
|
|
for (const auto & [param, value] : url_params)
|
|
|
|
request_uri.addQueryParameter(param, value);
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2020-04-28 00:56:44 +00:00
|
|
|
request_uri.addQueryParameter("db_name", remote_database_name);
|
|
|
|
request_uri.addQueryParameter("table_name", remote_table_name);
|
2020-05-05 23:42:44 +00:00
|
|
|
request_uri.addQueryParameter("format_name", format_name);
|
2021-04-17 08:09:22 +00:00
|
|
|
request_uri.addQueryParameter("sample_block", metadata_snapshot->getSampleBlock().getNamesAndTypesList().toString());
|
2020-04-28 00:56:44 +00:00
|
|
|
|
|
|
|
return std::make_shared<StorageURLBlockOutputStream>(
|
2020-06-16 14:25:08 +00:00
|
|
|
request_uri,
|
|
|
|
format_name,
|
2021-04-10 23:33:54 +00:00
|
|
|
getFormatSettings(local_context),
|
2020-06-16 14:25:08 +00:00
|
|
|
metadata_snapshot->getSampleBlock(),
|
2021-04-10 23:33:54 +00:00
|
|
|
local_context,
|
|
|
|
ConnectionTimeouts::getHTTPTimeouts(local_context),
|
2020-06-16 14:25:08 +00:00
|
|
|
chooseCompressionMethod(uri.toString(), compression_method));
|
2020-04-28 00:56:44 +00:00
|
|
|
}
|
2018-09-25 14:29:44 +00:00
|
|
|
|
2020-06-16 14:25:08 +00:00
|
|
|
Block StorageXDBC::getHeaderBlock(const Names & column_names, const StorageMetadataPtr & metadata_snapshot) const
|
2018-10-03 10:44:43 +00:00
|
|
|
{
|
2021-05-04 23:02:54 +00:00
|
|
|
return getSampleBlockForColumns(metadata_snapshot, column_names);
|
2018-10-03 10:44:43 +00:00
|
|
|
}
|
2018-09-28 02:46:33 +00:00
|
|
|
|
2018-10-03 10:44:43 +00:00
|
|
|
std::string StorageXDBC::getName() const
|
|
|
|
{
|
|
|
|
return bridge_helper->getName();
|
|
|
|
}
|
2018-09-28 02:46:33 +00:00
|
|
|
|
2018-10-03 10:44:43 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
template <typename BridgeHelperMixin>
|
|
|
|
void registerXDBCStorage(StorageFactory & factory, const std::string & name)
|
2018-09-28 02:46:33 +00:00
|
|
|
{
|
2019-11-11 08:40:00 +00:00
|
|
|
factory.registerStorage(name, [name](const StorageFactory::Arguments & args)
|
2018-10-10 08:38:54 +00:00
|
|
|
{
|
2018-10-03 10:44:43 +00:00
|
|
|
ASTs & engine_args = args.engine_args;
|
|
|
|
|
|
|
|
if (engine_args.size() != 3)
|
|
|
|
throw Exception("Storage " + name + " requires exactly 3 parameters: " + name + "('DSN', database or schema, table)",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 3; ++i)
|
2021-04-10 23:33:54 +00:00
|
|
|
engine_args[i] = evaluateConstantExpressionOrIdentifierAsLiteral(engine_args[i], args.getLocalContext());
|
2018-10-03 10:44:43 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
BridgeHelperPtr bridge_helper = std::make_shared<XDBCBridgeHelper<BridgeHelperMixin>>(args.getContext(),
|
|
|
|
args.getContext()->getSettingsRef().http_receive_timeout.value,
|
2019-03-15 17:09:14 +00:00
|
|
|
engine_args[0]->as<ASTLiteral &>().value.safeGet<String>());
|
2021-04-23 12:18:23 +00:00
|
|
|
return std::make_shared<StorageXDBC>(
|
|
|
|
args.table_id,
|
2019-03-15 17:09:14 +00:00
|
|
|
engine_args[1]->as<ASTLiteral &>().value.safeGet<String>(),
|
|
|
|
engine_args[2]->as<ASTLiteral &>().value.safeGet<String>(),
|
2018-10-03 10:44:43 +00:00
|
|
|
args.columns,
|
2021-04-23 12:18:23 +00:00
|
|
|
args.comment,
|
2021-04-10 23:33:54 +00:00
|
|
|
args.getContext(),
|
2018-10-03 10:44:43 +00:00
|
|
|
bridge_helper);
|
|
|
|
|
2020-04-06 05:19:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.source_access_type = BridgeHelperMixin::getSourceAccessType(),
|
2018-10-03 10:44:43 +00:00
|
|
|
});
|
2018-09-28 02:46:33 +00:00
|
|
|
}
|
2018-10-03 10:44:43 +00:00
|
|
|
}
|
2018-09-28 02:46:33 +00:00
|
|
|
|
2018-10-03 10:44:43 +00:00
|
|
|
void registerStorageJDBC(StorageFactory & factory)
|
|
|
|
{
|
|
|
|
registerXDBCStorage<JDBCBridgeMixin>(factory, "JDBC");
|
|
|
|
}
|
2018-09-25 14:29:44 +00:00
|
|
|
|
2018-10-03 10:44:43 +00:00
|
|
|
void registerStorageODBC(StorageFactory & factory)
|
|
|
|
{
|
|
|
|
registerXDBCStorage<ODBCBridgeMixin>(factory, "ODBC");
|
2018-09-25 14:29:44 +00:00
|
|
|
}
|
2018-10-10 08:38:54 +00:00
|
|
|
}
|