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>
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <IO/ReadWriteBufferFromHTTP.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2018-09-28 02:46:33 +00:00
|
|
|
#include <Common/XDBCBridgeHelper.h>
|
2018-12-10 15:25:45 +00:00
|
|
|
#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"
|
2016-05-29 01:12:10 +00:00
|
|
|
|
2019-12-15 06:34:43 +00:00
|
|
|
#include "registerDictionaries.h"
|
|
|
|
|
2020-05-08 14:11:19 +00:00
|
|
|
#if USE_ODBC
|
2018-12-10 15:25:45 +00:00
|
|
|
# include <Poco/Data/ODBC/Connector.h>
|
2018-11-28 11:37:12 +00:00
|
|
|
#endif
|
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;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
const Context & 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);
|
2018-12-10 15:25:45 +00:00
|
|
|
reader
|
|
|
|
= FormatFactory::instance().getInput(IXDBCBridgeHelper::DEFAULT_FORMAT, *read_buf, sample_block, context, max_block_size);
|
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;
|
|
|
|
};
|
|
|
|
}
|
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_,
|
|
|
|
const Context & context_,
|
|
|
|
const BridgeHelperPtr bridge_)
|
|
|
|
: log(&Logger::get(bridge_->getName() + "DictionarySource"))
|
|
|
|
, update_time{std::chrono::system_clock::from_time_t(0)}
|
|
|
|
, dict_struct{dict_struct_}
|
|
|
|
, db{config_.getString(config_prefix_ + ".db", "")}
|
|
|
|
, table{config_.getString(config_prefix_ + ".table")}
|
|
|
|
, where{config_.getString(config_prefix_ + ".where", "")}
|
|
|
|
, update_field{config_.getString(config_prefix_ + ".update_field", "")}
|
|
|
|
, sample_block{sample_block_}
|
|
|
|
, query_builder{dict_struct, db, table, where, bridge_->getIdentifierQuotingStyle()}
|
|
|
|
, 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_)}
|
2018-12-10 15:25:45 +00:00
|
|
|
, global_context(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
|
|
|
|
2018-10-03 12:10:57 +00:00
|
|
|
auto url_params = bridge_helper->getURLParams(sample_block_.getNamesAndTypesList().toString(), 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)
|
2018-12-10 15:25:45 +00:00
|
|
|
: log(&Logger::get(other.bridge_helper->getName() + "DictionarySource"))
|
|
|
|
, 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}
|
|
|
|
, query_builder{dict_struct, db, table, where, other.bridge_helper->getIdentifierQuotingStyle()}
|
|
|
|
, 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}
|
|
|
|
, global_context{other.global_context}
|
2016-06-05 15:21:35 +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))
|
|
|
|
{
|
|
|
|
auto tmp_time = update_time;
|
|
|
|
update_time = std::chrono::system_clock::now();
|
|
|
|
time_t hr_time = std::chrono::system_clock::to_time_t(tmp_time) - 1;
|
|
|
|
std::string str_time = std::to_string(LocalDateTime(hr_time));
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2018-08-13 18:10:26 +00:00
|
|
|
return loadBase(load_all_query);
|
2016-06-05 15:21:35 +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);
|
2018-08-13 18:10:26 +00:00
|
|
|
return loadBase(load_query_update);
|
2018-02-15 13:08:23 +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);
|
2018-08-13 18:10:26 +00:00
|
|
|
return loadBase(query);
|
2016-06-05 15:21:35 +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);
|
2018-08-13 18:10:26 +00:00
|
|
|
return loadBase(query);
|
2016-06-05 15:21:35 +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
|
|
|
}
|
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
bool XDBCDictionarySource::hasUpdateField() const
|
2018-02-15 13:08:23 +00:00
|
|
|
{
|
|
|
|
return !update_field.empty();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
if (invalidate_query_response == response)
|
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();
|
|
|
|
auto url_params = bridge_helper->getURLParams(invalidate_sample_block.getNamesAndTypesList().toString(), max_block_size);
|
2018-09-12 21:34:48 +00:00
|
|
|
for (const auto & [name, value] : url_params)
|
|
|
|
invalidate_url.addQueryParameter(name, value);
|
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
XDBCBridgeBlockInputStream stream(
|
2018-09-12 21:34:48 +00:00
|
|
|
invalidate_url,
|
2018-08-13 18:10:26 +00:00
|
|
|
[request](std::ostream & os) { os << "query=" << request; },
|
|
|
|
invalidate_sample_block,
|
|
|
|
global_context,
|
|
|
|
max_block_size,
|
2018-12-10 15:25:45 +00:00
|
|
|
timeouts,
|
|
|
|
bridge_helper->getName() + "BlockInputStream");
|
2018-08-14 10:33:41 +00:00
|
|
|
|
2018-08-13 18:10:26 +00:00
|
|
|
return readInvalidateQuery(stream);
|
|
|
|
}
|
|
|
|
|
2018-09-28 02:46:33 +00:00
|
|
|
BlockInputStreamPtr XDBCDictionarySource::loadBase(const std::string & query) const
|
2018-08-13 18:10:26 +00:00
|
|
|
{
|
2018-09-28 02:46:33 +00:00
|
|
|
bridge_helper->startBridgeSync();
|
2018-12-10 15:25:45 +00:00
|
|
|
return std::make_shared<XDBCBridgeBlockInputStream>(
|
|
|
|
bridge_url,
|
2018-08-13 18:10:26 +00:00
|
|
|
[query](std::ostream & os) { os << "query=" << query; },
|
|
|
|
sample_block,
|
|
|
|
global_context,
|
|
|
|
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
|
|
|
|
2018-11-28 11:37:12 +00:00
|
|
|
void registerDictionarySourceXDBC(DictionarySourceFactory & factory)
|
|
|
|
{
|
2020-05-08 14:11:19 +00:00
|
|
|
#if USE_ODBC
|
2018-11-28 11:37:12 +00:00
|
|
|
Poco::Data::ODBC::Connector::registerConnector();
|
|
|
|
#endif
|
|
|
|
|
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,
|
|
|
|
const Context & context,
|
|
|
|
bool /* check_config */) -> DictionarySourcePtr {
|
|
|
|
#if USE_ODBC
|
2018-12-10 15:25:45 +00:00
|
|
|
BridgeHelperPtr bridge = std::make_shared<XDBCBridgeHelper<ODBCBridgeMixin>>(
|
|
|
|
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;
|
2018-12-10 15:25:45 +00:00
|
|
|
throw Exception{"Dictionary source of type `odbc` is disabled because poco library was built without ODBC support.",
|
|
|
|
ErrorCodes::SUPPORT_IS_DISABLED};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 */,
|
2019-12-10 17:27:29 +00:00
|
|
|
const Context & /* context */,
|
|
|
|
bool /* check_config */) -> DictionarySourcePtr {
|
2018-12-10 15:25:45 +00:00
|
|
|
throw Exception{"Dictionary source of type `jdbc` is disabled until consistent support for nullable fields.",
|
|
|
|
ErrorCodes::SUPPORT_IS_DISABLED};
|
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
|
|
|
}
|