2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/range.h>
|
2016-12-08 02:49:04 +00:00
|
|
|
#include <boost/range/join.hpp>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Dictionaries/writeParenthesisedString.h>
|
|
|
|
#include <Dictionaries/DictionaryStructure.h>
|
|
|
|
#include <Dictionaries/ExternalQueryBuilder.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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ExternalQueryBuilder::ExternalQueryBuilder(
|
2017-04-01 07:20:54 +00:00
|
|
|
const DictionaryStructure & dict_struct,
|
|
|
|
const std::string & db,
|
|
|
|
const std::string & table,
|
|
|
|
const std::string & where,
|
2018-07-16 01:02:46 +00:00
|
|
|
IdentifierQuotingStyle quoting_style)
|
2017-04-01 07:20:54 +00:00
|
|
|
: dict_struct(dict_struct), db(db), table(table), where(where), quoting_style(quoting_style)
|
2016-12-08 02:49:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-14 02:30:03 +00:00
|
|
|
void ExternalQueryBuilder::writeQuoted(const std::string & s, WriteBuffer & out) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
switch (quoting_style)
|
|
|
|
{
|
2018-07-16 01:02:46 +00:00
|
|
|
case IdentifierQuotingStyle::None:
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString(s, out);
|
|
|
|
break;
|
|
|
|
|
2018-07-16 01:02:46 +00:00
|
|
|
case IdentifierQuotingStyle::Backticks:
|
2017-04-01 07:20:54 +00:00
|
|
|
writeBackQuotedString(s, out);
|
|
|
|
break;
|
|
|
|
|
2018-07-16 01:02:46 +00:00
|
|
|
case IdentifierQuotingStyle::DoubleQuotes:
|
2017-04-01 07:20:54 +00:00
|
|
|
writeDoubleQuotedString(s, out);
|
|
|
|
break;
|
|
|
|
}
|
2017-01-14 02:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-08 02:49:04 +00:00
|
|
|
std::string ExternalQueryBuilder::composeLoadAllQuery() const
|
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
WriteBufferFromOwnString out;
|
|
|
|
writeString("SELECT ", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
if (dict_struct.id)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-10-04 00:22:00 +00:00
|
|
|
if (!dict_struct.id->expression.empty())
|
2017-07-31 21:39:24 +00:00
|
|
|
{
|
2017-10-04 00:22:00 +00:00
|
|
|
writeParenthesisedString(dict_struct.id->expression, out);
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-10-04 00:22:00 +00:00
|
|
|
writeQuoted(dict_struct.id->name, out);
|
2017-07-31 21:39:24 +00:00
|
|
|
|
|
|
|
if (dict_struct.range_min && dict_struct.range_max)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(", ", out);
|
|
|
|
|
2017-10-04 00:22:00 +00:00
|
|
|
if (!dict_struct.range_min->expression.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-10-04 00:22:00 +00:00
|
|
|
writeParenthesisedString(dict_struct.range_min->expression, out);
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
|
|
|
|
2017-10-04 00:22:00 +00:00
|
|
|
writeQuoted(dict_struct.range_min->name, out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(", ", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-10-04 00:22:00 +00:00
|
|
|
if (!dict_struct.range_max->expression.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-10-04 00:22:00 +00:00
|
|
|
writeParenthesisedString(dict_struct.range_max->expression, out);
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(" AS ", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 00:22:00 +00:00
|
|
|
writeQuoted(dict_struct.range_max->name, out);
|
2017-07-31 21:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (dict_struct.key)
|
|
|
|
{
|
|
|
|
auto first = true;
|
|
|
|
for (const auto & key : *dict_struct.key)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
if (!first)
|
|
|
|
writeString(", ", out);
|
|
|
|
|
|
|
|
first = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
if (!key.expression.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
writeParenthesisedString(key.expression, out);
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
writeQuoted(key.name, out);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-07-31 21:39:24 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
for (const auto & attr : dict_struct.attributes)
|
|
|
|
{
|
|
|
|
writeString(", ", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
if (!attr.expression.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
writeParenthesisedString(attr.expression, out);
|
|
|
|
writeString(" AS ", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
writeQuoted(attr.name, out);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(" FROM ", out);
|
|
|
|
if (!db.empty())
|
|
|
|
{
|
|
|
|
writeQuoted(db, out);
|
|
|
|
writeChar('.', out);
|
|
|
|
}
|
|
|
|
writeQuoted(table, out);
|
|
|
|
|
|
|
|
if (!where.empty())
|
|
|
|
{
|
|
|
|
writeString(" WHERE ", out);
|
|
|
|
writeString(where, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeChar(';', out);
|
|
|
|
|
|
|
|
return out.str();
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-16 01:02:46 +00:00
|
|
|
std::string ExternalQueryBuilder::composeUpdateQuery(const std::string & update_field, const std::string & time_point) const
|
2018-01-15 12:44:39 +00:00
|
|
|
{
|
|
|
|
std::string out = composeLoadAllQuery();
|
2018-02-15 13:08:23 +00:00
|
|
|
std::string update_query;
|
2018-01-15 12:44:39 +00:00
|
|
|
|
2018-02-15 13:08:23 +00:00
|
|
|
if (!where.empty())
|
|
|
|
update_query = " AND " + update_field + " >= '" + time_point + "'";
|
|
|
|
else
|
|
|
|
update_query = " WHERE " + update_field + " >= '" + time_point + "'";
|
|
|
|
|
|
|
|
return out.insert(out.size()-1, update_query); ///This is done to insert "update_query" before "out"'s semicolon
|
2018-01-15 12:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-08 02:49:04 +00:00
|
|
|
std::string ExternalQueryBuilder::composeLoadIdsQuery(const std::vector<UInt64> & ids)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!dict_struct.id)
|
|
|
|
throw Exception{"Simple key required for method", ErrorCodes::UNSUPPORTED_METHOD};
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
WriteBufferFromOwnString out;
|
|
|
|
writeString("SELECT ", out);
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-10-04 00:22:00 +00:00
|
|
|
if (!dict_struct.id->expression.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-10-04 00:22:00 +00:00
|
|
|
writeParenthesisedString(dict_struct.id->expression, out);
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-10-04 00:22:00 +00:00
|
|
|
writeQuoted(dict_struct.id->name, out);
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
for (const auto & attr : dict_struct.attributes)
|
|
|
|
{
|
|
|
|
writeString(", ", out);
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
if (!attr.expression.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
writeParenthesisedString(attr.expression, out);
|
|
|
|
writeString(" AS ", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
writeQuoted(attr.name, out);
|
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(" FROM ", out);
|
|
|
|
if (!db.empty())
|
|
|
|
{
|
|
|
|
writeQuoted(db, out);
|
|
|
|
writeChar('.', out);
|
|
|
|
}
|
|
|
|
writeQuoted(table, out);
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(" WHERE ", out);
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
if (!where.empty())
|
|
|
|
{
|
|
|
|
writeString(where, out);
|
|
|
|
writeString(" AND ", out);
|
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-10-04 00:22:00 +00:00
|
|
|
writeQuoted(dict_struct.id->name, out);
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(" IN (", out);
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
auto first = true;
|
|
|
|
for (const auto id : ids)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
writeString(", ", out);
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
first = false;
|
|
|
|
writeString(DB::toString(id), out);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(");", out);
|
|
|
|
|
|
|
|
return out.str();
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string ExternalQueryBuilder::composeLoadKeysQuery(
|
2017-05-25 19:52:05 +00:00
|
|
|
const Columns & key_columns,
|
2017-07-21 06:35:58 +00:00
|
|
|
const std::vector<size_t> & requested_rows,
|
2017-04-01 07:20:54 +00:00
|
|
|
LoadKeysMethod method)
|
2016-12-08 02:49:04 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!dict_struct.key)
|
|
|
|
throw Exception{"Composite key required for method", ErrorCodes::UNSUPPORTED_METHOD};
|
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
WriteBufferFromOwnString out;
|
|
|
|
writeString("SELECT ", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
auto first = true;
|
|
|
|
for (const auto & key_or_attribute : boost::join(*dict_struct.key, dict_struct.attributes))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
if (!first)
|
|
|
|
writeString(", ", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
first = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
if (!key_or_attribute.expression.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
writeParenthesisedString(key_or_attribute.expression, out);
|
|
|
|
writeString(" AS ", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
writeQuoted(key_or_attribute.name, out);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(" FROM ", out);
|
|
|
|
if (!db.empty())
|
|
|
|
{
|
|
|
|
writeQuoted(db, out);
|
|
|
|
writeChar('.', out);
|
|
|
|
}
|
|
|
|
writeQuoted(table, out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(" WHERE ", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
if (!where.empty())
|
|
|
|
{
|
|
|
|
writeString("(", out);
|
|
|
|
writeString(where, out);
|
|
|
|
writeString(") AND (", out);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
if (method == AND_OR_CHAIN)
|
|
|
|
{
|
|
|
|
first = true;
|
|
|
|
for (const auto row : requested_rows)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
writeString(" OR ", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
first = false;
|
|
|
|
composeKeyCondition(key_columns, row, out);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-07-31 21:39:24 +00:00
|
|
|
}
|
|
|
|
else if (method == IN_WITH_TUPLES)
|
|
|
|
{
|
|
|
|
writeString(composeKeyTupleDefinition(), out);
|
|
|
|
writeString(" IN (", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
first = true;
|
|
|
|
for (const auto row : requested_rows)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
if (!first)
|
|
|
|
writeString(", ", out);
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
composeKeyTuple(key_columns, row, out);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
writeString(")", out);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 21:39:24 +00:00
|
|
|
if (!where.empty())
|
|
|
|
{
|
|
|
|
writeString(")", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeString(";", out);
|
|
|
|
|
|
|
|
return out.str();
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
void ExternalQueryBuilder::composeKeyCondition(const Columns & key_columns, const size_t row, WriteBuffer & out) const
|
2016-12-08 02:49:04 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString("(", out);
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto keys_size = key_columns.size();
|
|
|
|
auto first = true;
|
|
|
|
for (const auto i : ext::range(0, keys_size))
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
writeString(" AND ", out);
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
first = false;
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto & key_description = (*dict_struct.key)[i];
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// key_i=value_i
|
|
|
|
writeString(key_description.name, out);
|
|
|
|
writeString("=", out);
|
2018-06-08 01:51:55 +00:00
|
|
|
key_description.type->serializeTextQuoted(*key_columns[i], row, out, format_settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString(")", out);
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string ExternalQueryBuilder::composeKeyTupleDefinition() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!dict_struct.key)
|
|
|
|
throw Exception{"Composite key required for method", ErrorCodes::UNSUPPORTED_METHOD};
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string result{"("};
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto first = true;
|
|
|
|
for (const auto & key : *dict_struct.key)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
result += ", ";
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
first = false;
|
|
|
|
result += key.name;
|
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
result += ")";
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return result;
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
void ExternalQueryBuilder::composeKeyTuple(const Columns & key_columns, const size_t row, WriteBuffer & out) const
|
2016-12-08 02:49:04 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString("(", out);
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto keys_size = key_columns.size();
|
|
|
|
auto first = true;
|
|
|
|
for (const auto i : ext::range(0, keys_size))
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
writeString(", ", out);
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
first = false;
|
2018-06-08 01:51:55 +00:00
|
|
|
(*dict_struct.key)[i].type->serializeTextQuoted(*key_columns[i], row, out, format_settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-12-08 02:49:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString(")", out);
|
2016-12-08 02:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|