2021-08-18 22:19:14 +00:00
|
|
|
#include <Parsers/ParserBackupQuery.h>
|
|
|
|
#include <Parsers/ASTBackupQuery.h>
|
2022-11-11 15:26:04 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2021-11-26 15:49:40 +00:00
|
|
|
#include <Parsers/ASTIdentifier_fwd.h>
|
2022-04-26 07:48:40 +00:00
|
|
|
#include <Parsers/ASTSetQuery.h>
|
2021-08-18 22:19:14 +00:00
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <Parsers/ParserPartition.h>
|
|
|
|
#include <Parsers/ParserSetQuery.h>
|
|
|
|
#include <Parsers/parseDatabaseAndTableName.h>
|
2022-04-26 07:48:40 +00:00
|
|
|
#include <Common/assert_cast.h>
|
|
|
|
#include <boost/range/algorithm_ext/erase.hpp>
|
2021-08-18 22:19:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
using Kind = ASTBackupQuery::Kind;
|
|
|
|
using Element = ASTBackupQuery::Element;
|
|
|
|
using ElementType = ASTBackupQuery::ElementType;
|
|
|
|
|
2022-05-29 19:53:56 +00:00
|
|
|
bool parsePartitions(IParser::Pos & pos, Expected & expected, std::optional<ASTs> & partitions)
|
2021-08-18 22:19:14 +00:00
|
|
|
{
|
|
|
|
if (!ParserKeyword{"PARTITION"}.ignore(pos, expected) && !ParserKeyword{"PARTITIONS"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ASTs result;
|
|
|
|
auto parse_list_element = [&]
|
|
|
|
{
|
|
|
|
ASTPtr ast;
|
|
|
|
if (!ParserPartition{}.parse(pos, ast, expected))
|
|
|
|
return false;
|
2022-01-18 11:45:22 +00:00
|
|
|
result.push_back(ast);
|
2021-08-18 22:19:14 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
if (!ParserList::parseUtil(pos, expected, parse_list_element, false))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
partitions = std::move(result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-11 11:36:02 +00:00
|
|
|
bool parseExceptDatabases(IParser::Pos & pos, Expected & expected, std::set<String> & except_databases)
|
2022-01-18 11:45:22 +00:00
|
|
|
{
|
2022-06-06 13:52:34 +00:00
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
2022-01-18 11:45:22 +00:00
|
|
|
{
|
2022-06-11 11:36:02 +00:00
|
|
|
if (!ParserKeyword{"EXCEPT DATABASE"}.ignore(pos, expected) && !ParserKeyword{"EXCEPT DATABASES"}.ignore(pos, expected))
|
2022-01-18 11:45:22 +00:00
|
|
|
return false;
|
|
|
|
|
2022-06-06 13:52:34 +00:00
|
|
|
std::set<String> result;
|
|
|
|
auto parse_list_element = [&]
|
|
|
|
{
|
|
|
|
ASTPtr ast;
|
|
|
|
if (!ParserIdentifier{}.parse(pos, ast, expected))
|
|
|
|
return false;
|
|
|
|
result.insert(getIdentifierName(ast));
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
if (!ParserList::parseUtil(pos, expected, parse_list_element, false))
|
|
|
|
return false;
|
|
|
|
|
2022-06-11 11:36:02 +00:00
|
|
|
except_databases = std::move(result);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseExceptTables(IParser::Pos & pos, Expected & expected, const std::optional<String> & database_name, std::set<DatabaseAndTableName> & except_tables)
|
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
if (!ParserKeyword{"EXCEPT TABLE"}.ignore(pos, expected) && !ParserKeyword{"EXCEPT TABLES"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::set<DatabaseAndTableName> result;
|
|
|
|
auto parse_list_element = [&]
|
|
|
|
{
|
|
|
|
DatabaseAndTableName table_name;
|
|
|
|
if (database_name)
|
|
|
|
{
|
|
|
|
ASTPtr ast;
|
|
|
|
if (!ParserIdentifier{}.parse(pos, ast, expected))
|
|
|
|
return false;
|
|
|
|
table_name.first = *database_name;
|
|
|
|
table_name.second = getIdentifierName(ast);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!parseDatabaseAndTableName(pos, expected, table_name.first, table_name.second))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.emplace(std::move(table_name));
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
if (!ParserList::parseUtil(pos, expected, parse_list_element, false))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
except_tables = std::move(result);
|
2022-06-06 13:52:34 +00:00
|
|
|
return true;
|
|
|
|
});
|
2022-01-18 11:45:22 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 22:11:13 +00:00
|
|
|
bool parseElement(IParser::Pos & pos, Expected & expected, Element & element)
|
2021-08-18 22:19:14 +00:00
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
2022-06-11 11:36:02 +00:00
|
|
|
if (ParserKeyword{"TABLE"}.ignore(pos, expected) || ParserKeyword{"DICTIONARY"}.ignore(pos, expected) ||
|
|
|
|
ParserKeyword{"VIEW"}.ignore(pos, expected))
|
2022-01-18 11:45:22 +00:00
|
|
|
{
|
2022-06-06 13:52:34 +00:00
|
|
|
element.type = ElementType::TABLE;
|
|
|
|
if (!parseDatabaseAndTableName(pos, expected, element.database_name, element.table_name))
|
2022-01-18 11:45:22 +00:00
|
|
|
return false;
|
2022-06-06 13:52:34 +00:00
|
|
|
|
|
|
|
element.new_database_name = element.database_name;
|
|
|
|
element.new_table_name = element.table_name;
|
|
|
|
if (ParserKeyword("AS").ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (!parseDatabaseAndTableName(pos, expected, element.new_database_name, element.new_table_name))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
parsePartitions(pos, expected, element.partitions);
|
|
|
|
return true;
|
2022-01-18 11:45:22 +00:00
|
|
|
}
|
2021-08-18 22:19:14 +00:00
|
|
|
|
2022-06-06 13:52:34 +00:00
|
|
|
if (ParserKeyword{"TEMPORARY TABLE"}.ignore(pos, expected))
|
2021-08-18 22:19:14 +00:00
|
|
|
{
|
2022-06-11 11:36:02 +00:00
|
|
|
element.type = ElementType::TEMPORARY_TABLE;
|
2022-06-06 13:52:34 +00:00
|
|
|
|
|
|
|
ASTPtr ast;
|
|
|
|
if (!ParserIdentifier{}.parse(pos, ast, expected))
|
|
|
|
return false;
|
|
|
|
element.table_name = getIdentifierName(ast);
|
|
|
|
element.new_table_name = element.table_name;
|
|
|
|
|
|
|
|
if (ParserKeyword("AS").ignore(pos, expected))
|
2022-01-18 11:45:22 +00:00
|
|
|
{
|
2022-06-06 13:52:34 +00:00
|
|
|
ast = nullptr;
|
|
|
|
if (!ParserIdentifier{}.parse(pos, ast, expected))
|
2022-05-07 15:14:43 +00:00
|
|
|
return false;
|
2022-06-06 13:52:34 +00:00
|
|
|
element.new_table_name = getIdentifierName(ast);
|
2022-01-18 11:45:22 +00:00
|
|
|
}
|
2022-06-06 13:52:34 +00:00
|
|
|
|
|
|
|
return true;
|
2021-08-18 22:19:14 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 13:52:34 +00:00
|
|
|
if (ParserKeyword{"DATABASE"}.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
element.type = ElementType::DATABASE;
|
2021-08-18 22:19:14 +00:00
|
|
|
|
2022-06-06 13:52:34 +00:00
|
|
|
ASTPtr ast;
|
|
|
|
if (!ParserIdentifier{}.parse(pos, ast, expected))
|
|
|
|
return false;
|
|
|
|
element.database_name = getIdentifierName(ast);
|
|
|
|
element.new_database_name = element.database_name;
|
|
|
|
|
|
|
|
if (ParserKeyword("AS").ignore(pos, expected))
|
|
|
|
{
|
|
|
|
ast = nullptr;
|
|
|
|
if (!ParserIdentifier{}.parse(pos, ast, expected))
|
|
|
|
return false;
|
|
|
|
element.new_database_name = getIdentifierName(ast);
|
|
|
|
}
|
|
|
|
|
2022-06-11 11:36:02 +00:00
|
|
|
parseExceptTables(pos, expected, element.database_name, element.except_tables);
|
2022-06-06 13:52:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-29 22:11:13 +00:00
|
|
|
if (ParserKeyword{"ALL"}.ignore(pos, expected))
|
2022-05-07 15:14:43 +00:00
|
|
|
{
|
2022-06-11 11:36:02 +00:00
|
|
|
element.type = ElementType::ALL;
|
|
|
|
parseExceptDatabases(pos, expected, element.except_databases);
|
|
|
|
parseExceptTables(pos, expected, {}, element.except_tables);
|
2022-06-06 13:52:34 +00:00
|
|
|
return true;
|
2022-05-07 15:14:43 +00:00
|
|
|
}
|
2022-01-18 11:45:22 +00:00
|
|
|
|
2022-06-06 13:52:34 +00:00
|
|
|
return false;
|
2021-08-18 22:19:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-29 22:11:13 +00:00
|
|
|
bool parseElements(IParser::Pos & pos, Expected & expected, std::vector<Element> & elements)
|
2021-08-18 22:19:14 +00:00
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
std::vector<Element> result;
|
|
|
|
|
|
|
|
auto parse_element = [&]
|
|
|
|
{
|
|
|
|
Element element;
|
2023-03-29 22:11:13 +00:00
|
|
|
if (parseElement(pos, expected, element))
|
2021-08-18 22:19:14 +00:00
|
|
|
{
|
|
|
|
result.emplace_back(std::move(element));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!ParserList::parseUtil(pos, expected, parse_element, false))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
elements = std::move(result);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-05 14:18:23 +00:00
|
|
|
bool parseBackupName(IParser::Pos & pos, Expected & expected, ASTPtr & backup_name)
|
|
|
|
{
|
2022-11-11 15:26:04 +00:00
|
|
|
if (!ParserIdentifierWithOptionalParameters{}.parse(pos, backup_name, expected))
|
|
|
|
return false;
|
2022-11-14 18:28:19 +00:00
|
|
|
|
2022-11-11 15:26:04 +00:00
|
|
|
backup_name->as<ASTFunction &>().kind = ASTFunction::Kind::BACKUP_NAME;
|
|
|
|
return true;
|
2021-11-05 14:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool parseBaseBackupSetting(IParser::Pos & pos, Expected & expected, ASTPtr & base_backup_name)
|
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
return ParserKeyword{"base_backup"}.ignore(pos, expected)
|
|
|
|
&& ParserToken(TokenType::Equals).ignore(pos, expected)
|
|
|
|
&& parseBackupName(pos, expected, base_backup_name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-01 13:36:32 +00:00
|
|
|
bool parseClusterHostIDs(IParser::Pos & pos, Expected & expected, ASTPtr & cluster_host_ids)
|
|
|
|
{
|
|
|
|
return ParserArray{}.parse(pos, cluster_host_ids, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseClusterHostIDsSetting(IParser::Pos & pos, Expected & expected, ASTPtr & cluster_host_ids)
|
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
return ParserKeyword{"cluster_host_ids"}.ignore(pos, expected)
|
|
|
|
&& ParserToken(TokenType::Equals).ignore(pos, expected)
|
|
|
|
&& parseClusterHostIDs(pos, expected, cluster_host_ids);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseSettings(IParser::Pos & pos, Expected & expected, ASTPtr & settings, ASTPtr & base_backup_name, ASTPtr & cluster_host_ids)
|
2021-08-18 22:19:14 +00:00
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
if (!ParserKeyword{"SETTINGS"}.ignore(pos, expected))
|
|
|
|
return false;
|
|
|
|
|
2022-05-01 13:36:32 +00:00
|
|
|
SettingsChanges settings_changes;
|
2021-11-05 14:18:23 +00:00
|
|
|
ASTPtr res_base_backup_name;
|
2022-05-01 13:36:32 +00:00
|
|
|
ASTPtr res_cluster_host_ids;
|
2021-11-05 14:18:23 +00:00
|
|
|
|
|
|
|
auto parse_setting = [&]
|
|
|
|
{
|
2022-05-01 13:36:32 +00:00
|
|
|
if (!res_base_backup_name && parseBaseBackupSetting(pos, expected, res_base_backup_name))
|
2021-11-05 14:18:23 +00:00
|
|
|
return true;
|
|
|
|
|
2022-05-01 13:36:32 +00:00
|
|
|
if (!res_cluster_host_ids && parseClusterHostIDsSetting(pos, expected, res_cluster_host_ids))
|
2021-11-05 14:18:23 +00:00
|
|
|
return true;
|
|
|
|
|
2022-05-01 13:36:32 +00:00
|
|
|
SettingChange setting;
|
|
|
|
if (ParserSetQuery::parseNameValuePair(setting, pos, expected))
|
|
|
|
{
|
|
|
|
settings_changes.push_back(std::move(setting));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-05 14:18:23 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!ParserList::parseUtil(pos, expected, parse_setting, false))
|
2021-08-18 22:19:14 +00:00
|
|
|
return false;
|
|
|
|
|
2022-05-01 13:36:32 +00:00
|
|
|
ASTPtr res_settings;
|
|
|
|
if (!settings_changes.empty())
|
|
|
|
{
|
|
|
|
auto settings_changes_ast = std::make_shared<ASTSetQuery>();
|
|
|
|
settings_changes_ast->changes = std::move(settings_changes);
|
|
|
|
settings_changes_ast->is_standalone = false;
|
|
|
|
res_settings = settings_changes_ast;
|
|
|
|
}
|
|
|
|
|
2021-11-05 14:18:23 +00:00
|
|
|
settings = std::move(res_settings);
|
|
|
|
base_backup_name = std::move(res_base_backup_name);
|
2022-05-01 13:36:32 +00:00
|
|
|
cluster_host_ids = std::move(res_cluster_host_ids);
|
2021-08-18 22:19:14 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2022-04-19 18:15:27 +00:00
|
|
|
|
2022-04-26 07:48:40 +00:00
|
|
|
bool parseSyncOrAsync(IParser::Pos & pos, Expected & expected, ASTPtr & settings)
|
|
|
|
{
|
2022-04-26 16:33:19 +00:00
|
|
|
bool async;
|
|
|
|
if (ParserKeyword{"ASYNC"}.ignore(pos, expected))
|
|
|
|
async = true;
|
|
|
|
else if (ParserKeyword{"SYNC"}.ignore(pos, expected))
|
|
|
|
async = false;
|
2022-04-26 07:48:40 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SettingsChanges changes;
|
|
|
|
if (settings)
|
|
|
|
{
|
|
|
|
changes = assert_cast<ASTSetQuery *>(settings.get())->changes;
|
|
|
|
}
|
|
|
|
|
2022-04-26 16:33:19 +00:00
|
|
|
boost::remove_erase_if(changes, [](const SettingChange & change) { return change.name == "async"; });
|
|
|
|
changes.emplace_back("async", async);
|
2022-04-26 07:48:40 +00:00
|
|
|
|
|
|
|
auto new_settings = std::make_shared<ASTSetQuery>();
|
|
|
|
new_settings->changes = std::move(changes);
|
|
|
|
new_settings->is_standalone = false;
|
|
|
|
settings = new_settings;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-19 18:15:27 +00:00
|
|
|
bool parseOnCluster(IParserBase::Pos & pos, Expected & expected, String & cluster)
|
|
|
|
{
|
|
|
|
return IParserBase::wrapParseImpl(pos, [&]
|
|
|
|
{
|
|
|
|
return ParserKeyword{"ON"}.ignore(pos, expected) && ASTQueryWithOnCluster::parse(pos, cluster, expected);
|
|
|
|
});
|
|
|
|
}
|
2021-08-18 22:19:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ParserBackupQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
Kind kind;
|
|
|
|
if (ParserKeyword{"BACKUP"}.ignore(pos, expected))
|
|
|
|
kind = Kind::BACKUP;
|
|
|
|
else if (ParserKeyword{"RESTORE"}.ignore(pos, expected))
|
|
|
|
kind = Kind::RESTORE;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::vector<Element> elements;
|
2023-03-29 22:11:13 +00:00
|
|
|
if (!parseElements(pos, expected, elements))
|
2021-08-18 22:19:14 +00:00
|
|
|
return false;
|
|
|
|
|
2022-04-19 18:15:27 +00:00
|
|
|
String cluster;
|
|
|
|
parseOnCluster(pos, expected, cluster);
|
|
|
|
|
2021-08-18 22:19:14 +00:00
|
|
|
if (!ParserKeyword{(kind == Kind::BACKUP) ? "TO" : "FROM"}.ignore(pos, expected))
|
|
|
|
return false;
|
2021-11-05 14:18:23 +00:00
|
|
|
|
|
|
|
ASTPtr backup_name;
|
|
|
|
if (!parseBackupName(pos, expected, backup_name))
|
2021-08-18 22:19:14 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ASTPtr settings;
|
2021-11-05 14:18:23 +00:00
|
|
|
ASTPtr base_backup_name;
|
2022-05-01 13:36:32 +00:00
|
|
|
ASTPtr cluster_host_ids;
|
|
|
|
parseSettings(pos, expected, settings, base_backup_name, cluster_host_ids);
|
2022-04-26 07:48:40 +00:00
|
|
|
parseSyncOrAsync(pos, expected, settings);
|
2021-08-18 22:19:14 +00:00
|
|
|
|
|
|
|
auto query = std::make_shared<ASTBackupQuery>();
|
|
|
|
node = query;
|
|
|
|
|
|
|
|
query->kind = kind;
|
|
|
|
query->elements = std::move(elements);
|
2022-05-01 13:36:32 +00:00
|
|
|
query->cluster = std::move(cluster);
|
2022-11-11 15:26:04 +00:00
|
|
|
|
|
|
|
if (backup_name)
|
2022-11-16 14:57:50 +00:00
|
|
|
query->set(query->backup_name, backup_name);
|
2022-11-11 15:26:04 +00:00
|
|
|
|
2021-08-18 22:19:14 +00:00
|
|
|
query->settings = std::move(settings);
|
2022-05-01 13:36:32 +00:00
|
|
|
query->cluster_host_ids = std::move(cluster_host_ids);
|
2021-08-18 22:19:14 +00:00
|
|
|
|
2022-11-11 15:26:04 +00:00
|
|
|
if (base_backup_name)
|
2022-11-16 14:57:50 +00:00
|
|
|
query->set(query->base_backup_name, base_backup_name);
|
2022-11-11 15:26:04 +00:00
|
|
|
|
2021-08-18 22:19:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|