Skip to content

Commit

Permalink
Merge pull request ClickHouse#55373 from lingtaolf/enhancement/rename…
Browse files Browse the repository at this point in the history
…_without_keyword

Support rename table without keyword TABLE
  • Loading branch information
antaljanosbenjamin authored Oct 16, 2023
2 parents 478f635 + e646fb7 commit eeff98f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 13 deletions.
7 changes: 6 additions & 1 deletion docs/en/sql-reference/statements/rename.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
r1
r1_bak
r1
r1_bak
r2_bak
test_dictionary
test_dictionary_2
42 changes: 42 additions & 0 deletions tests/queries/0_stateless/02891_rename_table_without_keyword.sql
Original file line number Diff line number Diff line change
@@ -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};

0 comments on commit eeff98f

Please sign in to comment.