From f5764910c48ea51d160c6a1eca3f6f91bd323de0 Mon Sep 17 00:00:00 2001 From: Amos Bird Date: Tue, 10 Jul 2018 22:24:19 +0800 Subject: [PATCH] Fix TRUNCATE command for temporary tables. --- .../src/Interpreters/InterpreterDropQuery.cpp | 29 +++++++++++++------ .../00670_truncate_temporary_table.reference | 5 ++++ .../00670_truncate_temporary_table.sql | 16 ++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 dbms/tests/queries/0_stateless/00670_truncate_temporary_table.reference create mode 100644 dbms/tests/queries/0_stateless/00670_truncate_temporary_table.sql diff --git a/dbms/src/Interpreters/InterpreterDropQuery.cpp b/dbms/src/Interpreters/InterpreterDropQuery.cpp index cdb107a13ad..efe6aae87a8 100644 --- a/dbms/src/Interpreters/InterpreterDropQuery.cpp +++ b/dbms/src/Interpreters/InterpreterDropQuery.cpp @@ -121,18 +121,29 @@ BlockIO InterpreterDropQuery::executeToTemporaryTable(String & table_name, ASTDr { if (kind == ASTDropQuery::Kind::Detach) throw Exception("Unable to detach temporary table.", ErrorCodes::SYNTAX_ERROR); - else if (kind == ASTDropQuery::Kind::Drop) + else { - StoragePtr table = (context.hasSessionContext() ? context.getSessionContext() : context).tryRemoveExternalTable(table_name); + auto & context_handle = context.hasSessionContext() ? context.getSessionContext() : context; + StoragePtr table = context_handle.tryGetExternalTable(table_name); if (table) { - table->shutdown(); - /// If table was already dropped by anyone, an exception will be thrown - auto table_lock = table->lockForAlter(__PRETTY_FUNCTION__); - /// Delete table data - table->drop(); - table->is_dropped = true; - return {}; + if (kind == ASTDropQuery::Kind::Truncate) + { + /// If table was already dropped by anyone, an exception will be thrown + auto table_lock = table->lockDataForAlter(__PRETTY_FUNCTION__); + /// Drop table data, don't touch metadata + table->truncate(query_ptr); + } + else if (kind == ASTDropQuery::Kind::Drop) + { + context_handle.tryRemoveExternalTable(table_name); + table->shutdown(); + /// If table was already dropped by anyone, an exception will be thrown + auto table_lock = table->lockForAlter(__PRETTY_FUNCTION__); + /// Delete table data + table->drop(); + table->is_dropped = true; + } } } diff --git a/dbms/tests/queries/0_stateless/00670_truncate_temporary_table.reference b/dbms/tests/queries/0_stateless/00670_truncate_temporary_table.reference new file mode 100644 index 00000000000..ddfc922fc29 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00670_truncate_temporary_table.reference @@ -0,0 +1,5 @@ +======Before Truncate====== +0 +======After Truncate And Empty====== +======After Truncate And Insert Data====== +0 diff --git a/dbms/tests/queries/0_stateless/00670_truncate_temporary_table.sql b/dbms/tests/queries/0_stateless/00670_truncate_temporary_table.sql new file mode 100644 index 00000000000..60e7f216f99 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00670_truncate_temporary_table.sql @@ -0,0 +1,16 @@ +drop table if exists test; +create temporary table test(id int); + +select '======Before Truncate======'; +insert into test values(0); +select * from test; + +select '======After Truncate And Empty======'; +truncate table test; +select * from test; + +select '======After Truncate And Insert Data======'; +insert into test values(0); +select * from test; + +drop table if exists test;