Fix BACKUP and RESTORE of a materialized view in an Ordinary database.

This commit is contained in:
Vitaly Baranov 2024-04-04 18:11:15 +02:00
parent f8ef9fc5d3
commit ed942eff76
5 changed files with 33 additions and 16 deletions

View File

@ -567,17 +567,16 @@ std::vector<std::pair<ASTPtr, StoragePtr>> BackupEntriesCollector::findTablesInD
checkIsQueryCancelled(); checkIsQueryCancelled();
auto filter_by_table_name = [my_database_info = &database_info](const String & table_name) auto filter_by_table_name = [&](const String & table_name)
{ {
/// We skip inner tables of materialized views. if (isInnerTableShouldBeSkippedForBackup(database_name, table_name))
if (table_name.starts_with(".inner_id."))
return false; return false;
if (my_database_info->tables.contains(table_name)) if (database_info.tables.contains(table_name))
return true; return true;
if (my_database_info->all_tables) if (database_info.all_tables)
return !my_database_info->except_table_names.contains(table_name); return !database_info.except_table_names.contains(table_name);
return false; return false;
}; };

View File

@ -120,4 +120,15 @@ bool compareRestoredDatabaseDef(const IAST & restored_database_create_query, con
return compareRestoredTableDef(restored_database_create_query, create_query_from_backup, global_context); return compareRestoredTableDef(restored_database_create_query, create_query_from_backup, global_context);
} }
bool isInnerTableShouldBeSkippedForBackup(const QualifiedTableName & table_name)
{
return isInnerTableShouldBeSkippedForBackup(table_name.database, table_name.table);
}
bool isInnerTableShouldBeSkippedForBackup(const String & /* database_name */, const String & table_name)
{
/// We skip inner tables of materialized views.
return table_name.starts_with(".inner.") || table_name.starts_with(".inner_id.");
}
} }

View File

@ -9,6 +9,7 @@ namespace DB
class IBackup; class IBackup;
class AccessRightsElements; class AccessRightsElements;
class DDLRenamingMap; class DDLRenamingMap;
struct QualifiedTableName;
/// Initializes a DDLRenamingMap from a BACKUP or RESTORE query. /// Initializes a DDLRenamingMap from a BACKUP or RESTORE query.
DDLRenamingMap makeRenamingMapFromBackupQuery(const ASTBackupQuery::Elements & elements); DDLRenamingMap makeRenamingMapFromBackupQuery(const ASTBackupQuery::Elements & elements);
@ -20,4 +21,8 @@ AccessRightsElements getRequiredAccessToBackup(const ASTBackupQuery::Elements &
bool compareRestoredTableDef(const IAST & restored_table_create_query, const IAST & create_query_from_backup, const ContextPtr & global_context); bool compareRestoredTableDef(const IAST & restored_table_create_query, const IAST & create_query_from_backup, const ContextPtr & global_context);
bool compareRestoredDatabaseDef(const IAST & restored_database_create_query, const IAST & create_query_from_backup, const ContextPtr & global_context); bool compareRestoredDatabaseDef(const IAST & restored_database_create_query, const IAST & create_query_from_backup, const ContextPtr & global_context);
/// Returns true if this table should be skipped while making a backup because it's an inner table.
bool isInnerTableShouldBeSkippedForBackup(const QualifiedTableName & table_name);
bool isInnerTableShouldBeSkippedForBackup(const String & database_name, const String & table_name);
} }

View File

