diff --git a/dbms/src/Interpreters/InterpreterDropQuery.cpp b/dbms/src/Interpreters/InterpreterDropQuery.cpp index cdb107a13adca09288942c6ddcd43b1c612e1aa9..efe6aae87a89606cf48529c3806e6429f4b0cf9d 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/src/Parsers/ASTDropQuery.cpp b/dbms/src/Parsers/ASTDropQuery.cpp index dff25e7023ec1d8af3c43551d5bbcccb0c40669e..aa47829519a5861f88c3b3749f799061567f9958 100644 --- a/dbms/src/Parsers/ASTDropQuery.cpp +++ b/dbms/src/Parsers/ASTDropQuery.cpp @@ -53,6 +53,9 @@ void ASTDropQuery::formatQueryImpl(const FormatSettings & settings, FormatState else throw Exception("Not supported kind of drop query.", ErrorCodes::SYNTAX_ERROR); + if (temporary) + settings.ostr << "TEMPORARY "; + settings.ostr << ((table.empty() && !database.empty()) ? "DATABASE " : "TABLE "); if (if_exists) 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 0000000000000000000000000000000000000000..ddfc922fc2914818ac31b391ed77deae295e576a --- /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 0000000000000000000000000000000000000000..e0a411a710e474f1858284e318075c73e3fb43a2 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00670_truncate_temporary_table.sql @@ -0,0 +1,16 @@ +drop temporary 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 temporary table test; +select * from test; + +select '======After Truncate And Insert Data======'; +insert into test values(0); +select * from test; + +drop temporary table test;