mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-24 08:32:02 +00:00
Fix TRUNCATE command for temporary tables.
This commit is contained in:
parent
a26a0f5331
commit
f5764910c4
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,5 @@
|
||||
======Before Truncate======
|
||||
0
|
||||
======After Truncate And Empty======
|
||||
======After Truncate And Insert Data======
|
||||
0
|
@ -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;
|
Loading…
Reference in New Issue
Block a user