2017-04-01 09:19:00 +00:00
|
|
|
#include <Dictionaries/ClickHouseDictionarySource.h>
|
|
|
|
#include <Dictionaries/ExternalQueryBuilder.h>
|
|
|
|
#include <Dictionaries/writeParenthesisedString.h>
|
|
|
|
#include <Client/ConnectionPool.h>
|
|
|
|
#include <DataStreams/RemoteBlockInputStream.h>
|
|
|
|
#include <Interpreters/executeQuery.h>
|
|
|
|
#include <Common/isLocalAddress.h>
|
2016-12-08 02:49:04 +00:00
|
|
|
#include <memory>
|
2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/range.h>
|
2017-12-27 17:58:52 +00:00
|
|
|
#include <IO/ConnectionTimeouts.h>
|
2016-12-08 02:49:04 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int UNSUPPORTED_METHOD;
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-19 17:40:55 +00:00
|
|
|
static const size_t MAX_CONNECTIONS = 16;
|
|
|
|
|
|
|
|
static ConnectionPoolWithFailoverPtr createPool(
|
2017-12-27 17:58:52 +00:00
|
|
|
const std::string & host, UInt16 port, const std::string & db,
|
|
|
|
const std::string & user, const std::string & password, const Context & context)
|
2017-04-19 17:40:55 +00:00
|
|
|
{
|
2017-12-27 17:58:52 +00:00
|
|
|
auto timeouts = ConnectionTimeouts::getTCPTimeouts(context.getSettingsRef());
|
2017-04-19 17:40:55 +00:00
|
|
|
ConnectionPoolPtrs pools;
|
|
|
|
pools.emplace_back(std::make_shared<ConnectionPool>(
|
2017-12-27 17:58:52 +00:00
|
|
|
MAX_CONNECTIONS, host, port, db, user, password, timeouts, "ClickHouseDictionarySource"));
|
2017-04-19 17:40:55 +00:00
|
|
|
return std::make_shared<ConnectionPoolWithFailover>(pools, LoadBalancing::RANDOM);
|
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
|
|
|
|
2017-04-19 17:40:55 +00:00
|
|
|
ClickHouseDictionarySource::ClickHouseDictionarySource(
|
|
|
|
const DictionaryStructure & dict_struct_,
|
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const std::string & config_prefix,
|
|
|
|
const Block & sample_block, Context & context)
|
2018-02-15 13:08:23 +00:00
|
|
|
: update_time{std::chrono::system_clock::from_time_t(0)},
|
|
|
|
dict_struct{dict_struct_},
|
2017-04-01 07:20:54 +00:00
|
|
|
host{config.getString(config_prefix + ".host")},
|
|
|
|
port(config.getInt(config_prefix + ".port")),
|
|
|
|
user{config.getString(config_prefix + ".user", "")},
|
|
|
|
password{config.getString(config_prefix + ".password", "")},
|
|
|
|
db{config.getString(config_prefix + ".db", "")},
|
|
|
|
table{config.getString(config_prefix + ".table")},
|
|
|
|
where{config.getString(config_prefix + ".where", "")},
|
2018-02-15 13:08:23 +00:00
|
|
|
update_field{config.getString(config_prefix + ".update_field")},
|
2017-04-01 07:20:54 +00:00
|
|
|
query_builder{dict_struct, db, table, where, ExternalQueryBuilder::Backticks},
|
|
|
|
sample_block{sample_block}, context(context),
|
2017-09-07 14:38:35 +00:00
|
|
|
is_local{isLocalAddress({ host, port }, config.getInt("tcp_port", 0))},
|
2017-12-27 17:58:52 +00:00
|
|
|
pool{is_local ? nullptr : createPool(host, port, db, user, password, context)},
|
2017-04-01 07:20:54 +00:00
|
|
|
load_all_query{query_builder.composeLoadAllQuery()}
|
2016-12-08 02:49:04 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
ClickHouseDictionarySource::ClickHouseDictionarySource(const ClickHouseDictionarySource & other)
|
2018-02-15 13:08:23 +00:00
|
|
|
: update_time{other.update_time},
|
|
|
|
dict_struct{other.dict_struct},
|
2017-04-01 07:20:54 +00:00
|
|
|
host{other.host}, port{other.port}, user{other.user}, password{other.password},
|
|
|
|
db{other.db}, table{other.table},
|
|
|
|
where{other.where},
|
2018-02-15 13:08:23 +00:00
|
|
|
update_field{other.update_field},
|
2017-04-01 07:20:54 +00:00
|
|
|
query_builder{dict_struct, db, table, where, ExternalQueryBuilder::Backticks},
|
|
|
|
sample_block{other.sample_block}, context(other.context),
|
|
|
|
is_local{other.is_local},
|
2017-12-27 17:58:52 +00:00
|
|
|
pool{is_local ? nullptr : createPool(host, port, db, user, password, context)},
|
2017-04-01 07:20:54 +00:00
|
|
|
load_all_query{other.load_all_query}
|
2016-12-08 02:49:04 +00:00
|
|
|
{}
|
|
|
|
|
2018-02-15 13:08:23 +00:00
|
|
|
std::string ClickHouseDictionarySource::getUpdateFieldAndDate()
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
std::string str_time("0000-00-00 00:00:00"); ///for initial load
|
|
|
|
return query_builder.composeUpdateQuery(update_field, str_time);
|
|
|
|
}
|
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
|
|
|
BlockInputStreamPtr ClickHouseDictionarySource::loadAll()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Query to local ClickHouse is marked internal in order to avoid
|
|
|
|
* the necessity of holding process_list_element shared pointer.
|
|
|
|
*/
|
|
|
|
if (is_local)
|
|
|
|
return executeQuery(load_all_query, context, true).in;
|
2018-02-15 18:54:12 +00:00
|
|
|
return std::make_shared<RemoteBlockInputStream>(pool, load_all_query, sample_block, context);
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 13:08:23 +00:00
|
|
|
BlockInputStreamPtr ClickHouseDictionarySource::loadUpdatedAll()
|
|
|
|
{
|
|
|
|
std::string load_update_query = getUpdateFieldAndDate();
|
|
|
|
if (is_local)
|
|
|
|
return executeQuery(load_update_query, context, true).in;
|
2018-03-07 19:20:36 +00:00
|
|
|
return std::make_shared<RemoteBlockInputStream>(pool, load_update_query, sample_block, context);
|
2018-02-15 13:08:23 +00:00
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
|
|
|
BlockInputStreamPtr ClickHouseDictionarySource::loadIds(const std::vector<UInt64> & ids)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return createStreamForSelectiveLoad(
|
|
|
|
query_builder.composeLoadIdsQuery(ids));
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockInputStreamPtr ClickHouseDictionarySource::loadKeys(
|
2017-07-21 06:35:58 +00:00
|
|
|
const Columns & key_columns, const std::vector<size_t> & requested_rows)
|
2016-12-08 02:49:04 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return createStreamForSelectiveLoad(
|
|
|
|
query_builder.composeLoadKeysQuery(
|
|
|
|
key_columns, requested_rows, ExternalQueryBuilder::IN_WITH_TUPLES));
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 13:08:23 +00:00
|
|
|
bool ClickHouseDictionarySource::hasUpdateField() const
|
|
|
|
{
|
|
|
|
return !update_field.empty();
|
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
|
|
|
std::string ClickHouseDictionarySource::toString() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return "ClickHouse: " + db + '.' + table + (where.empty() ? "" : ", where: " + where);
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-28 18:43:40 +00:00
|
|
|
BlockInputStreamPtr ClickHouseDictionarySource::createStreamForSelectiveLoad(const std::string & query)
|
2016-12-08 02:49:04 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (is_local)
|
|
|
|
return executeQuery(query, context, true).in;
|
2018-02-15 18:54:12 +00:00
|
|
|
return std::make_shared<RemoteBlockInputStream>(pool, query, sample_block, context);
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|