Fix TRUNCATE command for temporary tables.

This commit is contained in:
Amos Bird 2018-07-10 22:24:19 +08:00
parent a26a0f5331
commit f5764910c4
3 changed files with 41 additions and 9 deletions

View File

@ -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;
}
}
}

View File

@ -0,0 +1,5 @@
======Before Truncate======
0
======After Truncate And Empty======
======After Truncate And Insert Data======
0

View File

@ -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;