ClickHouse/src/Parsers/ASTSystemQuery.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

213 lines
6.8 KiB
C++
Raw Normal View History

2021-10-12 18:11:00 +00:00
#include <Parsers/ASTIdentifier.h>
#include <Parsers/IAST.h>
#include <Parsers/ASTSystemQuery.h>
#include <Common/quoteString.h>
2020-11-09 16:05:40 +00:00
#include <IO/Operators.h>
2021-09-20 18:33:25 +00:00
#include <magic_enum.hpp>
namespace DB
{
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
{
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 : "");
auto print_database_table = [&]
{
settings.ostr << " ";
2021-11-11 13:28:18 +00:00
if (database)
{
2021-10-12 18:11:00 +00:00
settings.ostr << (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(getDatabase())
<< (settings.hilite ? hilite_none : "") << ".";
}
2021-10-12 18:11:00 +00:00
settings.ostr << (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(getTable())
<< (settings.hilite ? hilite_none : "");
};
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-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();
}
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-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-05-17 12:44:22 +00:00
};
auto print_on_volume = [&]
{
2021-01-07 19:19:33 +00:00
settings.ostr << (settings.hilite ? hilite_keyword : "") << " ON VOLUME "
<< (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(storage_policy)
<< (settings.hilite ? hilite_none : "")
<< "."
<< (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(volume)
<< (settings.hilite ? hilite_none : "");
};
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())
formatOnCluster(settings);
2019-12-23 03:48:34 +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
|| 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
|| 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)
{
2021-11-11 13:28:18 +00:00
if (table)
print_database_table();
else if (!volume.empty())
print_on_volume();
}
SYSTEM RESTORE REPLICA replica [ON CLUSTER cluster] (#13652) * initial commit: add setting and stub * typo * added test stub * fix * wip merging new integration test and code proto * adding steps interpreters * adding firstly proposed solution (moving parts etc) * added checking zookeeper path existence * fixing the include * fixing and sorting includes * fixing outdated struct * fix the name * added ast ptr as level of indirection * fix ref * updating the changes * working on test stub * fix iterator -> reference * revert rocksdb submodule update * fixed show privileges test * updated the test stub * replaced rand() with thread_local_rng(), updated the tests updated the test fixed test config path test fix removed error messages fixed the test updated the test fixed string literal fixed literal typo: = * fixed the empty replica error message * updated the test and the code with logs * updated the possible test cases, updated * added the code/test milestone comments * updated the test (added more testcases) * replaced native assert with CH one * individual replicas recursive delete fix * updated the AS db.name AST * two small logging fixes * manually generated AST fixes * Updated the test, added the possible algo change * Some thoughts about optimizing the solution: ALTER MOVE PARTITION .. TO TABLE -> move to detached/ + ALTER ... ATTACH * fix * Removed the replica sync in test as it's invalid * Some test tweaks * tmp * Rewrote the algo by using the executeQuery instead of hand-crafting the ASTPtr. Two questions still active. * tr: logging active parts * Extracted the parts moving algo into a separate helper function * Fixed the test data and the queries slightly * Replaced query to system.parts to direct invocation, started building the test that breaks on various parts. * Added the case for tables when at least one replica is alive * Updated the test to test replicas restoration by detaching/attaching * Altered the test to check restoration without replica restart * Added the tables swap in the start if the server failed last time * Hotfix when only /replicas/replica... path was deleted * Restore ZK paths while creating a replicated MergeTree table * Updated the docs, fixed the algo for individual replicas restoration case * Initial parts table storage fix, tests sync fix * Reverted individual replica restoration to general algo * Slightly optimised getDataParts * Trying another solution with parts detaching * Rewrote algo without any steps, added ON CLUSTER support * Attaching parts from other replica on restoration * Getting part checksums from ZK * Removed ON CLUSTER, finished working solution * Multiple small changes after review * Fixing parallel test * Supporting rewritten form on cluster * Test fix * Moar logging * Using source replica as checksum provider * improve test, remove some code from parser * Trying solution with move to detached + forget * Moving all parts (not only Committed) to detached * Edited docs for RESTORE REPLICA * Re-merging * minor fixes Co-authored-by: Alexander Tokmakov <avtokmakov@yandex-team.ru>
2021-06-20 08:24:43 +00:00
else if ( type == Type::RESTART_REPLICA
|| type == Type::RESTORE_REPLICA
|| type == Type::SYNC_REPLICA
|| type == Type::WAIT_LOADING_PARTS
SYSTEM RESTORE REPLICA replica [ON CLUSTER cluster] (#13652) * initial commit: add setting and stub * typo * added test stub * fix * wip merging new integration test and code proto * adding steps interpreters * adding firstly proposed solution (moving parts etc) * added checking zookeeper path existence * fixing the include * fixing and sorting includes * fixing outdated struct * fix the name * added ast ptr as level of indirection * fix ref * updating the changes * working on test stub * fix iterator -> reference * revert rocksdb submodule update * fixed show privileges test * updated the test stub * replaced rand() with thread_local_rng(), updated the tests updated the test fixed test config path test fix removed error messages fixed the test updated the test fixed string literal fixed literal typo: = * fixed the empty replica error message * updated the test and the code with logs * updated the possible test cases, updated * added the code/test milestone comments * updated the test (added more testcases) * replaced native assert with CH one * individual replicas recursive delete fix * updated the AS db.name AST * two small logging fixes * manually generated AST fixes * Updated the test, added the possible algo change * Some thoughts about optimizing the solution: ALTER MOVE PARTITION .. TO TABLE -> move to detached/ + ALTER ... ATTACH * fix * Removed the replica sync in test as it's invalid * Some test tweaks * tmp * Rewrote the algo by using the executeQuery instead of hand-crafting the ASTPtr. Two questions still active. * tr: logging active parts * Extracted the parts moving algo into a separate helper function * Fixed the test data and the queries slightly * Replaced query to system.parts to direct invocation, started building the test that breaks on various parts. * Added the case for tables when at least one replica is alive * Updated the test to test replicas restoration by detaching/attaching * Altered the test to check restoration without replica restart * Added the tables swap in the start if the server failed last time * Hotfix when only /replicas/replica... path was deleted * Restore ZK paths while creating a replicated MergeTree table * Updated the docs, fixed the algo for individual replicas restoration case * Initial parts table storage fix, tests sync fix * Reverted individual replica restoration to general algo * Slightly optimised getDataParts * Trying another solution with parts detaching * Rewrote algo without any steps, added ON CLUSTER support * Attaching parts from other replica on restoration * Getting part checksums from ZK * Removed ON CLUSTER, finished working solution * Multiple small changes after review * Fixing parallel test * Supporting rewritten form on cluster * Test fix * Moar logging * Using source replica as checksum provider * improve test, remove some code from parser * Trying solution with move to detached + forget * Moving all parts (not only Committed) to detached * Edited docs for RESTORE REPLICA * Re-merging * minor fixes Co-authored-by: Alexander Tokmakov <avtokmakov@yandex-team.ru>
2021-06-20 08:24:43 +00:00
|| type == Type::FLUSH_DISTRIBUTED
|| type == Type::RELOAD_DICTIONARY
|| type == Type::RELOAD_MODEL
|| type == Type::RELOAD_FUNCTION
|| type == Type::RESTART_DISK)
{
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);
}
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
}
else if (type == Type::UNFREEZE)
{
settings.ostr << (settings.hilite ? hilite_identifier : "") << backQuoteIfNeed(backup_name);
}
}
}