Prevent LOGICAL_ERROR on CREATE TABLE as MaterializedView

This commit is contained in:
Raúl Marín 2024-05-20 21:27:24 +02:00
parent 6e33443926
commit 84459052b6
3 changed files with 21 additions and 0 deletions

View File

@ -977,6 +977,13 @@ void InterpreterCreateQuery::setEngine(ASTCreateQuery & create) const
if (as_create.is_ordinary_view)
throw Exception(ErrorCodes::INCORRECT_QUERY, "Cannot CREATE a table AS {}, it is a View", qualified_name);
if (as_create.is_materialized_view && as_create.to_table_id)
throw Exception(
ErrorCodes::INCORRECT_QUERY,
"Cannot CREATE a table AS {}, it is a Materialized View without storage. Use \"AS `{}`\" instead",
qualified_name,
as_create.to_table_id.getQualifiedName());
if (as_create.is_live_view)
throw Exception(ErrorCodes::INCORRECT_QUERY, "Cannot CREATE a table AS {}, it is a Live View", qualified_name);

View File

@ -0,0 +1,14 @@
DROP TABLE IF EXISTS base_table;
DROP TABLE IF EXISTS target_table;
DROP TABLE IF EXISTS mv_from_base_to_target;
DROP TABLE IF EXISTS mv_with_storage;
DROP TABLE IF EXISTS other_table_1;
DROP TABLE IF EXISTS other_table_2;
CREATE TABLE base_table (date DateTime, id String, cost Float64) ENGINE = MergeTree() ORDER BY date;
CREATE TABLE target_table (id String, total AggregateFunction(sum, Float64)) ENGINE = MergeTree() ORDER BY id;
CREATE MATERIALIZED VIEW mv_from_base_to_target TO target_table AS Select id, sumState(cost) FROM base_table GROUP BY id;
CREATE MATERIALIZED VIEW mv_with_storage ENGINE=MergeTree() ORDER BY id AS Select id, sumState(cost) FROM base_table GROUP BY id;
CREATE TABLE other_table_1 AS mv_with_storage;
CREATE TABLE other_table_2 AS mv_from_base_to_target; -- { serverError INCORRECT_QUERY }