Skip to content

Commit

Permalink
The default_temporary_table_engine provides better support for MergeTree
Browse files Browse the repository at this point in the history
  • Loading branch information
xiedeyantu committed Feb 18, 2024
1 parent 76578eb commit 2c6b1e1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
71 changes: 40 additions & 31 deletions src/Interpreters/InterpreterCreateQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,37 +1005,7 @@ void InterpreterCreateQuery::setEngine(ASTCreateQuery & create) const
if (create.is_materialized_view && create.to_table_id)
return;

if (create.temporary)
{
/// Some part of storage definition is specified, but ENGINE is not: just set the one from default_temporary_table_engine setting.

if (!create.cluster.empty())
throw Exception(ErrorCodes::INCORRECT_QUERY, "Temporary tables cannot be created with ON CLUSTER clause");

if (!create.storage)
{
auto storage_ast = std::make_shared<ASTStorage>();
create.set(create.storage, storage_ast);
}

if (!create.storage->engine)
{
setDefaultTableEngine(*create.storage, getContext()->getSettingsRef().default_temporary_table_engine.value);
}

checkTemporaryTableEngineName(create.storage->engine->name);
return;
}

if (create.storage)
{
/// Some part of storage definition (such as PARTITION BY) is specified, but ENGINE is not: just set default one.
if (!create.storage->engine)
setDefaultTableEngine(*create.storage, getContext()->getSettingsRef().default_table_engine.value);
return;
}

if (!create.as_table.empty())
auto replace_storage = [&]()
{
/// NOTE Getting the structure from the table specified in the AS is done not atomically with the creation of the table.

Expand Down Expand Up @@ -1065,7 +1035,46 @@ void InterpreterCreateQuery::setEngine(ASTCreateQuery & create) const
create.set(create.as_table_function, as_create.as_table_function->ptr());
else
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot set engine, it's a bug.");
};

if (create.temporary)
{
/// Some part of storage definition is specified, but ENGINE is not: just set the one from default_temporary_table_engine setting.

if (!create.cluster.empty())
throw Exception(ErrorCodes::INCORRECT_QUERY, "Temporary tables cannot be created with ON CLUSTER clause");

if (create.as_table.empty())
{
if (!create.storage)
{
auto storage_ast = std::make_shared<ASTStorage>();
create.set(create.storage, storage_ast);
}

if (!create.storage->engine)
{
setDefaultTableEngine(*create.storage, getContext()->getSettingsRef().default_temporary_table_engine.value);
}
}
else
replace_storage();

checkTemporaryTableEngineName(create.storage->engine->name);
return;
}

if (create.storage)
{
/// Some part of storage definition (such as PARTITION BY) is specified, but ENGINE is not: just set default one.
if (!create.storage->engine)
setDefaultTableEngine(*create.storage, getContext()->getSettingsRef().default_table_engine.value);
return;
}

if (!create.as_table.empty())
{
replace_storage();
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TEMPORARY TABLE `02992_test2`\n(\n `a` UInt8,\n `b` String,\n `c` Nullable(Float32),\n `date` Date\n)\nENGINE = MergeTree\nPARTITION BY date\nORDER BY (a, b)\nSETTINGS index_granularity = 8192
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE 02992_test (a UInt8, b String, c Nullable(Float), date Date) Engine=MergeTree ORDER BY (a,b) PARTITION BY date;
CREATE TEMPORARY TABLE 02992_test2 AS 02992_test;
SHOW CREATE TEMPORARY TABLE 02992_test2;

0 comments on commit 2c6b1e1

Please sign in to comment.