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)
|
|
|
|
{
|
2021-10-21 12:50:31 +00:00
|
|
|
if (database)
|
|
|
|
{
|
2021-11-11 13:28:18 +00:00
|
|
|
std::erase(children, database);
|
|
|
|
database.reset();
|
2021-10-21 12:50:31 +00:00
|
|
|
}
|
2021-11-11 13:28:18 +00:00
|
|
|
|
|
|
|
if (!name.empty())
|
2021-10-21 12:50:31 +00:00
|
|
|
{
|
2021-10-12 18:11:00 +00:00
|
|
|
database = std::make_shared<ASTIdentifier>(name);
|
2021-10-21 12:50:31 +00:00
|
|
|
children.push_back(database);
|
|
|
|
}
|
2021-10-12 18:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTSystemQuery::setTable(const String & name)
|
|
|
|
{
|
2021-10-21 12:50:31 +00:00
|
|
|
if (table)
|
|
|
|
{
|
2021-11-11 13:28:18 +00:00
|
|
|
std::erase(children, table);
|
|
|
|
table.reset();
|
2021-10-21 12:50:31 +00:00
|
|
|
}
|
2021-11-11 13:28:18 +00:00
|
|
|
|
|
|
|
if (!name.empty())
|
2021-10-21 12:50:31 +00:00
|
|
|
{
|
2021-10-12 18:11:00 +00:00
|
|
|
table = std::make_shared<ASTIdentifier>(name);
|
2021-10-21 12:50:31 +00:00
|
|
|
children.push_back(table);
|
|
|
|
}
|
2021-10-12 18:11:00 +00:00
|
|
|
}
|
|
|
|
|
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-11-11 13:28:18 +00:00
|
|
|
if (database)
|
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-11-11 13:28:18 +00:00
|
|
|
if (table)
|
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-11-11 13:28:18 +00:00
|
|
|
else if (database)
|
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 : "");
|
|
|
|
};
|
|
|
|
|
2022-02-17 19:41:41 +00:00
|
|
|
auto print_identifier = [&](const String & identifier)
|
|
|
|
{
|
|
|
|
settings.ostr << " " << (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(identifier)
|
|
|
|
<< (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-11-11 13:28:18 +00:00
|
|
|
if (table)
|
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
|
2022-12-12 14:33:42 +00:00
|
|
|
|| type == Type::WAIT_LOADING_PARTS
|
2021-06-20 08:24:43 +00:00
|
|
|
|| type == Type::FLUSH_DISTRIBUTED
|
2022-02-17 19:41:41 +00:00
|
|
|
|| type == Type::RELOAD_DICTIONARY
|
|
|
|
|| type == Type::RELOAD_MODEL
|
|
|
|
|| type == Type::RELOAD_FUNCTION
|
|
|
|
|| type == Type::RESTART_DISK)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
2022-02-17 19:41:41 +00:00
|
|
|
if (table)
|
|
|
|
print_database_table();
|
|
|
|
else if (!target_model.empty())
|
|
|
|
print_identifier(target_model);
|
|
|
|
else if (!target_function.empty())
|
|
|
|
print_identifier(target_function);
|
|
|
|
else if (!disk.empty())
|
|
|
|
print_identifier(disk);
|
2018-05-21 13:49:54 +00:00
|
|
|
}
|
2022-05-06 16:37:20 +00:00
|
|
|
else if (type == Type::SYNC_DATABASE_REPLICA)
|
|
|
|
{
|
|
|
|
print_identifier(database->as<ASTIdentifier>()->name());
|
|
|
|
}
|
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 : "");
|
|
|
|
}
|
2022-04-25 19:57:13 +00:00
|
|
|
else if (type == Type::DROP_FILESYSTEM_CACHE)
|
|
|
|
{
|
|
|
|
if (!filesystem_cache_path.empty())
|
2022-04-26 09:24:24 +00:00
|
|
|
settings.ostr << (settings.hilite ? hilite_none : "") << " " << filesystem_cache_path;
|
2022-04-25 19:57:13 +00:00
|
|
|
}
|
2022-06-21 09:17:52 +00:00
|
|
|
else if (type == Type::UNFREEZE)
|
|
|
|
{
|
|
|
|
settings.ostr << (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(backup_name);
|
|
|
|
}
|
2017-08-06 20:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-30 21:25:44 +00:00
|
|
|
}
|