2015-01-29 11:51:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Dictionaries/IDictionarySource.h>
|
2015-11-18 21:35:24 +00:00
|
|
|
#include <DB/Dictionaries/DictionaryStructure.h>
|
2015-01-29 11:51:52 +00:00
|
|
|
#include <DB/Client/ConnectionPool.h>
|
|
|
|
#include <DB/DataStreams/RemoteBlockInputStream.h>
|
2015-02-05 13:44:33 +00:00
|
|
|
#include <DB/Interpreters/executeQuery.h>
|
2015-02-10 14:50:43 +00:00
|
|
|
#include <DB/Common/isLocalAddress.h>
|
2015-10-05 00:33:43 +00:00
|
|
|
#include <ext/range.hpp>
|
2015-01-30 13:43:16 +00:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2015-09-07 17:22:54 +00:00
|
|
|
#include "writeParenthesisedString.h"
|
|
|
|
|
2015-01-29 11:51:52 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-12 02:21:15 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int UNSUPPORTED_METHOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-04 09:36:30 +00:00
|
|
|
const auto max_connections = 16;
|
2015-01-30 13:43:16 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
/** Allows loading dictionaries from local or remote ClickHouse instance
|
|
|
|
* @todo use ConnectionPoolWithFailover
|
|
|
|
* @todo invent a way to keep track of source modifications
|
|
|
|
*/
|
|
|
|
class ClickHouseDictionarySource final : public IDictionarySource
|
2015-01-29 11:51:52 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-05-26 15:09:53 +00:00
|
|
|
ClickHouseDictionarySource(const DictionaryStructure & dict_struct,
|
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
2015-02-05 18:48:29 +00:00
|
|
|
const std::string & config_prefix,
|
2015-10-09 14:51:31 +00:00
|
|
|
const Block & sample_block, Context & context)
|
2015-05-26 15:09:53 +00:00
|
|
|
: dict_struct{dict_struct},
|
|
|
|
host{config.getString(config_prefix + ".host")},
|
2015-02-10 14:50:43 +00:00
|
|
|
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")},
|
2015-05-22 13:25:45 +00:00
|
|
|
where{config.getString(config_prefix + ".where", "")},
|
2015-02-03 17:03:35 +00:00
|
|
|
sample_block{sample_block}, context(context),
|
2015-02-10 14:50:43 +00:00
|
|
|
is_local{isLocalAddress({ host, port })},
|
2015-02-10 21:14:11 +00:00
|
|
|
pool{is_local ? nullptr : std::make_unique<ConnectionPool>(
|
2015-05-28 03:49:28 +00:00
|
|
|
max_connections, host, port, db, user, password,
|
2015-02-10 14:50:43 +00:00
|
|
|
"ClickHouseDictionarySource")
|
2015-01-29 11:51:52 +00:00
|
|
|
},
|
2015-11-16 17:27:12 +00:00
|
|
|
load_all_query{composeLoadAllQuery()},
|
2015-11-19 06:50:05 +00:00
|
|
|
key_tuple_definition{dict_struct.key ? composeKeyTupleDefinition() : std::string{}}
|
2015-02-05 13:44:33 +00:00
|
|
|
{}
|
2015-01-29 11:51:52 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
/// copy-constructor is provided in order to support cloneability
|
|
|
|
ClickHouseDictionarySource(const ClickHouseDictionarySource & other)
|
2015-05-26 15:09:53 +00:00
|
|
|
: dict_struct{other.dict_struct},
|
|
|
|
host{other.host}, port{other.port}, user{other.user}, password{other.password},
|
2015-03-18 14:19:55 +00:00
|
|
|
db{other.db}, table{other.table},
|
2015-05-22 13:25:45 +00:00
|
|
|
where{other.where},
|
2015-02-03 17:03:35 +00:00
|
|
|
sample_block{other.sample_block}, context(other.context),
|
2015-01-30 13:43:16 +00:00
|
|
|
is_local{other.is_local},
|
2015-02-10 21:14:11 +00:00
|
|
|
pool{is_local ? nullptr : std::make_unique<ConnectionPool>(
|
2015-05-28 03:49:28 +00:00
|
|
|
max_connections, host, port, db, user, password,
|
2015-02-10 14:50:43 +00:00
|
|
|
"ClickHouseDictionarySource")},
|
2015-01-30 13:43:16 +00:00
|
|
|
load_all_query{other.load_all_query}
|
|
|
|
{}
|
|
|
|
|
2015-01-29 11:51:52 +00:00
|
|
|
BlockInputStreamPtr loadAll() override
|
|
|
|
{
|
2015-02-10 14:50:43 +00:00
|
|
|
/** Query to local ClickHouse is marked internal in order to avoid
|
|
|
|
* the necessity of holding process_list_element shared pointer.
|
|
|
|
*/
|
2015-01-29 11:51:52 +00:00
|
|
|
if (is_local)
|
2015-02-10 14:50:43 +00:00
|
|
|
return executeQuery(load_all_query, context, true).in;
|
2015-01-29 11:51:52 +00:00
|
|
|
return new RemoteBlockInputStream{pool.get(), load_all_query, nullptr};
|
|
|
|
}
|
|
|
|
|
2015-05-22 13:25:45 +00:00
|
|
|
BlockInputStreamPtr loadIds(const std::vector<std::uint64_t> & ids) override
|
2015-01-29 11:51:52 +00:00
|
|
|
{
|
2015-11-16 17:27:12 +00:00
|
|
|
return createStreamForSelectiveLoad(composeLoadIdsQuery(ids));
|
|
|
|
}
|
2015-02-19 14:51:39 +00:00
|
|
|
|
2015-11-16 17:27:12 +00:00
|
|
|
BlockInputStreamPtr loadKeys(
|
|
|
|
const ConstColumnPlainPtrs & key_columns, const std::vector<std::size_t> & requested_rows) override
|
|
|
|
{
|
|
|
|
return createStreamForSelectiveLoad(composeLoadKeysQuery(key_columns, requested_rows));
|
2015-01-29 11:51:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 15:47:21 +00:00
|
|
|
bool isModified() const override { return true; }
|
2015-02-10 14:50:43 +00:00
|
|
|
bool supportsSelectiveLoad() const override { return true; }
|
2015-01-29 15:47:21 +00:00
|
|
|
|
2015-02-10 21:14:11 +00:00
|
|
|
DictionarySourcePtr clone() const override { return std::make_unique<ClickHouseDictionarySource>(*this); }
|
2015-01-29 15:47:21 +00:00
|
|
|
|
2015-05-22 13:25:45 +00:00
|
|
|
std::string toString() const override
|
|
|
|
{
|
|
|
|
return "ClickHouse: " + db + '.' + table + (where.empty() ? "" : ", where: " + where);
|
|
|
|
}
|
2015-03-25 10:09:33 +00:00
|
|
|
|
2015-01-30 13:43:16 +00:00
|
|
|
private:
|
2015-05-22 13:25:45 +00:00
|
|
|
std::string composeLoadAllQuery() const
|
2015-01-29 11:51:52 +00:00
|
|
|
{
|
2015-03-04 09:36:30 +00:00
|
|
|
std::string query;
|
2015-01-29 11:51:52 +00:00
|
|
|
|
|
|
|
{
|
2015-03-04 09:36:30 +00:00
|
|
|
WriteBufferFromString out{query};
|
|
|
|
writeString("SELECT ", out);
|
|
|
|
|
2015-11-12 14:28:23 +00:00
|
|
|
if (dict_struct.id)
|
2015-09-07 17:22:54 +00:00
|
|
|
{
|
2016-03-07 06:00:25 +00:00
|
|
|
if (!dict_struct.id.value().expression.empty())
|
2015-09-07 17:22:54 +00:00
|
|
|
{
|
2016-03-07 06:00:25 +00:00
|
|
|
writeParenthesisedString(dict_struct.id.value().expression, out);
|
2015-09-07 17:22:54 +00:00
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
|
|
|
|
2016-03-07 06:00:25 +00:00
|
|
|
writeProbablyBackQuotedString(dict_struct.id.value().name, out);
|
2015-09-07 17:22:54 +00:00
|
|
|
|
2015-11-12 14:28:23 +00:00
|
|
|
if (dict_struct.range_min && dict_struct.range_max)
|
|
|
|
{
|
|
|
|
writeString(", ", out);
|
|
|
|
|
2016-03-07 06:08:34 +00:00
|
|
|
if (!dict_struct.range_min.value().expression.empty())
|
2015-11-12 14:28:23 +00:00
|
|
|
{
|
2016-03-07 06:08:34 +00:00
|
|
|
writeParenthesisedString(dict_struct.range_min.value().expression, out);
|
2015-11-12 14:28:23 +00:00
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
2015-09-07 17:22:54 +00:00
|
|
|
|
2016-03-07 06:08:34 +00:00
|
|
|
writeProbablyBackQuotedString(dict_struct.range_min.value().name, out);
|
2015-11-12 14:28:23 +00:00
|
|
|
|
|
|
|
writeString(", ", out);
|
|
|
|
|
2016-03-07 06:08:34 +00:00
|
|
|
if (!dict_struct.range_max.value().expression.empty())
|
2015-11-12 14:28:23 +00:00
|
|
|
{
|
2016-03-07 06:08:34 +00:00
|
|
|
writeParenthesisedString(dict_struct.range_max.value().expression, out);
|
2015-11-12 14:28:23 +00:00
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
|
|
|
|
2016-03-07 06:08:34 +00:00
|
|
|
writeProbablyBackQuotedString(dict_struct.range_max.value().name, out);
|
2015-11-12 14:28:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (dict_struct.key)
|
|
|
|
{
|
|
|
|
auto first = true;
|
|
|
|
for (const auto & key : *dict_struct.key)
|
2015-09-07 17:22:54 +00:00
|
|
|
{
|
2015-11-12 14:28:23 +00:00
|
|
|
if (!first)
|
|
|
|
writeString(", ", out);
|
2015-11-13 01:44:41 +00:00
|
|
|
|
|
|
|
first = false;
|
2015-11-12 14:28:23 +00:00
|
|
|
|
|
|
|
if (!key.expression.empty())
|
|
|
|
{
|
|
|
|
writeParenthesisedString(key.expression, out);
|
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeProbablyBackQuotedString(key.name, out);
|
2015-09-07 17:22:54 +00:00
|
|
|
}
|
2015-07-13 16:18:28 +00:00
|
|
|
}
|
2015-05-26 15:09:53 +00:00
|
|
|
|
|
|
|
for (const auto & attr : dict_struct.attributes)
|
2015-03-04 09:36:30 +00:00
|
|
|
{
|
2015-05-26 15:09:53 +00:00
|
|
|
writeString(", ", out);
|
2015-03-04 09:36:30 +00:00
|
|
|
|
2015-05-26 15:09:53 +00:00
|
|
|
if (!attr.expression.empty())
|
|
|
|
{
|
2015-09-07 17:22:54 +00:00
|
|
|
writeParenthesisedString(attr.expression, out);
|
2015-05-26 15:09:53 +00:00
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeProbablyBackQuotedString(attr.name, out);
|
2015-03-04 09:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeString(" FROM ", out);
|
2015-03-25 10:09:33 +00:00
|
|
|
if (!db.empty())
|
|
|
|
{
|
|
|
|
writeProbablyBackQuotedString(db, out);
|
|
|
|
writeChar('.', out);
|
|
|
|
}
|
2015-03-04 09:36:30 +00:00
|
|
|
writeProbablyBackQuotedString(table, out);
|
2015-05-22 13:25:45 +00:00
|
|
|
|
|
|
|
if (!where.empty())
|
|
|
|
{
|
|
|
|
writeString(" WHERE ", out);
|
|
|
|
writeString(where, out);
|
|
|
|
}
|
|
|
|
|
2015-03-04 09:36:30 +00:00
|
|
|
writeChar(';', out);
|
2015-01-29 11:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2015-02-26 14:53:33 +00:00
|
|
|
std::string composeLoadIdsQuery(const std::vector<std::uint64_t> ids)
|
2015-01-29 11:51:52 +00:00
|
|
|
{
|
2015-11-16 17:27:12 +00:00
|
|
|
if (!dict_struct.id)
|
|
|
|
throw Exception{"Simple key required for method", ErrorCodes::UNSUPPORTED_METHOD};
|
2015-11-12 14:28:23 +00:00
|
|
|
|
2015-03-04 09:36:30 +00:00
|
|
|
std::string query;
|
2015-02-26 14:53:33 +00:00
|
|
|
|
|
|
|
{
|
2015-03-04 09:36:30 +00:00
|
|
|
WriteBufferFromString out{query};
|
|
|
|
writeString("SELECT ", out);
|
|
|
|
|
2016-03-07 06:00:25 +00:00
|
|
|
if (!dict_struct.id.value().expression.empty())
|
2015-09-07 17:22:54 +00:00
|
|
|
{
|
2016-03-07 06:00:25 +00:00
|
|
|
writeParenthesisedString(dict_struct.id.value().expression, out);
|
2015-09-07 17:22:54 +00:00
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
|
|
|
|
2016-03-07 06:00:25 +00:00
|
|
|
writeProbablyBackQuotedString(dict_struct.id.value().name, out);
|
2015-05-26 15:09:53 +00:00
|
|
|
|
|
|
|
for (const auto & attr : dict_struct.attributes)
|
2015-03-04 09:36:30 +00:00
|
|
|
{
|
2015-05-26 15:09:53 +00:00
|
|
|
writeString(", ", out);
|
2015-03-04 09:36:30 +00:00
|
|
|
|
2015-05-26 15:09:53 +00:00
|
|
|
if (!attr.expression.empty())
|
|
|
|
{
|
2015-09-07 17:22:54 +00:00
|
|
|
writeParenthesisedString(attr.expression, out);
|
2015-05-26 15:09:53 +00:00
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeProbablyBackQuotedString(attr.name, out);
|
2015-03-04 09:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeString(" FROM ", out);
|
2015-03-25 10:09:33 +00:00
|
|
|
if (!db.empty())
|
|
|
|
{
|
|
|
|
writeProbablyBackQuotedString(db, out);
|
|
|
|
writeChar('.', out);
|
|
|
|
}
|
2015-03-04 09:36:30 +00:00
|
|
|
writeProbablyBackQuotedString(table, out);
|
2015-05-22 13:25:45 +00:00
|
|
|
|
2015-03-04 09:36:30 +00:00
|
|
|
writeString(" WHERE ", out);
|
2015-05-22 13:25:45 +00:00
|
|
|
|
|
|
|
if (!where.empty())
|
|
|
|
{
|
|
|
|
writeString(where, out);
|
|
|
|
writeString(" AND ", out);
|
|
|
|
}
|
|
|
|
|
2016-03-07 06:00:25 +00:00
|
|
|
writeProbablyBackQuotedString(dict_struct.id.value().name, out);
|
2015-03-04 09:36:30 +00:00
|
|
|
writeString(" IN (", out);
|
|
|
|
|
2015-05-26 15:09:53 +00:00
|
|
|
auto first = true;
|
2015-03-04 09:36:30 +00:00
|
|
|
for (const auto id : ids)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
writeString(", ", out);
|
|
|
|
|
|
|
|
first = false;
|
2015-03-25 10:09:33 +00:00
|
|
|
writeString(DB::toString(id), out);
|
2015-03-04 09:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeString(");", out);
|
2015-02-26 14:53:33 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 14:51:39 +00:00
|
|
|
return query;
|
2015-01-29 11:51:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 17:27:12 +00:00
|
|
|
std::string composeLoadKeysQuery(
|
|
|
|
const ConstColumnPlainPtrs & key_columns, const std::vector<std::size_t> & requested_rows)
|
|
|
|
{
|
|
|
|
if (!dict_struct.key)
|
|
|
|
throw Exception{"Composite key required for method", ErrorCodes::UNSUPPORTED_METHOD};
|
|
|
|
|
|
|
|
std::string query;
|
|
|
|
|
|
|
|
{
|
|
|
|
WriteBufferFromString out{query};
|
|
|
|
writeString("SELECT ", out);
|
|
|
|
|
|
|
|
auto first = true;
|
|
|
|
for (const auto & key_or_attribute : boost::join(*dict_struct.key, dict_struct.attributes))
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
writeString(", ", out);
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
if (!key_or_attribute.expression.empty())
|
|
|
|
{
|
|
|
|
writeParenthesisedString(key_or_attribute.expression, out);
|
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeProbablyBackQuotedString(key_or_attribute.name, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeString(" FROM ", out);
|
|
|
|
if (!db.empty())
|
|
|
|
{
|
|
|
|
writeProbablyBackQuotedString(db, out);
|
|
|
|
writeChar('.', out);
|
|
|
|
}
|
|
|
|
writeProbablyBackQuotedString(table, out);
|
|
|
|
|
|
|
|
writeString(" WHERE ", out);
|
|
|
|
|
|
|
|
if (!where.empty())
|
|
|
|
{
|
|
|
|
writeString(where, out);
|
|
|
|
writeString(" AND ", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeString(key_tuple_definition, out);
|
|
|
|
writeString(" IN (", out);
|
|
|
|
|
|
|
|
first = true;
|
|
|
|
for (const auto row : requested_rows)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
writeString(", ", out);
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
composeKeyTuple(key_columns, row, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeString(");", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string composeKeyTupleDefinition() const
|
|
|
|
{
|
2015-11-19 06:50:05 +00:00
|
|
|
if (!dict_struct.key)
|
|
|
|
throw Exception{"Composite key required for method", ErrorCodes::UNSUPPORTED_METHOD};
|
|
|
|
|
2015-11-16 17:27:12 +00:00
|
|
|
std::string result{"("};
|
|
|
|
|
|
|
|
auto first = true;
|
|
|
|
for (const auto & key : *dict_struct.key)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
result += ", ";
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
result += key.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
result += ")";
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void composeKeyTuple(const ConstColumnPlainPtrs & key_columns, const std::size_t row, WriteBuffer & out) const
|
|
|
|
{
|
|
|
|
writeString("(", out);
|
|
|
|
|
|
|
|
const auto keys_size = key_columns.size();
|
|
|
|
auto first = true;
|
|
|
|
for (const auto i : ext::range(0, keys_size))
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
writeString(", ", out);
|
|
|
|
|
|
|
|
first = false;
|
2016-02-16 16:39:39 +00:00
|
|
|
(*dict_struct.key)[i].type->serializeTextQuoted(*key_columns[i], row, out);
|
2015-11-16 17:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeString(")", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockInputStreamPtr createStreamForSelectiveLoad(const std::string query)
|
|
|
|
{
|
|
|
|
if (is_local)
|
|
|
|
return executeQuery(query, context, true).in;
|
|
|
|
return new RemoteBlockInputStream{pool.get(), query, nullptr};
|
|
|
|
}
|
|
|
|
|
2015-05-26 15:09:53 +00:00
|
|
|
const DictionaryStructure dict_struct;
|
2015-01-29 11:51:52 +00:00
|
|
|
const std::string host;
|
|
|
|
const UInt16 port;
|
2015-01-30 13:43:16 +00:00
|
|
|
const std::string user;
|
|
|
|
const std::string password;
|
|
|
|
const std::string db;
|
|
|
|
const std::string table;
|
2015-05-22 13:25:45 +00:00
|
|
|
const std::string where;
|
2015-01-29 11:51:52 +00:00
|
|
|
Block sample_block;
|
2015-02-03 17:03:35 +00:00
|
|
|
Context & context;
|
2015-01-30 13:43:16 +00:00
|
|
|
const bool is_local;
|
|
|
|
std::unique_ptr<ConnectionPool> pool;
|
2015-01-29 11:51:52 +00:00
|
|
|
const std::string load_all_query;
|
2015-11-16 17:27:12 +00:00
|
|
|
const std::string key_tuple_definition;
|
2015-01-29 11:51:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|