add TRUNCATE ALL TABLES

This commit is contained in:
HowePa 2024-03-25 15:59:15 +08:00
parent 0d1751fd55
commit d38534e047
7 changed files with 95 additions and 3 deletions

View File

@ -23,9 +23,16 @@ You can specify how long (in seconds) to wait for inactive replicas to execute `
If the `alter_sync` is set to `2` and some replicas are not active for more than the time, specified by the `replication_wait_for_inactive_replica_timeout` setting, then an exception `UNFINISHED` is thrown.
:::
## TRUNCATE ALL TABLES
``` sql
TRUNCATE ALL TABLES [IF EXISTS] db [ON CLUSTER cluster]
```
Removes all data from all tables in a database.
## TRUNCATE DATABASE
``` sql
TRUNCATE DATABASE [IF EXISTS] [db.]name [ON CLUSTER cluster]
TRUNCATE DATABASE [IF EXISTS] db [ON CLUSTER cluster]
```
Removes all tables from a database but keeps the database itself. When the clause `IF EXISTS` is omitted, the query returns an error if the database does not exist.

View File

@ -388,7 +388,7 @@ BlockIO InterpreterDropQuery::executeToDatabaseImpl(const ASTDropQuery & query,
query_for_table.kind = query.kind;
// For truncate operation on database, drop the tables
if (truncate)
query_for_table.kind = ASTDropQuery::Kind::Drop;
query_for_table.kind = query.has_all ? ASTDropQuery::Kind::Truncate : ASTDropQuery::Kind::Drop;
query_for_table.if_exists = true;
query_for_table.if_empty = false;
query_for_table.setDatabase(database_name);

View File

@ -47,7 +47,9 @@ void ASTDropQuery::formatQueryImpl(const FormatSettings & settings, FormatState
if (temporary)
settings.ostr << "TEMPORARY ";
if (!table && database)
if (has_all)
settings.ostr << "ALL TABLES ";
else if (!table && database)
settings.ostr << "DATABASE ";
else if (is_dictionary)
settings.ostr << "DICTIONARY ";

View File

@ -26,6 +26,9 @@ public:
/// Useful if we already have a DDL lock
bool no_ddl_lock{false};
/// For `TRUNCATE ALL TABLES` query
bool has_all{false};
/// We dropping dictionary, so print correct word
bool is_dictionary{false};

View File

@ -17,6 +17,8 @@ bool parseDropQuery(IParser::Pos & pos, ASTPtr & node, Expected & expected, cons
ParserKeyword s_dictionary(Keyword::DICTIONARY);
ParserKeyword s_view(Keyword::VIEW);
ParserKeyword s_database(Keyword::DATABASE);
ParserKeyword s_all(Keyword::ALL);
ParserKeyword s_tables(Keyword::TABLES);
ParserToken s_dot(TokenType::Dot);
ParserKeyword s_if_exists(Keyword::IF_EXISTS);
ParserKeyword s_if_empty(Keyword::IF_EMPTY);
@ -30,6 +32,7 @@ bool parseDropQuery(IParser::Pos & pos, ASTPtr & node, Expected & expected, cons
String cluster_str;
bool if_exists = false;
bool if_empty = false;
bool has_all = false;
bool temporary = false;
bool is_dictionary = false;
bool is_view = false;
@ -47,6 +50,16 @@ bool parseDropQuery(IParser::Pos & pos, ASTPtr & node, Expected & expected, cons
if (!name_p.parse(pos, database, expected))
return false;
}
else if (s_all.ignore(pos, expected) && s_tables.ignore(pos, expected) && kind == ASTDropQuery::Kind::Truncate)
{
has_all = true;
if (s_if_exists.ignore(pos, expected))
if_exists = true;
if (!name_p.parse(pos, database, expected))
return false;
}
else
{
if (s_view.ignore(pos, expected))
@ -99,6 +112,7 @@ bool parseDropQuery(IParser::Pos & pos, ASTPtr & node, Expected & expected, cons
query->kind = kind;
query->if_exists = if_exists;
query->if_empty = if_empty;
query->has_all = has_all;
query->temporary = temporary;
query->is_dictionary = is_dictionary;
query->is_view = is_view;

View File

@ -0,0 +1,16 @@
======Before Truncate======
1
1
1
1
1
2000-01-01 1
======After Truncate And Empty======
0
======After Truncate And Insert Data======
1
1
1
1
1
2000-01-01 1

View File

@ -0,0 +1,50 @@
SET allow_deprecated_syntax_for_merge_tree = true;
DROP DATABASE IF EXISTS truncate_test;
CREATE DATABASE truncate_test;
CREATE TABLE truncate_test.truncate_test_set(id UInt64) ENGINE = Set;
CREATE TABLE truncate_test.truncate_test_log(id UInt64) ENGINE = Log;
CREATE TABLE truncate_test.truncate_test_memory(id UInt64) ENGINE = Memory;
CREATE TABLE truncate_test.truncate_test_tiny_log(id UInt64) ENGINE = TinyLog;
CREATE TABLE truncate_test.truncate_test_stripe_log(id UInt64) ENGINE = StripeLog;
CREATE TABLE truncate_test.truncate_test_merge_tree(p Date, k UInt64) ENGINE = MergeTree(p, k, 1);
SELECT '======Before Truncate======';
INSERT INTO truncate_test.truncate_test_set VALUES(0);
INSERT INTO truncate_test.truncate_test_log VALUES(1);
INSERT INTO truncate_test.truncate_test_memory VALUES(1);
INSERT INTO truncate_test.truncate_test_tiny_log VALUES(1);
INSERT INTO truncate_test.truncate_test_stripe_log VALUES(1);
INSERT INTO truncate_test.truncate_test_merge_tree VALUES('2000-01-01', 1);
SELECT * FROM system.numbers WHERE number NOT IN truncate_test.truncate_test_set LIMIT 1;
SELECT * FROM truncate_test.truncate_test_log;
SELECT * FROM truncate_test.truncate_test_memory;
SELECT * FROM truncate_test.truncate_test_tiny_log;
SELECT * FROM truncate_test.truncate_test_stripe_log;
SELECT * FROM truncate_test.truncate_test_merge_tree;
SELECT '======After Truncate And Empty======';
TRUNCATE ALL TABLES IF EXISTS truncate_test;
SELECT * FROM system.numbers WHERE number NOT IN truncate_test.truncate_test_set LIMIT 1;
SELECT * FROM truncate_test.truncate_test_log;
SELECT * FROM truncate_test.truncate_test_memory;
SELECT * FROM truncate_test.truncate_test_tiny_log;
SELECT * FROM truncate_test.truncate_test_stripe_log;
SELECT * FROM truncate_test.truncate_test_merge_tree;
SELECT '======After Truncate And Insert Data======';
INSERT INTO truncate_test.truncate_test_set VALUES(0);
INSERT INTO truncate_test.truncate_test_log VALUES(1);
INSERT INTO truncate_test.truncate_test_memory VALUES(1);
INSERT INTO truncate_test.truncate_test_tiny_log VALUES(1);
INSERT INTO truncate_test.truncate_test_stripe_log VALUES(1);
INSERT INTO truncate_test.truncate_test_merge_tree VALUES('2000-01-01', 1);
SELECT * FROM system.numbers WHERE number NOT IN truncate_test.truncate_test_set LIMIT 1;
SELECT * FROM truncate_test.truncate_test_log;
SELECT * FROM truncate_test.truncate_test_memory;
SELECT * FROM truncate_test.truncate_test_tiny_log;
SELECT * FROM truncate_test.truncate_test_stripe_log;
SELECT * FROM truncate_test.truncate_test_merge_tree;
DROP DATABASE IF EXISTS truncate_test;