@ -343,12 +343,12 @@ void RestorerFromBackup::findDatabasesAndTablesInBackup()
{ {
case ASTBackupQuery::ElementType::TABLE: case ASTBackupQuery::ElementType::TABLE:
{ {
findTableInBackup({element.database_name, element.table_name}, element.partitions); findTableInBackup({element.database_name, element.table_name}, /* skip_if_inner_table= */ false, element.partitions);
break; break;
} }
case ASTBackupQuery::ElementType::TEMPORARY_TABLE: case ASTBackupQuery::ElementType::TEMPORARY_TABLE:
{ {
findTableInBackup({DatabaseCatalog::TEMPORARY_DATABASE, element.table_name}, element.partitions); findTableInBackup({DatabaseCatalog::TEMPORARY_DATABASE, element.table_name}, /* skip_if_inner_table= */ false, element.partitions);
break; break;
} }
case ASTBackupQuery::ElementType::DATABASE: case ASTBackupQuery::ElementType::DATABASE:
@ -367,14 +367,14 @@ void RestorerFromBackup::findDatabasesAndTablesInBackup()
LOG_INFO(log, "Will restore {} databases and {} tables", getNumDatabases(), getNumTables()); LOG_INFO(log, "Will restore {} databases and {} tables", getNumDatabases(), getNumTables());
} }
void RestorerFromBackup::findTableInBackup(const QualifiedTableName & table_name_in_backup, const std::optional<ASTs> & partitions) void RestorerFromBackup::findTableInBackup(const QualifiedTableName & table_name_in_backup, bool skip_if_inner_table, const std::optional<ASTs> & partitions)
{ {
schedule( schedule(
[this, table_name_in_backup, partitions]() { findTableInBackupImpl(table_name_in_backup, partitions); }, [this, table_name_in_backup, skip_if_inner_table, partitions]() { findTableInBackupImpl(table_name_in_backup, skip_if_inner_table, partitions); },
"Restore_FindTbl"); "Restore_FindTbl");
} }
void RestorerFromBackup::findTableInBackupImpl(const QualifiedTableName & table_name_in_backup, const std::optional<ASTs> & partitions) void RestorerFromBackup::findTableInBackupImpl(const QualifiedTableName & table_name_in_backup, bool skip_if_inner_table, const std::optional<ASTs> & partitions)
{ {
bool is_temporary_table = (table_name_in_backup.database == DatabaseCatalog::TEMPORARY_DATABASE); bool is_temporary_table = (table_name_in_backup.database == DatabaseCatalog::TEMPORARY_DATABASE);
@ -419,6 +419,10 @@ void RestorerFromBackup::findTableInBackupImpl(const QualifiedTableName & table_
= *root_path_in_use / "data" / escapeForFileName(table_name_in_backup.database) / escapeForFileName(table_name_in_backup.table); = *root_path_in_use / "data" / escapeForFileName(table_name_in_backup.database) / escapeForFileName(table_name_in_backup.table);
} }
QualifiedTableName table_name = renaming_map.getNewTableName(table_name_in_backup);
if (skip_if_inner_table && isInnerTableShouldBeSkippedForBackup(table_name))
return;
auto read_buffer = backup->readFile(*metadata_path); auto read_buffer = backup->readFile(*metadata_path);
String create_query_str; String create_query_str;
readStringUntilEOF(create_query_str, *read_buffer); readStringUntilEOF(create_query_str, *read_buffer);
@ -429,8 +433,6 @@ void RestorerFromBackup::findTableInBackupImpl(const QualifiedTableName & table_
renameDatabaseAndTableNameInCreateQuery(create_table_query, renaming_map, context->getGlobalContext()); renameDatabaseAndTableNameInCreateQuery(create_table_query, renaming_map, context->getGlobalContext());
String create_table_query_str = serializeAST(*create_table_query); String create_table_query_str = serializeAST(*create_table_query);
QualifiedTableName table_name = renaming_map.getNewTableName(table_name_in_backup);
bool is_predefined_table = DatabaseCatalog::instance().isPredefinedTable(StorageID{table_name.database, table_name.table}); bool is_predefined_table = DatabaseCatalog::instance().isPredefinedTable(StorageID{table_name.database, table_name.table});
auto table_dependencies = getDependenciesFromCreateQuery(context, table_name, create_table_query); auto table_dependencies = getDependenciesFromCreateQuery(context, table_name, create_table_query);
bool table_has_data = backup->hasFiles(data_path_in_backup); bool table_has_data = backup->hasFiles(data_path_in_backup);
@ -565,7 +567,7 @@ void RestorerFromBackup::findDatabaseInBackupImpl(const String & database_name_i
if (except_table_names.contains({database_name_in_backup, table_name_in_backup})) if (except_table_names.contains({database_name_in_backup, table_name_in_backup}))
continue; continue;
findTableInBackup({database_name_in_backup, table_name_in_backup}, /* partitions= */ {}); findTableInBackup({database_name_in_backup, table_name_in_backup}, /* skip_if_inner_table= */ true, /* partitions= */ {});
} }
} }

View File

@ -92,8 +92,8 @@ private:
void findRootPathsInBackup(); void findRootPathsInBackup();
void findDatabasesAndTablesInBackup(); void findDatabasesAndTablesInBackup();
void findTableInBackup(const QualifiedTableName & table_name_in_backup, const std::optional<ASTs> & partitions); void findTableInBackup(const QualifiedTableName & table_name_in_backup, bool skip_if_inner_table, const std::optional<ASTs> & partitions);
void findTableInBackupImpl(const QualifiedTableName & table_name_in_backup, const std::optional<ASTs> & partitions); void findTableInBackupImpl(const QualifiedTableName & table_name_in_backup, bool skip_if_inner_table, const std::optional<ASTs> & partitions);
void findDatabaseInBackup(const String & database_name_in_backup, const std::set<DatabaseAndTableName> & except_table_names); void findDatabaseInBackup(const String & database_name_in_backup, const std::set<DatabaseAndTableName> & except_table_names);
void findDatabaseInBackupImpl(const String & database_name_in_backup, const std::set<DatabaseAndTableName> & except_table_names); void findDatabaseInBackupImpl(const String & database_name_in_backup, const std::set<DatabaseAndTableName> & except_table_names);
void findEverythingInBackup(const std::set<String> & except_database_names, const std::set<DatabaseAndTableName> & except_table_names); void findEverythingInBackup(const std::set<String> & except_database_names, const std::set<DatabaseAndTableName> & except_table_names);