2018-11-28 11:37:12 +00:00
|
|
|
#include "XDBCDictionarySource.h"
|
|
|
|
|
2018-06-05 19:46:49 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2021-08-05 18:08:52 +00:00
|
|
|
#include <Processors/Sources/SourceWithProgress.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
|
|
|
|
{
|
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,
|
2021-06-10 08:34:57 +00:00
|
|
|
"Dictionary source of type {} specifies a schema but schema is not supported by {}-driver",
|
2021-04-10 18:48:36 +00:00
|
|
|
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_,
|
2021-06-10 08:34:57 +00:00
|
|
|
const Configuration & configuration_,
|
2018-12-10 15:25:45 +00:00
|
|
|
const Block & sample_block_,
|
2021-06-01 12:20:52 +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"))
|
2021-06-10 08:34:57 +00:00
|
|
|
, update_time(std::chrono::system_clock::from_time_t(0))
|
|
|
|
, dict_struct(dict_struct_)
|
|
|
|
, configuration(configuration_)
|
|
|
|
, sample_block(sample_block_)
|
|
|
|
, query_builder(makeExternalQueryBuilder(dict_struct, configuration.db, configuration.schema, configuration.table, configuration.where, *bridge_))
|
|
|
|
, load_all_query(query_builder.composeLoadAllQuery())
|
|
|
|
, bridge_helper(bridge_)
|
|
|
|
, bridge_url(bridge_helper->getMainURI())
|
|
|
|
, timeouts(ConnectionTimeouts::getHTTPTimeouts(context_))
|
2016-06-05 15:21:35 +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"))
|
2021-06-10 08:34:57 +00:00
|
|
|
, update_time(other.update_time)
|
|
|
|
, dict_struct(other.dict_struct)
|
|
|
|
, configuration(other.configuration)
|
|
|
|
, sample_block(other.sample_block)
|
|
|
|
, query_builder(other.query_builder)
|
|
|
|
, load_all_query(other.load_all_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-06-10 08:34:57 +00:00
|
|
|
time_t hr_time = std::chrono::system_clock::to_time_t(update_time) - configuration.update_lag;
|
2021-03-15 19:23:27 +00:00
|
|
|
std::string str_time = DateLUT::instance().timeToString(hr_time);
|
2018-02-15 13:08:23 +00:00
|
|
|
update_time = std::chrono::system_clock::now();
|
2021-06-10 08:34:57 +00:00
|
|
|
return query_builder.composeUpdateQuery(configuration.update_field, str_time);
|
2018-02-15 13:08:23 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2021-08-05 18:08:52 +00:00
|
|
|
Pipe 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
|
|
|
|
2021-08-05 18:08:52 +00:00
|
|
|
Pipe 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
|
|
|
|
2021-08-05 18:08:52 +00:00
|
|
|
Pipe 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
|
|
|
|
2021-08-05 18:08:52 +00:00
|
|
|
Pipe 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
|
|
|
{
|
2021-06-10 08:34:57 +00:00
|
|
|
return !configuration.update_field.empty();
|
2018-02-15 13:08:23 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-06-10 08:34:57 +00:00
|
|
|
const auto & where = configuration.where;
|
|
|
|
return bridge_helper->getName() + ": " + configuration.db + '.' + configuration.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
|
|
|
{
|
2021-06-10 08:34:57 +00:00
|
|
|
if (!configuration.invalidate_query.empty())
|
2017-05-15 14:16:10 +00:00
|
|
|
{
|
2021-06-10 08:34:57 +00:00
|
|
|
auto response = doInvalidateQuery(configuration.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-08-05 18:08:52 +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
|
|
|
|
2021-08-05 18:08:52 +00:00
|
|
|
Pipe 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);
|
|
|
|
};
|
|
|
|
|
2021-08-05 18:08:52 +00:00
|
|
|
auto read_buf = std::make_unique<ReadWriteBufferFromHTTP>(url, Poco::Net::HTTPRequest::HTTP_POST, write_body_callback, timeouts);
|
|
|
|
auto format = FormatFactory::instance().getInput(IXDBCBridgeHelper::DEFAULT_FORMAT, *read_buf, sample_block, getContext(), max_block_size);
|
|
|
|
format->addBuffer(std::move(read_buf));
|
2016-06-05 15:21:35 +00:00
|
|
|
|
2021-08-05 18:08:52 +00:00
|
|
|
return Pipe(std::move(format));
|
|
|
|
}
|
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-06-01 12:20:52 +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"));
|
2021-06-10 08:34:57 +00:00
|
|
|
|
|
|
|
std::string settings_config_prefix = config_prefix + ".odbc";
|
|
|
|
|
|
|
|
XDBCDictionarySource::Configuration configuration
|
|
|
|
{
|
|
|
|
.db = config.getString(settings_config_prefix + ".db", ""),
|
|
|
|
.schema = config.getString(settings_config_prefix + ".schema", ""),
|
|
|
|
.table = config.getString(settings_config_prefix + ".table"),
|
|
|
|
.where = config.getString(settings_config_prefix + ".where", ""),
|
|
|
|
.invalidate_query = config.getString(settings_config_prefix + ".invalidate_query", ""),
|
|
|
|
.update_field = config.getString(settings_config_prefix + ".update_field", ""),
|
|
|
|
.update_lag = config.getUInt64(settings_config_prefix + ".update_lag", 1)
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::make_unique<XDBCDictionarySource>(dict_struct, configuration, sample_block, context, bridge);
|
2018-11-28 11:37:12 +00:00
|
|
|
#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-06-01 12:20:52 +00:00
|
|
|
ContextPtr /* context */,
|
2020-08-15 03:10:57 +00:00
|
|
|
const std::string & /* default_database */,
|
2021-05-24 21:27:24 +00:00
|
|
|
bool /* created_from_ddl */) -> 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
|
|
|
}
|
|
|
|
|
2021-08-05 18:08:52 +00:00
|
|
|
}
|