fix: ensure Create Or Replace and If Not Exist cannot coexist in create view (#5003)

ensure Create Or Replace and If Not Exist cannot coexist in create view statement
This commit is contained in:
Lanqing Yang
2024-11-16 23:08:30 -08:00
committed by GitHub
parent 4b263ef1cc
commit 485782af51
2 changed files with 21 additions and 18 deletions

View File

@@ -71,10 +71,10 @@ use crate::error::{
self, AlterExprToRequestSnafu, CatalogSnafu, ColumnDataTypeSnafu, ColumnNotFoundSnafu,
ConvertSchemaSnafu, CreateLogicalTablesSnafu, CreateTableInfoSnafu, DeserializePartitionSnafu,
EmptyDdlExprSnafu, ExtractTableNamesSnafu, FlowNotFoundSnafu, InvalidPartitionRuleSnafu,
InvalidPartitionSnafu, InvalidTableNameSnafu, InvalidViewNameSnafu, InvalidViewStmtSnafu,
ParseSqlValueSnafu, Result, SchemaInUseSnafu, SchemaNotFoundSnafu, SchemaReadOnlySnafu,
SubstraitCodecSnafu, TableAlreadyExistsSnafu, TableMetadataManagerSnafu, TableNotFoundSnafu,
UnrecognizedTableOptionSnafu, ViewAlreadyExistsSnafu,
InvalidPartitionSnafu, InvalidSqlSnafu, InvalidTableNameSnafu, InvalidViewNameSnafu,
InvalidViewStmtSnafu, ParseSqlValueSnafu, Result, SchemaInUseSnafu, SchemaNotFoundSnafu,
SchemaReadOnlySnafu, SubstraitCodecSnafu, TableAlreadyExistsSnafu, TableMetadataManagerSnafu,
TableNotFoundSnafu, UnrecognizedTableOptionSnafu, ViewAlreadyExistsSnafu,
};
use crate::expr_factory;
use crate::statement::show::create_partitions_stmt;
@@ -465,6 +465,12 @@ impl StatementExecutor {
expr: CreateViewExpr,
ctx: QueryContextRef,
) -> Result<TableRef> {
ensure! {
!(expr.create_if_not_exists & expr.or_replace),
InvalidSqlSnafu {
err_msg: "syntax error Create Or Replace and If Not Exist cannot be used together",
}
};
let _timer = crate::metrics::DIST_CREATE_VIEW.start_timer();
let schema_exists = self

View File

@@ -97,26 +97,23 @@ SELECT * FROM v1;
--- if not exists with replace, so it changes ---
CREATE OR REPLACE VIEW IF NOT EXISTS v1 AS SELECT c FROM t1;
Affected Rows: 0
Error: 1004(InvalidArguments), Invalid SQL, error: syntax error Create Or Replace and If Not Exist cannot be used together
SHOW CREATE VIEW v1;
+------+-------------------------------------------------------------+
| View | Create View |
+------+-------------------------------------------------------------+
| v1 | CREATE OR REPLACE VIEW IF NOT EXISTS v1 AS SELECT c FROM t1 |
+------+-------------------------------------------------------------+
+------+------------------------------------------------------------------+
| View | Create View |
+------+------------------------------------------------------------------+
| v1 | CREATE OR REPLACE VIEW v1 AS SELECT a, b, c FROM t1 WHERE a > 43 |
+------+------------------------------------------------------------------+
SELECT * FROM v1;
+-------------------------+
| c |
+-------------------------+
| 1970-01-01T00:00:00.001 |
| 1970-01-01T00:00:00.002 |
| 1970-01-01T00:00:00.003 |
| 1970-01-01T00:00:00.004 |
+-------------------------+
+----+------------+-------------------------+
| a | b | c |
+----+------------+-------------------------+
| 44 | greptimedb | 1970-01-01T00:00:00.004 |
+----+------------+-------------------------+
DROP VIEW v1;