Skip to content

Commit

Permalink
fix: ensure Create Or Replace and If Not Exist cannot coexist in crea…
Browse files Browse the repository at this point in the history
…te view (#5003)

ensure Create Or Replace and If Not Exist cannot coexist in create view statement
  • Loading branch information
lyang24 authored Nov 17, 2024
1 parent 4b263ef commit 485782a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
14 changes: 10 additions & 4 deletions src/operator/src/statement/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
25 changes: 11 additions & 14 deletions tests/cases/standalone/common/view/show_create.result
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 485782a

Please sign in to comment.