From 8f82d64129b3aaa52a0ff17287b5714c7ca84c7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=8C=E6=B6=9B?= Date: Mon, 9 Oct 2023 17:06:31 +0800 Subject: [PATCH 1/6] support rename table without keyword TABLE --- src/Parsers/ParserRenameQuery.cpp | 27 ++++++++++--------- ...891_rename_table_without_keyword.reference | 3 +++ .../02891_rename_table_without_keyword.sql | 14 ++++++++++ 3 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 tests/queries/0_stateless/02891_rename_table_without_keyword.reference create mode 100644 tests/queries/0_stateless/02891_rename_table_without_keyword.sql diff --git a/src/Parsers/ParserRenameQuery.cpp b/src/Parsers/ParserRenameQuery.cpp index cb595846633..56845d781b3 100644 --- a/src/Parsers/ParserRenameQuery.cpp +++ b/src/Parsers/ParserRenameQuery.cpp @@ -11,6 +11,7 @@ namespace DB bool ParserRenameQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) { + ParserKeyword s_rename("RENAME"); ParserKeyword s_rename_table("RENAME TABLE"); ParserKeyword s_exchange_tables("EXCHANGE TABLES"); ParserKeyword s_rename_dictionary("RENAME DICTIONARY"); @@ -24,18 +25,7 @@ bool ParserRenameQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) bool exchange = false; bool dictionary = false; - if (s_rename_table.ignore(pos, expected)) - ; - else if (s_exchange_tables.ignore(pos, expected)) - exchange = true; - else if (s_rename_dictionary.ignore(pos, expected)) - dictionary = true; - else if (s_exchange_dictionaries.ignore(pos, expected)) - { - exchange = true; - dictionary = true; - } - else if (s_rename_database.ignore(pos, expected)) + if (s_rename_database.ignore(pos, expected)) { ASTPtr from_db; ASTPtr to_db; @@ -67,6 +57,19 @@ bool ParserRenameQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) node = query; return true; } + else if(s_rename_table.ignore(pos, expected)) + ; + else if (s_exchange_tables.ignore(pos, expected)) + exchange = true; + else if (s_rename_dictionary.ignore(pos, expected)) + dictionary = true; + else if (s_exchange_dictionaries.ignore(pos, expected)) + { + exchange = true; + dictionary = true; + } + else if (s_rename.ignore(pos, expected)) + ; else return false; diff --git a/tests/queries/0_stateless/02891_rename_table_without_keyword.reference b/tests/queries/0_stateless/02891_rename_table_without_keyword.reference new file mode 100644 index 00000000000..74c047b102a --- /dev/null +++ b/tests/queries/0_stateless/02891_rename_table_without_keyword.reference @@ -0,0 +1,3 @@ +r1 +r2 +r3 diff --git a/tests/queries/0_stateless/02891_rename_table_without_keyword.sql b/tests/queries/0_stateless/02891_rename_table_without_keyword.sql new file mode 100644 index 00000000000..d92b3f69e5d --- /dev/null +++ b/tests/queries/0_stateless/02891_rename_table_without_keyword.sql @@ -0,0 +1,14 @@ +DROP DATABASE IF EXISTS rename_db; +CREATE DATABASE rename_db; + +CREATE TABLE rename_db.r1 (name String) Engine=Memory(); +SHOW TABLES FROM rename_db; + +RENAME TABLE rename_db.r1 TO rename_db.r2; +SHOW TABLES FROM rename_db; + +RENAME rename_db.r2 TO rename_db.r3; +SHOW TABLES FROM rename_db; + +DROP DATABASE rename_db; + From 4499dd3e2f5f515343c8a5751cf6a72afd178622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=8C=E6=B6=9B?= Date: Mon, 9 Oct 2023 19:03:17 +0800 Subject: [PATCH 2/6] modify style --- docs/en/sql-reference/statements/rename.md | 7 ++- src/Parsers/ParserRenameQuery.cpp | 2 +- ...891_rename_table_without_keyword.reference | 9 ++- .../02891_rename_table_without_keyword.sh | 57 +++++++++++++++++++ .../02891_rename_table_without_keyword.sql | 30 +++++++++- 5 files changed, 98 insertions(+), 7 deletions(-) create mode 100755 tests/queries/0_stateless/02891_rename_table_without_keyword.sh diff --git a/docs/en/sql-reference/statements/rename.md b/docs/en/sql-reference/statements/rename.md index a2d4b15df13..957f2238a84 100644 --- a/docs/en/sql-reference/statements/rename.md +++ b/docs/en/sql-reference/statements/rename.md @@ -16,7 +16,7 @@ The `RENAME` query is supported by the [Atomic](../../engines/database-engines/a **Syntax** ```sql -RENAME DATABASE|TABLE|DICTIONARY name TO new_name [,...] [ON CLUSTER cluster] +RENAME DATABASE|[TABLE|DICTIONARY] name TO new_name [,...] [ON CLUSTER cluster] ``` ## RENAME DATABASE @@ -48,6 +48,11 @@ RENAME TABLE [db1.]name1 TO [db2.]name2 [,...] [ON CLUSTER cluster] RENAME TABLE table_A TO table_A_bak, table_B TO table_B_bak; ``` +And you can use a simpler sql: +```sql +RENAME table_A TO table_A_bak, table_B TO table_B_bak; +``` + ## RENAME DICTIONARY Renames one or several dictionaries. This query can be used to move dictionaries between databases. diff --git a/src/Parsers/ParserRenameQuery.cpp b/src/Parsers/ParserRenameQuery.cpp index 56845d781b3..27f2ed1cd22 100644 --- a/src/Parsers/ParserRenameQuery.cpp +++ b/src/Parsers/ParserRenameQuery.cpp @@ -57,7 +57,7 @@ bool ParserRenameQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) node = query; return true; } - else if(s_rename_table.ignore(pos, expected)) + else if (s_rename_table.ignore(pos, expected)) ; else if (s_exchange_tables.ignore(pos, expected)) exchange = true; diff --git a/tests/queries/0_stateless/02891_rename_table_without_keyword.reference b/tests/queries/0_stateless/02891_rename_table_without_keyword.reference index 74c047b102a..3116f82ea08 100644 --- a/tests/queries/0_stateless/02891_rename_table_without_keyword.reference +++ b/tests/queries/0_stateless/02891_rename_table_without_keyword.reference @@ -1,3 +1,8 @@ r1 -r2 -r3 +r1_bak +r1 +r1_bak +r2_bak +test_dictionary +test_dictionary_2 +UNKNOWN_TABLE diff --git a/tests/queries/0_stateless/02891_rename_table_without_keyword.sh b/tests/queries/0_stateless/02891_rename_table_without_keyword.sh new file mode 100755 index 00000000000..8d9d73f6ab4 --- /dev/null +++ b/tests/queries/0_stateless/02891_rename_table_without_keyword.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CUR_DIR"/../shell_config.sh + +db_name=db_$(echo $RANDOM |md5sum |cut -c 1-4) + +$CLICKHOUSE_CLIENT --param_db="${db_name}" --multiquery \ + --query="DROP DATABASE IF EXISTS {db:Identifier}; + CREATE DATABASE {db:Identifier}; + CREATE TABLE IF NOT EXISTS {db:Identifier}.r1 (name String) Engine=Memory(); + SHOW TABLES FROM {db:Identifier}" + +$CLICKHOUSE_CLIENT --param_db="${db_name}" --multiquery \ + --query="RENAME TABLE {db:Identifier}.r1 TO {db:Identifier}.r1_bak; + SHOW TABLES FROM {db:Identifier};" + +$CLICKHOUSE_CLIENT --param_db="${db_name}" --multiquery \ + --query="RENAME {db:Identifier}.r1_bak TO {db:Identifier}.r1; + SHOW TABLES FROM {db:Identifier};" + +$CLICKHOUSE_CLIENT --param_db="${db_name}" --multiquery \ + --query="CREATE TABLE IF NOT EXISTS {db:Identifier}.r2 (name String) Engine=Memory(); + RENAME {db:Identifier}.r1 TO {db:Identifier}.r1_bak, {db:Identifier}.r2 TO {db:Identifier}.r2_bak; + SHOW TABLES FROM {db:Identifier};" + + +$CLICKHOUSE_CLIENT --param_db="${db_name}" --multiquery \ + --query="CREATE TABLE IF NOT EXISTS {db:Identifier}.source_table ( + id UInt64, + value String + ) ENGINE = Memory; + + CREATE DICTIONARY IF NOT EXISTS {db:Identifier}.test_dictionary + ( + id UInt64, + value String + ) + PRIMARY KEY id + SOURCE(CLICKHOUSE(TABLE '{db:Identifier}.source_table')) + LAYOUT(FLAT()) + LIFETIME(MIN 0 MAX 1000); + + SHOW DICTIONARIES FROM {db:Identifier};" + + +$CLICKHOUSE_CLIENT --param_db="${db_name}" --multiquery \ + --query="RENAME {db:Identifier}.test_dictionary TO {db:Identifier}.test_dictionary_2; + SHOW DICTIONARIES FROM {db:Identifier};" + +todb_name=db_$(echo $RANDOM |md5sum |cut -c 1-4) + +$CLICKHOUSE_CLIENT --param_todb="${todb_name}" --param_db="${db_name}" --query="RENAME {db:Identifier} TO {todb:Identifier}; -- { serverError 60 }" 2>&1 | grep -o "UNKNOWN_TABLE" | uniq + +$CLICKHOUSE_CLIENT --param_db="${db_name}" --query="DROP DATABASE IF EXISTS {db:Identifier}" + diff --git a/tests/queries/0_stateless/02891_rename_table_without_keyword.sql b/tests/queries/0_stateless/02891_rename_table_without_keyword.sql index d92b3f69e5d..291b5db5c56 100644 --- a/tests/queries/0_stateless/02891_rename_table_without_keyword.sql +++ b/tests/queries/0_stateless/02891_rename_table_without_keyword.sql @@ -1,7 +1,7 @@ DROP DATABASE IF EXISTS rename_db; -CREATE DATABASE rename_db; +CREATE DATABASE IF NOT EXISTS rename_db; -CREATE TABLE rename_db.r1 (name String) Engine=Memory(); +CREATE TABLE IF NOT EXISTS rename_db.r1 (name String) Engine=Memory(); SHOW TABLES FROM rename_db; RENAME TABLE rename_db.r1 TO rename_db.r2; @@ -10,5 +10,29 @@ SHOW TABLES FROM rename_db; RENAME rename_db.r2 TO rename_db.r3; SHOW TABLES FROM rename_db; -DROP DATABASE rename_db; +CREATE TABLE IF NOT EXISTS rename_db.source_table ( + id UInt64, + value String +) ENGINE = Memory; + +CREATE DICTIONARY IF NOT EXISTS rename_db.test_dictionary +( + id UInt64, + value String +) +PRIMARY KEY id +SOURCE(CLICKHOUSE(TABLE 'rename_db.dictionary_table')) +LAYOUT(FLAT()) +LIFETIME(MIN 0 MAX 1000); + +SHOW DICTIONARIES FROM rename_db; + +RENAME rename_db.test_dictionary TO rename_db.test_dictionary_2; -- { serverError UNKNOWN_TABLE } +SHOW DICTIONARIES FROM rename_db; + +SHOW DATABASES LIKE 'rename_db'; +RENAME rename_db TO rename_db_2; -- { serverError UNKNOWN_TABLE } +SHOW DATABASES LIKE 'rename_db'; + +DROP DATABASE IF EXISTS rename_db; From 89d123d0fdae5fc5fa582a0325b9c7bb08f24141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=8C=E6=B6=9B?= Date: Thu, 12 Oct 2023 14:24:59 +0800 Subject: [PATCH 3/6] modify doc --- docs/en/sql-reference/statements/rename.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/sql-reference/statements/rename.md b/docs/en/sql-reference/statements/rename.md index 957f2238a84..bb62cc3af1c 100644 --- a/docs/en/sql-reference/statements/rename.md +++ b/docs/en/sql-reference/statements/rename.md @@ -16,7 +16,7 @@ The `RENAME` query is supported by the [Atomic](../../engines/database-engines/a **Syntax** ```sql -RENAME DATABASE|[TABLE|DICTIONARY] name TO new_name [,...] [ON CLUSTER cluster] +RENAME [DATABASE|TABLE|DICTIONARY] name TO new_name [,...] [ON CLUSTER cluster] ``` ## RENAME DATABASE From ca6975a2653273ce269dbc96bf50c543bf461036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=8C=E6=B6=9B?= Date: Thu, 12 Oct 2023 14:25:45 +0800 Subject: [PATCH 4/6] Delete tests/queries/0_stateless/02891_rename_table_without_keyword.sql --- .../02891_rename_table_without_keyword.sql | 38 ------------------- 1 file changed, 38 deletions(-) delete mode 100644 tests/queries/0_stateless/02891_rename_table_without_keyword.sql diff --git a/tests/queries/0_stateless/02891_rename_table_without_keyword.sql b/tests/queries/0_stateless/02891_rename_table_without_keyword.sql deleted file mode 100644 index 291b5db5c56..00000000000 --- a/tests/queries/0_stateless/02891_rename_table_without_keyword.sql +++ /dev/null @@ -1,38 +0,0 @@ -DROP DATABASE IF EXISTS rename_db; -CREATE DATABASE IF NOT EXISTS rename_db; - -CREATE TABLE IF NOT EXISTS rename_db.r1 (name String) Engine=Memory(); -SHOW TABLES FROM rename_db; - -RENAME TABLE rename_db.r1 TO rename_db.r2; -SHOW TABLES FROM rename_db; - -RENAME rename_db.r2 TO rename_db.r3; -SHOW TABLES FROM rename_db; - -CREATE TABLE IF NOT EXISTS rename_db.source_table ( - id UInt64, - value String -) ENGINE = Memory; - -CREATE DICTIONARY IF NOT EXISTS rename_db.test_dictionary -( - id UInt64, - value String -) -PRIMARY KEY id -SOURCE(CLICKHOUSE(TABLE 'rename_db.dictionary_table')) -LAYOUT(FLAT()) -LIFETIME(MIN 0 MAX 1000); - -SHOW DICTIONARIES FROM rename_db; - -RENAME rename_db.test_dictionary TO rename_db.test_dictionary_2; -- { serverError UNKNOWN_TABLE } -SHOW DICTIONARIES FROM rename_db; - -SHOW DATABASES LIKE 'rename_db'; -RENAME rename_db TO rename_db_2; -- { serverError UNKNOWN_TABLE } -SHOW DATABASES LIKE 'rename_db'; - -DROP DATABASE IF EXISTS rename_db; - From 2fd2352addb24a18828c41c75cf4b597499660c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Benjamin=20Antal?= Date: Thu, 12 Oct 2023 09:52:06 +0000 Subject: [PATCH 5/6] Use SQL test --- ...891_rename_table_without_keyword.reference | 1 - .../02891_rename_table_without_keyword.sh | 57 ------------------- .../02891_rename_table_without_keyword.sql | 42 ++++++++++++++ 3 files changed, 42 insertions(+), 58 deletions(-) delete mode 100755 tests/queries/0_stateless/02891_rename_table_without_keyword.sh create mode 100755 tests/queries/0_stateless/02891_rename_table_without_keyword.sql diff --git a/tests/queries/0_stateless/02891_rename_table_without_keyword.reference b/tests/queries/0_stateless/02891_rename_table_without_keyword.reference index 3116f82ea08..958c53935c1 100644 --- a/tests/queries/0_stateless/02891_rename_table_without_keyword.reference +++ b/tests/queries/0_stateless/02891_rename_table_without_keyword.reference @@ -5,4 +5,3 @@ r1_bak r2_bak test_dictionary test_dictionary_2 -UNKNOWN_TABLE diff --git a/tests/queries/0_stateless/02891_rename_table_without_keyword.sh b/tests/queries/0_stateless/02891_rename_table_without_keyword.sh deleted file mode 100755 index 8d9d73f6ab4..00000000000 --- a/tests/queries/0_stateless/02891_rename_table_without_keyword.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/env bash - -CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) -# shellcheck source=../shell_config.sh -. "$CUR_DIR"/../shell_config.sh - -db_name=db_$(echo $RANDOM |md5sum |cut -c 1-4) - -$CLICKHOUSE_CLIENT --param_db="${db_name}" --multiquery \ - --query="DROP DATABASE IF EXISTS {db:Identifier}; - CREATE DATABASE {db:Identifier}; - CREATE TABLE IF NOT EXISTS {db:Identifier}.r1 (name String) Engine=Memory(); - SHOW TABLES FROM {db:Identifier}" - -$CLICKHOUSE_CLIENT --param_db="${db_name}" --multiquery \ - --query="RENAME TABLE {db:Identifier}.r1 TO {db:Identifier}.r1_bak; - SHOW TABLES FROM {db:Identifier};" - -$CLICKHOUSE_CLIENT --param_db="${db_name}" --multiquery \ - --query="RENAME {db:Identifier}.r1_bak TO {db:Identifier}.r1; - SHOW TABLES FROM {db:Identifier};" - -$CLICKHOUSE_CLIENT --param_db="${db_name}" --multiquery \ - --query="CREATE TABLE IF NOT EXISTS {db:Identifier}.r2 (name String) Engine=Memory(); - RENAME {db:Identifier}.r1 TO {db:Identifier}.r1_bak, {db:Identifier}.r2 TO {db:Identifier}.r2_bak; - SHOW TABLES FROM {db:Identifier};" - - -$CLICKHOUSE_CLIENT --param_db="${db_name}" --multiquery \ - --query="CREATE TABLE IF NOT EXISTS {db:Identifier}.source_table ( - id UInt64, - value String - ) ENGINE = Memory; - - CREATE DICTIONARY IF NOT EXISTS {db:Identifier}.test_dictionary - ( - id UInt64, - value String - ) - PRIMARY KEY id - SOURCE(CLICKHOUSE(TABLE '{db:Identifier}.source_table')) - LAYOUT(FLAT()) - LIFETIME(MIN 0 MAX 1000); - - SHOW DICTIONARIES FROM {db:Identifier};" - - -$CLICKHOUSE_CLIENT --param_db="${db_name}" --multiquery \ - --query="RENAME {db:Identifier}.test_dictionary TO {db:Identifier}.test_dictionary_2; - SHOW DICTIONARIES FROM {db:Identifier};" - -todb_name=db_$(echo $RANDOM |md5sum |cut -c 1-4) - -$CLICKHOUSE_CLIENT --param_todb="${todb_name}" --param_db="${db_name}" --query="RENAME {db:Identifier} TO {todb:Identifier}; -- { serverError 60 }" 2>&1 | grep -o "UNKNOWN_TABLE" | uniq - -$CLICKHOUSE_CLIENT --param_db="${db_name}" --query="DROP DATABASE IF EXISTS {db:Identifier}" - diff --git a/tests/queries/0_stateless/02891_rename_table_without_keyword.sql b/tests/queries/0_stateless/02891_rename_table_without_keyword.sql new file mode 100755 index 00000000000..2f32dc94def --- /dev/null +++ b/tests/queries/0_stateless/02891_rename_table_without_keyword.sql @@ -0,0 +1,42 @@ +DROP DATABASE IF EXISTS {CLICKHOUSE_DATABASE:Identifier}; +CREATE DATABASE IF NOT EXISTS {CLICKHOUSE_DATABASE:Identifier}; + +CREATE TABLE IF NOT EXISTS {CLICKHOUSE_DATABASE:Identifier}.r1 (name String) Engine=Memory(); +SHOW TABLES FROM {CLICKHOUSE_DATABASE:Identifier}; + +RENAME TABLE {CLICKHOUSE_DATABASE:Identifier}.r1 TO {CLICKHOUSE_DATABASE:Identifier}.r1_bak; +SHOW TABLES FROM {CLICKHOUSE_DATABASE:Identifier}; + +RENAME {CLICKHOUSE_DATABASE:Identifier}.r1_bak TO {CLICKHOUSE_DATABASE:Identifier}.r1; +SHOW TABLES FROM {CLICKHOUSE_DATABASE:Identifier}; + +CREATE TABLE IF NOT EXISTS {CLICKHOUSE_DATABASE:Identifier}.r2 (name String) Engine=Memory(); +RENAME {CLICKHOUSE_DATABASE:Identifier}.r1 TO {CLICKHOUSE_DATABASE:Identifier}.r1_bak, + {CLICKHOUSE_DATABASE:Identifier}.r2 TO {CLICKHOUSE_DATABASE:Identifier}.r2_bak; +SHOW TABLES FROM {CLICKHOUSE_DATABASE:Identifier}; + +CREATE TABLE IF NOT EXISTS {CLICKHOUSE_DATABASE:Identifier}.source_table ( + id UInt64, + value String + ) ENGINE = Memory; + +CREATE DICTIONARY IF NOT EXISTS {CLICKHOUSE_DATABASE:Identifier}.test_dictionary +( + id UInt64, + value String +) +PRIMARY KEY id +SOURCE(CLICKHOUSE(TABLE '{CLICKHOUSE_DATABASE:String}.dictionary_table')) +LAYOUT(FLAT()) +LIFETIME(MIN 0 MAX 1000); + +SHOW DICTIONARIES FROM {CLICKHOUSE_DATABASE:Identifier}; + +RENAME {CLICKHOUSE_DATABASE:Identifier}.test_dictionary TO {CLICKHOUSE_DATABASE:Identifier}.test_dictionary_2; +SHOW DICTIONARIES FROM {CLICKHOUSE_DATABASE:Identifier}; + +SHOW DATABASES LIKE '{CLICKHOUSE_DATABASE:String}'; +RENAME {CLICKHOUSE_DATABASE:Identifier} TO {CLICKHOUSE_DATABASE_1:Identifier}; -- { serverError UNKNOWN_TABLE } +SHOW DATABASES LIKE '{CLICKHOUSE_DATABASE:String}'; + +DROP DATABASE IF EXISTS {CLICKHOUSE_DATABASE:Identifier}; From e646fb7613964a45f1c97236b284828ea1679c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Benjamin=20Antal?= Date: Thu, 12 Oct 2023 13:42:02 +0000 Subject: [PATCH 6/6] Remove executable flag --- tests/queries/0_stateless/02891_rename_table_without_keyword.sql | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 tests/queries/0_stateless/02891_rename_table_without_keyword.sql diff --git a/tests/queries/0_stateless/02891_rename_table_without_keyword.sql b/tests/queries/0_stateless/02891_rename_table_without_keyword.sql old mode 100755 new mode 100644