2021-10-12 18:11:00 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2017-08-04 15:54:00 +00:00
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Parsers/ASTSystemQuery.h>
|
2019-10-08 18:42:22 +00:00
|
|
|
#include <Common/quoteString.h>
|
2020-11-09 16:05:40 +00:00
|
|
|
#include <IO/Operators.h>
|
2017-08-04 15:54:00 +00:00
|
|
|
|
2021-09-20 18:33:25 +00:00
|
|
|
#include <magic_enum.hpp>
|
2017-08-04 15:54:00 +00:00
|
|
|
|
2017-08-06 20:26:23 +00:00
|
|
|
namespace DB
|
2017-08-04 15:54:00 +00:00
|
|
|
{
|
|
|
|
|
2021-09-20 15:15:23 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
std::vector<std::string> getTypeIndexToTypeName()
|
|
|
|
{
|
|
|
|
constexpr std::size_t types_size = magic_enum::enum_count<ASTSystemQuery::Type>();
|
|
|
|
|
|
|
|
std::vector<std::string> type_index_to_type_name;
|
|
|
|
type_index_to_type_name.resize(types_size);
|
|
|
|
|
|
|
|
auto entries = magic_enum::enum_entries<ASTSystemQuery::Type>();
|
|
|
|
for (const auto & [entry, str] : entries)
|
2021-09-20 18:33:25 +00:00
|
|
|
{
|
|
|
|
auto str_copy = String(str);
|
|
|
|
std::replace(str_copy.begin(), str_copy.end(), '_', ' ');
|
|
|
|
type_index_to_type_name[static_cast<UInt64>(entry)] = std::move(str_copy);
|
|
|
|
}
|
2021-09-20 15:15:23 +00:00
|
|
|
|
|
|
|
return type_index_to_type_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * ASTSystemQuery::typeToString(Type type)
|
|
|
|
{
|
2021-09-20 23:23:34 +00:00
|
|
|
/** During parsing if SystemQuery is not parsed properly it is added to Expected variants as description check IParser.h.
|
|
|
|
* Description string must be statically allocated.
|
|
|
|
*/
|
2021-09-20 15:15:23 +00:00
|
|
|
static std::vector<std::string> type_index_to_type_name = getTypeIndexToTypeName();
|
|
|
|
const auto & type_name = type_index_to_type_name[static_cast<UInt64>(type)];
|
|
|
|
return type_name.data();
|
|
|
|
}
|
|
|
|
|
2021-10-12 18:11:00 +00:00
|
|
|
String ASTSystemQuery::getDatabase() const
|
|
|
|
{
|
|
|
|
String name;
|
|
|
|
tryGetIdentifierNameInto(database, name);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
String ASTSystemQuery::getTable() const
|
|
|
|
{
|
|
|
|
String name;
|
|
|
|
tryGetIdentifierNameInto(table, name);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTSystemQuery::setDatabase(const String & name)
|
|
|
|
{
|
|
|
|
if (name.empty())
|
|
|
|
database.reset();
|
|
|
|
else
|
|
|
|
database = std::make_shared<ASTIdentifier>(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTSystemQuery::setTable(const String & name)
|
|
|
|
{
|
|
|
|
if (name.empty())
|
|
|
|
table.reset();
|
|
|
|
else
|
|
|
|
table = std::make_shared<ASTIdentifier>(name);
|
|
|
|
}
|
|
|
|
|
2017-12-01 18:36:55 +00:00
|
|
|
void ASTSystemQuery::formatImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const
|
2017-08-06 20:26:23 +00:00
|
|
|
{
|
2020-06-23 12:01:51 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << "SYSTEM ";
|
2021-09-20 15:15:23 +00:00
|
|
|
settings.ostr << typeToString(type) << (settings.hilite ? hilite_none : "");
|
2017-08-06 20:26:23 +00:00
|
|
|
|
2019-12-15 13:48:11 +00:00
|
|
|
auto print_database_table = [&]
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
|
|
|
settings.ostr << " ";
|
2021-10-12 18:11:00 +00:00
|
|
|
if (!getDatabase().empty())
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
2021-10-12 18:11:00 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(getDatabase())
|
2018-05-21 13:49:54 +00:00
|
|
|
<< (settings.hilite ? hilite_none : "") << ".";
|
|
|
|
}
|
2021-10-12 18:11:00 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(getTable())
|
2018-05-21 13:49:54 +00:00
|
|
|
<< (settings.hilite ? hilite_none : "");
|
|
|
|
};
|
|
|
|
|
2020-10-20 15:10:24 +00:00
|
|
|
auto print_drop_replica = [&]
|
|
|
|
{
|
2020-06-23 12:01:51 +00:00
|
|
|
settings.ostr << " " << quoteString(replica);
|
2021-10-12 18:11:00 +00:00
|
|
|
if (!getTable().empty())
|
2020-06-05 07:03:51 +00:00
|
|
|
{
|
2020-06-23 12:01:51 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << " FROM TABLE"
|
|
|
|
<< (settings.hilite ? hilite_none : "");
|
2020-05-17 12:44:22 +00:00
|
|
|
print_database_table();
|
2020-06-05 07:03:51 +00:00
|
|
|
}
|
|
|
|
else if (!replica_zk_path.empty())
|
2020-05-17 12:44:22 +00:00
|
|
|
{
|
2020-06-23 12:01:51 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << " FROM ZKPATH "
|
|
|
|
<< (settings.hilite ? hilite_none : "") << quoteString(replica_zk_path);
|
2020-05-17 12:44:22 +00:00
|
|
|
}
|
2021-10-12 18:11:00 +00:00
|
|
|
else if (!getDatabase().empty())
|
2020-06-05 07:03:51 +00:00
|
|
|
{
|
2020-06-23 12:01:51 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << " FROM DATABASE "
|
|
|
|
<< (settings.hilite ? hilite_none : "");
|
2021-10-12 18:11:00 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(getDatabase())
|
2020-06-23 12:01:51 +00:00
|
|
|
<< (settings.hilite ? hilite_none : "");
|
2020-06-05 07:03:51 +00:00
|
|
|
}
|
2020-05-17 12:44:22 +00:00
|
|
|
};
|
|
|
|
|
2020-10-20 15:10:24 +00:00
|
|
|
auto print_on_volume = [&]
|
|
|
|
{
|
2021-01-07 19:19:33 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << " ON VOLUME "
|
2020-10-20 15:10:24 +00:00
|
|
|
<< (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(storage_policy)
|
|
|
|
<< (settings.hilite ? hilite_none : "")
|
|
|
|
<< "."
|
|
|
|
<< (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(volume)
|
|
|
|
<< (settings.hilite ? hilite_none : "");
|
|
|
|
};
|
|
|
|
|
2019-12-19 03:46:20 +00:00
|
|
|
if (!cluster.empty())
|
2019-12-19 07:54:43 +00:00
|
|
|
formatOnCluster(settings);
|
2019-12-23 03:48:34 +00:00
|
|
|
|
2018-05-21 13:49:54 +00:00
|
|
|
if ( type == Type::STOP_MERGES
|
|
|
|
|| type == Type::START_MERGES
|
2019-08-01 15:36:12 +00:00
|
|
|
|| type == Type::STOP_TTL_MERGES
|
|
|
|
|| type == Type::START_TTL_MERGES
|
2019-09-03 14:50:49 +00:00
|
|
|
|| type == Type::STOP_MOVES
|
|
|
|
|| type == Type::START_MOVES
|
2018-05-21 13:49:54 +00:00
|
|
|
|| type == Type::STOP_FETCHES
|
|
|
|
|| type == Type::START_FETCHES
|
|
|
|
|| type == Type::STOP_REPLICATED_SENDS
|
2019-02-02 11:28:43 +00:00
|
|
|
|| type == Type::START_REPLICATED_SENDS
|
2018-05-21 13:49:54 +00:00
|
|
|
|| type == Type::STOP_REPLICATION_QUEUES
|
2019-04-22 15:11:16 +00:00
|
|
|
|| type == Type::START_REPLICATION_QUEUES
|
|
|
|
|| type == Type::STOP_DISTRIBUTED_SENDS
|
|
|
|
|| type == Type::START_DISTRIBUTED_SENDS)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
2021-10-12 18:11:00 +00:00
|
|
|
if (!getTable().empty())
|
2018-05-21 13:49:54 +00:00
|
|
|
print_database_table();
|
2020-10-20 15:10:24 +00:00
|
|
|
else if (!volume.empty())
|
|
|
|
print_on_volume();
|
2018-05-21 13:49:54 +00:00
|
|
|
}
|
2021-06-20 08:24:43 +00:00
|
|
|
else if ( type == Type::RESTART_REPLICA
|
|
|
|
|| type == Type::RESTORE_REPLICA
|
|
|
|
|| type == Type::SYNC_REPLICA
|
|
|
|
|| type == Type::FLUSH_DISTRIBUTED
|
|
|
|
|| type == Type::RELOAD_DICTIONARY)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
|
|
|
print_database_table();
|
|
|
|
}
|
2020-05-17 12:44:22 +00:00
|
|
|
else if (type == Type::DROP_REPLICA)
|
2021-01-07 19:19:33 +00:00
|
|
|
{
|
2020-05-17 12:44:22 +00:00
|
|
|
print_drop_replica();
|
2021-01-07 19:19:33 +00:00
|
|
|
}
|
|
|
|
else if (type == Type::SUSPEND)
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << " FOR "
|
|
|
|
<< (settings.hilite ? hilite_none : "") << seconds
|
|
|
|
<< (settings.hilite ? hilite_keyword : "") << " SECOND"
|
|
|
|
<< (settings.hilite ? hilite_none : "");
|
|
|
|
}
|
2017-08-06 20:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-30 21:25:44 +00:00
|
|
|
}
|