From 5277c802f7a6ae225e4a014e51f1554cd2dda057 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Wed, 12 Apr 2023 11:07:18 +0200 Subject: [PATCH] Fix table dependencies in case of failed RENAME TABLE CI found this [1] [ 382620 ] {} Application: Caught exception while loading metadata: Code: 60. DB::Exception: Table test_25.join doesn\'t exist: While processing _CAST(joinGet(test_25.join, \'m\', CAST(\'42\', \'int\')) AS m_tmp_alter1351896088559986425, \'Int32\') AS m: default expression and column type are incomp [ 382620 ] {} Application: Code: 60. DB::Exception: Table test_25.join doesn\'t exist: While processing _CAST(joinGet(test_25.join, \'m\', CAST(\'42\', \'int\')) AS m_tmp_alter1351896088559986425, \'Int32\') AS m: default expression and column type are incompatible.: Cannot attach table `test_25`.`t [1]: https://s3.amazonaws.com/clickhouse-test-reports/48670/d7f865037266ed87538cf4df7ec7e8165681871b/stress_test__asan_.html The problem here is that after failed RENAME dependencies got lost: [4e937f39d866] 2023.04.12 00:51:06.833624 [ 13419 ] {61429225-64cb-4fce-b60d-01e0dac6e52c} executeQuery: Code: 241. DB::Exception: Memory limit (total) exceeded: would use 34.65 GiB (attempt to allocate chunk of 2097419 bytes), maximum: 34.29 GiB. OvercommitTracker decision: Memory overcommit has freed not enough memory. (MEMORY_LIMIT_EXCEEDED) (version 23.4.1.1) (from [::1]:45710) (comment: 01160_table_dependencies.sh) (in query: create database test_25_1), Stack trace (when copying this message, always include the lines below): [4e937f39d866] 2023.04.12 00:51:07.351914 [ 5151 ] {66d8bdd4-668e-4239-a8af-6b8f17bb5222} executeQuery: Code: 81. DB::Exception: Database test_25_1 doesn't exist. (UNKNOWN_DATABASE) (version 23.4.1.1) (from [::1]:45762) (comment: 01160_table_dependencies.sh) (in query: rename table t to test_25_1.t), Stack trace (when copying this message, always include the lines below): And from the test output: OK OK OK OK a [] [] [] data_02344 [] [] [] date_table [] [] [] dict1 [] ['dict_src'] ['join'] dict2 [] ['join'] [] dict_src [] [] ['dict1'] dist_02346 [] [] [] join [] ['dict1'] ['dict2','s'] ^^^^^^^^^^^^^ no "t" Signed-off-by: Azat Khuzhin --- src/Interpreters/InterpreterRenameQuery.cpp | 25 +++++++++++++------ .../01160_table_dependencies.reference | 8 ++++++ .../0_stateless/01160_table_dependencies.sh | 4 +++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Interpreters/InterpreterRenameQuery.cpp b/src/Interpreters/InterpreterRenameQuery.cpp index 01c04999287..75d43b541e1 100644 --- a/src/Interpreters/InterpreterRenameQuery.cpp +++ b/src/Interpreters/InterpreterRenameQuery.cpp @@ -136,15 +136,24 @@ BlockIO InterpreterRenameQuery::executeToTables(const ASTRenameQuery & rename, c std::tie(ref_dependencies, loading_dependencies) = database_catalog.removeDependencies(from_table_id, check_ref_deps, check_loading_deps); } - database->renameTable( - getContext(), - elem.from_table_name, - *database_catalog.getDatabase(elem.to_database_name), - elem.to_table_name, - exchange_tables, - rename.dictionary); + try + { + database->renameTable( + getContext(), + elem.from_table_name, + *database_catalog.getDatabase(elem.to_database_name), + elem.to_table_name, + exchange_tables, + rename.dictionary); - DatabaseCatalog::instance().addDependencies(to_table_id, ref_dependencies, loading_dependencies); + DatabaseCatalog::instance().addDependencies(to_table_id, ref_dependencies, loading_dependencies); + } + catch (...) + { + /// Restore dependencies if RENAME fails + DatabaseCatalog::instance().addDependencies(from_table_id, ref_dependencies, loading_dependencies); + throw; + } } } diff --git a/tests/queries/0_stateless/01160_table_dependencies.reference b/tests/queries/0_stateless/01160_table_dependencies.reference index ead8377abc5..9fcd9bc504c 100644 --- a/tests/queries/0_stateless/01160_table_dependencies.reference +++ b/tests/queries/0_stateless/01160_table_dependencies.reference @@ -30,6 +30,14 @@ mv [] [] [] s [] ['join'] ['t'] t ['mv'] ['dict1','join','s'] [] OK +UNKNOWN_DATABASE +dict1 [] ['dict_src'] ['join','t'] +dict2 [] ['join'] [] +dict_src [] [] ['dict1'] +join [] ['dict1'] ['dict2','s','t'] +mv [] [] [] +s [] ['join'] ['t'] +t ['mv'] ['dict1','join','s'] [] dict1 [] ['dict_src'] ['join','t'] dict2 [] ['join'] [] dict_src [] [] ['dict1'] diff --git a/tests/queries/0_stateless/01160_table_dependencies.sh b/tests/queries/0_stateless/01160_table_dependencies.sh index a0a3f05c6a9..acb6522e9e2 100755 --- a/tests/queries/0_stateless/01160_table_dependencies.sh +++ b/tests/queries/0_stateless/01160_table_dependencies.sh @@ -64,6 +64,10 @@ else echo "OK" fi +$CLICKHOUSE_CLIENT -q "rename table t to ${CLICKHOUSE_DATABASE}_2.t" |& grep -m1 -F -o UNKNOWN_DATABASE +$CLICKHOUSE_CLIENT -q "select table, arraySort(dependencies_table), +arraySort(loading_dependencies_table), arraySort(loading_dependent_table) from system.tables where database in (currentDatabase(), '$t_database') order by table" + $CLICKHOUSE_CLIENT -q "drop table mv" $CLICKHOUSE_CLIENT -q "create database ${CLICKHOUSE_DATABASE}_1"