Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rename table without keyword TABLE #55373

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/en/sql-reference/statements/rename.md
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 15 additions & 12 deletions src/Parsers/ParserRenameQuery.cpp
Expand Up @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
@@ -0,0 +1,7 @@
r1
r1_bak
r1
r1_bak
r2_bak
test_dictionary
lingtaolf marked this conversation as resolved.
Show resolved Hide resolved
test_dictionary_2
42 changes: 42 additions & 0 deletions 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};