2018-11-28 11:37:12 +00:00
|
|
|
#include "MySQLDictionarySource.h"
|
2020-04-16 12:31:57 +00:00
|
|
|
|
2018-11-28 11:37:12 +00:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2018-12-10 15:25:45 +00:00
|
|
|
#include "DictionarySourceFactory.h"
|
|
|
|
#include "DictionaryStructure.h"
|
2019-12-15 06:34:43 +00:00
|
|
|
#include "registerDictionaries.h"
|
2018-11-28 11:37:12 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int SUPPORT_IS_DISABLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerDictionarySourceMysql(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 */,
|
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 {
|
2017-12-09 06:32:22 +00:00
|
|
|
#if USE_MYSQL
|
2018-11-28 11:37:12 +00:00
|
|
|
return std::make_unique<MySQLDictionarySource>(dict_struct, config, config_prefix + ".mysql", sample_block);
|
|
|
|
#else
|
|
|
|
(void)dict_struct;
|
|
|
|
(void)config;
|
|
|
|
(void)config_prefix;
|
|
|
|
(void)sample_block;
|
2018-12-10 15:25:45 +00:00
|
|
|
throw Exception{"Dictionary source of type `mysql` is disabled because ClickHouse was built without mysql support.",
|
|
|
|
ErrorCodes::SUPPORT_IS_DISABLED};
|
2018-11-28 11:37:12 +00:00
|
|
|
#endif
|
|
|
|
};
|
2020-03-23 02:12:31 +00:00
|
|
|
factory.registerSource("mysql", create_table_source);
|
2018-11-28 11:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-12-09 06:32:22 +00:00
|
|
|
|
2018-11-28 11:37:12 +00:00
|
|
|
|
|
|
|
#if USE_MYSQL
|
2018-12-10 15:25:45 +00:00
|
|
|
# include <Columns/ColumnString.h>
|
|
|
|
# include <DataTypes/DataTypeString.h>
|
|
|
|
# include <IO/WriteBufferFromString.h>
|
|
|
|
# include <IO/WriteHelpers.h>
|
|
|
|
# include <common/LocalDateTime.h>
|
|
|
|
# include <common/logger_useful.h>
|
2019-02-15 11:46:07 +00:00
|
|
|
# include <Formats/MySQLBlockInputStream.h>
|
2018-12-10 15:25:45 +00:00
|
|
|
# include "readInvalidateQuery.h"
|
2021-02-27 08:18:28 +00:00
|
|
|
# include <mysqlxx/Exception.h>
|
2020-02-27 09:34:06 +00:00
|
|
|
# include <mysqlxx/PoolFactory.h>
|
2018-06-05 19:46:49 +00:00
|
|
|
|
2016-06-05 15:21:35 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2019-02-10 16:55:12 +00:00
|
|
|
static const UInt64 max_block_size = 8192;
|
2021-02-27 08:18:28 +00:00
|
|
|
static const size_t default_num_tries_on_connection_loss = 3;
|
2016-06-05 15:21:35 +00:00
|
|
|
|
|
|
|
|
2018-12-10 15:25:45 +00:00
|
|
|
MySQLDictionarySource::MySQLDictionarySource(
|
|
|
|
const DictionaryStructure & dict_struct_,
|
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const std::string & config_prefix,
|
2019-08-03 11:02:40 +00:00
|
|
|
const Block & sample_block_)
|
2020-05-30 21:57:37 +00:00
|
|
|
: log(&Poco::Logger::get("MySQLDictionarySource"))
|
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", "")}
|
|
|
|
, table{config.getString(config_prefix + ".table")}
|
|
|
|
, where{config.getString(config_prefix + ".where", "")}
|
|
|
|
, update_field{config.getString(config_prefix + ".update_field", "")}
|
|
|
|
, dont_check_update_time{config.getBool(config_prefix + ".dont_check_update_time", false)}
|
2019-08-03 11:02:40 +00:00
|
|
|
, sample_block{sample_block_}
|
2021-03-27 14:35:44 +00:00
|
|
|
, pool{std::make_shared<mysqlxx::PoolWithFailover>(mysqlxx::PoolFactory::instance().get(config, config_prefix))}
|
2020-07-06 12:23:36 +00:00
|
|
|
, query_builder{dict_struct, db, "", table, where, IdentifierQuotingStyle::Backticks}
|
2018-12-10 15:25:45 +00:00
|
|
|
, load_all_query{query_builder.composeLoadAllQuery()}
|
|
|
|
, invalidate_query{config.getString(config_prefix + ".invalidate_query", "")}
|
2021-02-27 08:18:28 +00:00
|
|
|
, close_connection(
|
|
|
|
config.getBool(config_prefix + ".close_connection", false) || config.getBool(config_prefix + ".share_connection", false))
|
|
|
|
, max_tries_for_mysql_block_input_stream(
|
|
|
|
config.getBool(config_prefix + ".fail_on_connection_loss", false) ? 1 : default_num_tries_on_connection_loss)
|
2016-06-05 15:21:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// copy-constructor is provided in order to support cloneability
|
|
|
|
MySQLDictionarySource::MySQLDictionarySource(const MySQLDictionarySource & other)
|
2020-05-30 21:57:37 +00:00
|
|
|
: log(&Poco::Logger::get("MySQLDictionarySource"))
|
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}
|
|
|
|
, dont_check_update_time{other.dont_check_update_time}
|
|
|
|
, sample_block{other.sample_block}
|
|
|
|
, pool{other.pool}
|
2020-07-06 12:23:36 +00:00
|
|
|
, query_builder{dict_struct, db, "", table, where, IdentifierQuotingStyle::Backticks}
|
2018-12-10 15:25:45 +00:00
|
|
|
, load_all_query{other.load_all_query}
|
|
|
|
, last_modification{other.last_modification}
|
|
|
|
, invalidate_query{other.invalidate_query}
|
|
|
|
, invalidate_query_response{other.invalidate_query_response}
|
2019-05-23 13:55:00 +00:00
|
|
|
, close_connection{other.close_connection}
|
2021-02-27 08:18:28 +00:00
|
|
|
, max_tries_for_mysql_block_input_stream{other.max_tries_for_mysql_block_input_stream}
|
2016-06-05 15:21:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-15 13:08:23 +00:00
|
|
|
std::string MySQLDictionarySource::getUpdateFieldAndDate()
|
2018-01-15 12:44:39 +00:00
|
|
|
{
|
2018-02-15 13:08:23 +00:00
|
|
|
if (update_time != std::chrono::system_clock::from_time_t(0))
|
2018-01-15 12:44:39 +00:00
|
|
|
{
|
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-01-15 12:44:39 +00:00
|
|
|
update_time = std::chrono::system_clock::now();
|
2018-02-15 13:08:23 +00:00
|
|
|
return query_builder.composeUpdateQuery(update_field, str_time);
|
2018-01-15 12:44:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
update_time = std::chrono::system_clock::now();
|
2019-07-19 23:10:55 +00:00
|
|
|
return query_builder.composeLoadAllQuery();
|
2018-01-15 12:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 18:15:31 +00:00
|
|
|
BlockInputStreamPtr MySQLDictionarySource::loadBase(const String & query)
|
2021-02-27 08:18:28 +00:00
|
|
|
{
|
2021-03-24 18:15:31 +00:00
|
|
|
return std::make_shared<MySQLWithFailoverBlockInputStream>(
|
|
|
|
pool, query, sample_block, max_block_size, close_connection, false, max_tries_for_mysql_block_input_stream);
|
2021-02-27 08:18:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 15:21:35 +00:00
|
|
|
BlockInputStreamPtr MySQLDictionarySource::loadAll()
|
|
|
|
{
|
2021-03-27 14:35:44 +00:00
|
|
|
auto connection = pool->get();
|
2020-02-27 09:34:06 +00:00
|
|
|
last_modification = getLastModification(connection, false);
|
2016-06-05 15:21:35 +00:00
|
|
|
|
2020-05-23 22:24:01 +00:00
|
|
|
LOG_TRACE(log, load_all_query);
|
2021-03-24 18:15:31 +00:00
|
|
|
return loadBase(load_all_query);
|
2018-02-15 13:08:23 +00:00
|
|
|
}
|
2018-01-15 12:44:39 +00:00
|
|
|
|
2018-02-15 13:08:23 +00:00
|
|
|
BlockInputStreamPtr MySQLDictionarySource::loadUpdatedAll()
|
|
|
|
{
|
2021-03-27 14:35:44 +00:00
|
|
|
auto connection = pool->get();
|
2020-02-27 09:34:06 +00:00
|
|
|
last_modification = getLastModification(connection, false);
|
2018-01-15 12:44:39 +00:00
|
|
|
|
2018-02-15 13:08:23 +00:00
|
|
|
std::string load_update_query = getUpdateFieldAndDate();
|
2020-05-23 22:24:01 +00:00
|
|
|
LOG_TRACE(log, load_update_query);
|
2021-03-24 18:15:31 +00:00
|
|
|
return loadBase(load_update_query);
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
BlockInputStreamPtr MySQLDictionarySource::loadIds(const std::vector<UInt64> & ids)
|
2016-06-05 15:21:35 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// We do not log in here and do not update the modification time, as the request can be large, and often called.
|
|
|
|
const auto query = query_builder.composeLoadIdsQuery(ids);
|
2021-03-24 18:15:31 +00:00
|
|
|
return loadBase(query);
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 15:25:45 +00:00
|
|
|
BlockInputStreamPtr MySQLDictionarySource::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
|
|
|
/// We do not log in here and do not update the modification time, as the request can be large, and often called.
|
|
|
|
const auto query = query_builder.composeLoadKeysQuery(key_columns, requested_rows, ExternalQueryBuilder::AND_OR_CHAIN);
|
2021-03-24 18:15:31 +00:00
|
|
|
return loadBase(query);
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MySQLDictionarySource::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 (response == invalidate_query_response)
|
2017-05-15 14:16:10 +00:00
|
|
|
return false;
|
|
|
|
invalidate_query_response = response;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (dont_check_update_time)
|
|
|
|
return true;
|
2021-03-27 14:35:44 +00:00
|
|
|
auto connection = pool->get();
|
2020-02-27 09:34:06 +00:00
|
|
|
return getLastModification(connection, true) > last_modification;
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MySQLDictionarySource::supportsSelectiveLoad() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-15 12:44:39 +00:00
|
|
|
bool MySQLDictionarySource::hasUpdateField() const
|
|
|
|
{
|
2018-02-15 13:08:23 +00:00
|
|
|
return !update_field.empty();
|
2018-01-15 12:44:39 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 15:21:35 +00:00
|
|
|
DictionarySourcePtr MySQLDictionarySource::clone() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_unique<MySQLDictionarySource>(*this);
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MySQLDictionarySource::toString() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return "MySQL: " + db + '.' + table + (where.empty() ? "" : ", where: " + where);
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MySQLDictionarySource::quoteForLike(const std::string s)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string tmp;
|
|
|
|
tmp.reserve(s.size());
|
|
|
|
|
|
|
|
for (auto c : s)
|
|
|
|
{
|
|
|
|
if (c == '%' || c == '_' || c == '\\')
|
|
|
|
tmp.push_back('\\');
|
|
|
|
tmp.push_back(c);
|
|
|
|
}
|
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
WriteBufferFromOwnString out;
|
|
|
|
writeQuoted(tmp, out);
|
|
|
|
return out.str();
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 09:34:06 +00:00
|
|
|
LocalDateTime MySQLDictionarySource::getLastModification(mysqlxx::Pool::Entry & connection, bool allow_connection_closure) const
|
2016-06-05 15:21:35 +00:00
|
|
|
{
|
2018-08-10 04:02:56 +00:00
|
|
|
LocalDateTime modification_time{std::time(nullptr)};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (dont_check_update_time)
|
2018-08-10 04:02:56 +00:00
|
|
|
return modification_time;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto query = connection->query("SHOW TABLE STATUS LIKE " + quoteForLike(table));
|
|
|
|
|
2020-05-23 22:24:01 +00:00
|
|
|
LOG_TRACE(log, query.str());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
auto result = query.use();
|
|
|
|
|
|
|
|
size_t fetched_rows = 0;
|
|
|
|
if (auto row = result.fetch())
|
|
|
|
{
|
|
|
|
++fetched_rows;
|
2020-03-23 02:12:31 +00:00
|
|
|
static const auto UPDATE_TIME_IDX = 12;
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto & update_time_value = row[UPDATE_TIME_IDX];
|
|
|
|
|
|
|
|
if (!update_time_value.isNull())
|
|
|
|
{
|
2018-08-10 04:02:56 +00:00
|
|
|
modification_time = update_time_value.getDateTime();
|
2021-03-15 19:23:27 +00:00
|
|
|
LOG_TRACE(log, "Got modification time: {}", update_time_value.getString());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// fetch remaining rows to avoid "commands out of sync" error
|
|
|
|
while (result.fetch())
|
|
|
|
++fetched_rows;
|
|
|
|
}
|
|
|
|
|
2020-02-27 09:34:06 +00:00
|
|
|
if (close_connection && allow_connection_closure)
|
|
|
|
{
|
|
|
|
connection.disconnect();
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (0 == fetched_rows)
|
2020-05-23 22:24:01 +00:00
|
|
|
LOG_ERROR(log, "Cannot find table in SHOW TABLE STATUS result.");
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (fetched_rows > 1)
|
2020-05-23 22:24:01 +00:00
|
|
|
LOG_ERROR(log, "Found more than one table in SHOW TABLE STATUS result.");
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException("MySQLDictionarySource");
|
|
|
|
}
|
|
|
|
/// we suppose failure to get modification time is not an error, therefore return current time
|
2018-08-10 04:02:56 +00:00
|
|
|
return modification_time;
|
2016-06-05 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 16:38:24 +00:00
|
|
|
std::string MySQLDictionarySource::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"));
|
2021-03-27 14:35:44 +00:00
|
|
|
MySQLBlockInputStream block_input_stream(pool->get(), request, invalidate_sample_block, 1, close_connection);
|
2017-05-23 16:36:07 +00:00
|
|
|
return readInvalidateQuery(block_input_stream);
|
2017-05-15 14:16:10 +00:00
|
|
|
}
|
2016-06-05 15:21:35 +00:00
|
|
|
|
|
|
|
}
|
2017-04-19 00:25:57 +00:00
|
|
|
|
|
|
|
#endif
|