2018-11-28 11:37:12 +00:00
|
|
|
#include "XDBCDictionarySource.h"
|
|
|
|
|
2018-06-05 19:46:49 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2019-01-23 14:48:50 +00:00
|
|
|
#include <DataStreams/IBlockInputStream.h>
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
2018-08-13 18:10:26 +00:00
|
|
|
#include <Formats/FormatFactory.h>
|
2020-05-18 10:00:22 +00:00
|
|
|
#include <Processors/Formats/InputStreamFromInputFormat.h>
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <IO/ReadWriteBufferFromHTTP.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2020-12-10 22:05:02 +00:00
|
|
|
#include <IO/ConnectionTimeoutsContext.h>
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
|
|
|
#include <common/LocalDateTime.h>
|
|
|
|
#include <common/logger_useful.h>
|
2018-11-28 11:37:12 +00:00
|
|
|
#include "DictionarySourceFactory.h"
|
|
|
|
#include "DictionaryStructure.h"
|
2018-12-10 15:25:45 +00:00
|
|
|
#include "readInvalidateQuery.h"
|
2019-12-15 06:34:43 +00:00
|
|
|
#include "registerDictionaries.h"
|
2021-04-17 08:09:22 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
2019-12-15 06:34:43 +00:00
|
|
|
|
2016-06-05 15:21:35 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2018-11-28 11:37:12 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int SUPPORT_IS_DISABLED;
|
2020-07-06 12:23:36 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2018-11-28 11:37:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 18:10:26 +00:00
|
|
|
namespace
|
|
|
|
{
|
2019-01-23 14:48:50 +00:00
|
|
|
class XDBCBridgeBlockInputStream : public IBlockInputStream
|
2018-08-13 18:10:26 +00:00
|
|
|
{
|
|
|
|
public:
|
2018-12-10 15:25:45 +00:00
|
|
|
XDBCBridgeBlockInputStream(
|
|
|
|
const Poco::URI & uri,
|
2018-08-13 18:10:26 +00:00
|
|
|
std::function<void(std::ostream &)> callback,
|
|
|
|
const Block & sample_block,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2019-02-10 16:55:12 +00:00
|
|
|
UInt64 max_block_size,
|
2018-12-10 15:25:45 +00:00
|
|
|
const ConnectionTimeouts & timeouts,
|
2019-08-03 11:02:40 +00:00
|
|
|
const String name_)
|
|
|
|
: name(name_)
|
2018-08-13 18:10:26 +00:00
|
|
|
{
|
2018-08-15 22:00:28 +00:00
|
|
|
read_buf = std::make_unique<ReadWriteBufferFromHTTP>(uri, Poco::Net::HTTPRequest::HTTP_POST, callback, timeouts);
|
2020-05-18 10:00:22 +00:00
|
|
|
auto format = FormatFactory::instance().getInput(IXDBCBridgeHelper::DEFAULT_FORMAT, *read_buf, sample_block, context, max_block_size);
|
|
|
|
reader = std::make_shared<InputStreamFromInputFormat>(format);
|
2018-08-13 18:10:26 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 15:25:45 +00:00
|
|
|
Block getHeader() const override { return reader->getHeader(); }
|
2018-08-13 18:10:26 +00:00
|
|
|
|
2018-12-10 15:25:45 +00:00
|
|
|
String getName() const override { return name; }
|
2018-08-13 18:10:26 +00:00
|
|
|
|
|
|
|
private:
|
2018-12-10 15:25:45 +00:00
|
|
|
Block readImpl() override { return reader->read(); }
|
2018-08-14 10:33:41 +00:00
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
String name;
|
2018-08-13 18:10:26 +00:00
|
|
|
std::unique_ptr<ReadWriteBufferFromHTTP> read_buf;
|
|
|
|
BlockInputStreamPtr reader;
|
|
|
|
};
|
2020-07-06 12:23:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
ExternalQueryBuilder makeExternalQueryBuilder(const DictionaryStructure & dict_struct_,
|
|
|
|
const std::string & db_,
|
|
|
|
const std::string & schema_,
|
|
|
|
const std::string & table_,
|
|
|
|
const std::string & where_,
|
|
|
|
IXDBCBridgeHelper & bridge_)
|
|
|
|
{
|
|
|
|
std::string schema = schema_;
|
|
|
|
std::string table = table_;
|
|
|
|
|
|
|
|
if (bridge_.isSchemaAllowed())
|
|
|
|
{
|
|
|
|
if (schema.empty())
|
|
|
|
{
|
|
|
|
if (auto pos = table.find('.'); pos != std::string::npos)
|
|
|
|
{
|
|
|
|
schema = table.substr(0, pos);
|
|
|
|
table = table.substr(pos + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!schema.empty())
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Dictionary source of type {0} specifies a schema but schema is not supported by {0}-driver",
|
|
|
|
bridge_.getName());
|
2020-07-06 12:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {dict_struct_, db_, schema, table, where_, bridge_.getIdentifierQuotingStyle()};
|
|
|
|
}
|
2018-08-13 18:10:26 +00:00
|
|
|
}
|
2016-06-05 15:21:35 +00:00
|
|
|
|
2019-02-10 16:55:12 +00:00
|
|
|
static const UInt64 max_block_size = 8192;
|
2016-06-05 15:21:35 +00:00
|
|
|
|
|
|
|
|
2018-12-10 15:25:45 +00:00
|
|
|
XDBCDictionarySource::XDBCDictionarySource(
|
|
|
|
const DictionaryStructure & dict_struct_,
|
|
|
|
const Poco::Util::AbstractConfiguration & config_,
|
|
|
|
const std::string & config_prefix_,
|
|
|
|
const Block & sample_block_,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context_,
|
2018-12-10 15:25:45 +00:00
|
|
|
const BridgeHelperPtr bridge_)
|
2021-04-10 23:33:54 +00:00
|
|
|
: WithContext(context_->getGlobalContext())
|
|
|
|
, log(&Poco::Logger::get(bridge_->getName() + "DictionarySource"))
|
2018-12-10 15:25:45 +00:00
|
|
|
, update_time{std::chrono::system_clock::from_time_t(0)}
|
|
|
|
, dict_struct{dict_struct_}
|
|
|
|
, db{config_.getString(config_prefix_ + ".db", "")}
|
2020-07-06 12:23:36 +00:00
|
|
|
, schema{config_.getString(config_prefix_ + ".schema", "")}
|
2018-12-10 15:25:45 +00:00
|
|
|
, table{config_.getString(config_prefix_ + ".table")}
|
|
|
|
, where{config_.getString(config_prefix_ + ".where", "")}
|
|
|
|
, update_field{config_.getString(config_prefix_ + ".update_field", "")}
|
|
|
|
, sample_block{sample_block_}
|
2020-07-06 12:23:36 +00:00
|
|
|
, query_builder{makeExternalQueryBuilder(dict_struct, db, schema, table, where, *bridge_)}
|
2018-12-10 15:25:45 +00:00
|
|
|
, load_all_query{query_builder.composeLoadAllQuery()}
|
|
|
|
, invalidate_query{config_.getString(config_prefix_ + ".invalidate_query", "")}
|
|
|
|
, bridge_helper{bridge_}
|
2019-03-29 18:10:03 +00:00
|
|
|
, timeouts{ConnectionTimeouts::getHTTPTimeouts(context_)}
|
2016-06-05 15:21:35 +00:00
|
|
|
{
|
2018-09-28 02:46:33 +00:00
|
|
|
bridge_url = bridge_helper->getMainURI();
|
2018-03-02 12:59:41 +00:00
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
auto url_params = bridge_helper->getURLParams(max_block_size);
|
2018-08-13 18:10:26 +00:00
|
|
|
for (const auto & [name, value] : url_params)
|
|
|
|
bridge_url.addQueryParameter(name, value);
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// copy-constructor is provided in order to support cloneability
|
2018-09-28 02:46:33 +00:00
|
|
|
XDBCDictionarySource::XDBCDictionarySource(const XDBCDictionarySource & other)
|
2021-04-10 23:33:54 +00:00
|
|
|
: WithContext(other.getContext())
|
|
|
|
, log(&Poco::Logger::get(other.bridge_helper->getName() + "DictionarySource"))
|
2018-12-10 15:25:45 +00:00
|
|
|
, update_time{other.update_time}
|
|
|
|
, dict_struct{other.dict_struct}
|
|
|
|
, db{other.db}
|
|
|
|
, table{other.table}
|
|
|
|
, where{other.where}
|
|
|
|
, update_field{other.update_field}
|
|
|
|
, sample_block{other.sample_block}
|
2020-07-06 12:23:36 +00:00
|
|
|
, query_builder{other.query_builder}
|
2018-12-10 15:25:45 +00:00
|
|
|
, load_all_query{other.load_all_query}
|
|
|
|
, invalidate_query{other.invalidate_query}
|
|
|
|
, invalidate_query_response{other.invalidate_query_response}
|
|
|
|
, bridge_helper{other.bridge_helper}
|
|
|
|
, bridge_url{other.bridge_url}
|
|
|
|
, timeouts{other.timeouts}
|
2016-06-05 15:21:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
std::string XDBCDictionarySource::getUpdateFieldAndDate()
|
2018-02-15 13:08:23 +00:00
|
|
|
{
|
|
|
|
if (update_time != std::chrono::system_clock::from_time_t(0))
|
|
|
|
{
|
2021-03-15 19:23:27 +00:00
|
|
|
time_t hr_time = std::chrono::system_clock::to_time_t(update_time) - 1;
|
|
|
|
std::string str_time = DateLUT::instance().timeToString(hr_time);
|
2018-02-15 13:08:23 +00:00
|
|
|
update_time = std::chrono::system_clock::now();
|
|
|
|
return query_builder.composeUpdateQuery(update_field, str_time);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
update_time = std::chrono::system_clock::now();
|
2019-07-19 23:10:55 +00:00
|
|
|
return query_builder.composeLoadAllQuery();
|
2018-02-15 13:08:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
BlockInputStreamPtr XDBCDictionarySource::loadAll()
|
2016-06-05 15:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
LOG_TRACE(log, load_all_query);
|
2021-04-17 08:09:22 +00:00
|
|
|
return loadFromQuery(bridge_url, sample_block, load_all_query);
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
BlockInputStreamPtr XDBCDictionarySource::loadUpdatedAll()
|
2018-02-15 13:08:23 +00:00
|
|
|
{
|
|
|
|
std::string load_query_update = getUpdateFieldAndDate();
|
|
|
|
|
|
|
|
LOG_TRACE(log, load_query_update);
|
2021-04-17 08:09:22 +00:00
|
|
|
return loadFromQuery(bridge_url, sample_block, load_query_update);
|
2018-02-15 13:08:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
BlockInputStreamPtr XDBCDictionarySource::loadIds(const std::vector<UInt64> & ids)
|
2016-06-05 15:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto query = query_builder.composeLoadIdsQuery(ids);
|
2021-04-17 08:09:22 +00:00
|
|
|
return loadFromQuery(bridge_url, sample_block, query);
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2018-12-10 15:25:45 +00:00
|
|
|
BlockInputStreamPtr XDBCDictionarySource::loadKeys(const Columns & key_columns, const std::vector<size_t> & requested_rows)
|
2016-06-05 15:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto query = query_builder.composeLoadKeysQuery(key_columns, requested_rows, ExternalQueryBuilder::AND_OR_CHAIN);
|
2021-04-17 08:09:22 +00:00
|
|
|
return loadFromQuery(bridge_url, sample_block, query);
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
bool XDBCDictionarySource::supportsSelectiveLoad() const
|
2016-06-05 15:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
bool XDBCDictionarySource::hasUpdateField() const
|
2018-02-15 13:08:23 +00:00
|
|
|
{
|
|
|
|
return !update_field.empty();
|
|
|
|
}
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
DictionarySourcePtr XDBCDictionarySource::clone() const
|
2016-06-05 15:21:35 +00:00
|
|
|
{
|
2018-09-28 02:46:33 +00:00
|
|
|
return std::make_unique<XDBCDictionarySource>(*this);
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
std::string XDBCDictionarySource::toString() const
|
2016-06-05 15:21:35 +00:00
|
|
|
{
|
2018-11-26 00:56:50 +00:00
|
|
|
return bridge_helper->getName() + ": " + db + '.' + table + (where.empty() ? "" : ", where: " + where);
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
bool XDBCDictionarySource::isModified() const
|
2017-05-15 14:16:10 +00:00
|
|
|
{
|
|
|
|
if (!invalidate_query.empty())
|
|
|
|
{
|
2017-05-22 16:38:24 +00:00
|
|
|
auto response = doInvalidateQuery(invalidate_query);
|
2021-05-08 16:09:17 +00:00
|
|
|
if (invalidate_query_response == response) //-V1051
|
2017-05-15 14:16:10 +00:00
|
|
|
return false;
|
|
|
|
invalidate_query_response = response;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
std::string XDBCDictionarySource::doInvalidateQuery(const std::string & request) const
|
2017-05-15 14:16:10 +00:00
|
|
|
{
|
2018-08-10 04:02:56 +00:00
|
|
|
Block invalidate_sample_block;
|
2017-12-14 01:43:19 +00:00
|
|
|
ColumnPtr column(ColumnString::create());
|
2018-08-10 04:02:56 +00:00
|
|
|
invalidate_sample_block.insert(ColumnWithTypeAndName(column, std::make_shared<DataTypeString>(), "Sample Block"));
|
2018-09-12 21:34:48 +00:00
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
bridge_helper->startBridgeSync();
|
2018-08-14 10:33:41 +00:00
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
auto invalidate_url = bridge_helper->getMainURI();
|
2021-04-17 08:09:22 +00:00
|
|
|
auto url_params = bridge_helper->getURLParams(max_block_size);
|
2018-09-12 21:34:48 +00:00
|
|
|
for (const auto & [name, value] : url_params)
|
|
|
|
invalidate_url.addQueryParameter(name, value);
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
return readInvalidateQuery(*loadFromQuery(invalidate_url, invalidate_sample_block, request));
|
2018-08-13 18:10:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
|
|
|
BlockInputStreamPtr XDBCDictionarySource::loadFromQuery(const Poco::URI url, const Block & required_sample_block, const std::string & query) const
|
2018-08-13 18:10:26 +00:00
|
|
|
{
|
2018-09-28 02:46:33 +00:00
|
|
|
bridge_helper->startBridgeSync();
|
2021-04-17 08:09:22 +00:00
|
|
|
|
|
|
|
auto write_body_callback = [required_sample_block, query](std::ostream & os)
|
|
|
|
{
|
|
|
|
os << "sample_block=" << escapeForFileName(required_sample_block.getNamesAndTypesList().toString());
|
|
|
|
os << "&";
|
|
|
|
os << "query=" << escapeForFileName(query);
|
|
|
|
};
|
|
|
|
|
2018-12-10 15:25:45 +00:00
|
|
|
return std::make_shared<XDBCBridgeBlockInputStream>(
|
2021-04-17 08:09:22 +00:00
|
|
|
url,
|
|
|
|
write_body_callback,
|
|
|
|
required_sample_block,
|
2021-04-10 23:33:54 +00:00
|
|
|
getContext(),
|
2018-08-13 18:10:26 +00:00
|
|
|
max_block_size,
|
2018-12-10 15:25:45 +00:00
|
|
|
timeouts,
|
|
|
|
bridge_helper->getName() + "BlockInputStream");
|
2017-05-15 14:16:10 +00:00
|
|
|
}
|
2016-06-05 15:21:35 +00:00
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2018-11-28 11:37:12 +00:00
|
|
|
void registerDictionarySourceXDBC(DictionarySourceFactory & factory)
|
|
|
|
{
|
2020-03-23 02:12:31 +00:00
|
|
|
auto create_table_source = [=](const DictionaryStructure & dict_struct,
|
2020-05-08 14:11:19 +00:00
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const std::string & config_prefix,
|
|
|
|
Block & sample_block,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2020-08-15 03:10:57 +00:00
|
|
|
const std::string & /* default_database */,
|
2020-05-08 14:11:19 +00:00
|
|
|
bool /* check_config */) -> DictionarySourcePtr {
|
|
|
|
#if USE_ODBC
|
2018-12-10 15:25:45 +00:00
|
|
|
BridgeHelperPtr bridge = std::make_shared<XDBCBridgeHelper<ODBCBridgeMixin>>(
|
2021-04-10 23:33:54 +00:00
|
|
|
context, context->getSettings().http_receive_timeout, config.getString(config_prefix + ".odbc.connection_string"));
|
2018-11-28 11:37:12 +00:00
|
|
|
return std::make_unique<XDBCDictionarySource>(dict_struct, config, config_prefix + ".odbc", sample_block, context, bridge);
|
|
|
|
#else
|
2018-11-29 14:37:56 +00:00
|
|
|
(void)dict_struct;
|
|
|
|
(void)config;
|
|
|
|
(void)config_prefix;
|
|
|
|
(void)sample_block;
|
|
|
|
(void)context;
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::SUPPORT_IS_DISABLED,
|
|
|
|
"Dictionary source of type `odbc` is disabled because poco library was built without ODBC support.");
|
2018-11-28 11:37:12 +00:00
|
|
|
#endif
|
|
|
|
};
|
2020-03-23 02:12:31 +00:00
|
|
|
factory.registerSource("odbc", create_table_source);
|
2018-11-28 11:37:12 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:09:22 +00:00
|
|
|
|
2018-11-28 11:37:12 +00:00
|
|
|
void registerDictionarySourceJDBC(DictionarySourceFactory & factory)
|
|
|
|
{
|
2020-03-23 02:12:31 +00:00
|
|
|
auto create_table_source = [=](const DictionaryStructure & /* dict_struct */,
|
2018-11-28 11:37:12 +00:00
|
|
|
const Poco::Util::AbstractConfiguration & /* config */,
|
|
|
|
const std::string & /* config_prefix */,
|
|
|
|
Block & /* sample_block */,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr /* context */,
|
2020-08-15 03:10:57 +00:00
|
|
|
const std::string & /* default_database */,
|
2019-12-10 17:27:29 +00:00
|
|
|
bool /* check_config */) -> DictionarySourcePtr {
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::SUPPORT_IS_DISABLED,
|
|
|
|
"Dictionary source of type `jdbc` is disabled until consistent support for nullable fields.");
|
2018-11-28 11:37:12 +00:00
|
|
|
// BridgeHelperPtr bridge = std::make_shared<XDBCBridgeHelper<JDBCBridgeMixin>>(config, context.getSettings().http_receive_timeout, config.getString(config_prefix + ".connection_string"));
|
|
|
|
// return std::make_unique<XDBCDictionarySource>(dict_struct, config, config_prefix + ".jdbc", sample_block, context, bridge);
|
|
|
|
};
|
2020-03-23 02:12:31 +00:00
|
|
|
factory.registerSource("jdbc", create_table_source);
|
2018-11-28 11:37:12 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|