Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/postgres_create_or_replace_procedure.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE OR REPLACE PROCEDURE p (a INT) LANGUAGE sql BEGIN ATOMIC INSERT INTO t VALUES (a); END
6 changes: 5 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4591,6 +4591,8 @@ pub enum Statement {
CreateProcedure {
/// `OR ALTER` flag.
or_alter: bool,
/// `OR REPLACE` flag.
or_replace: bool,
/// Procedure name.
name: ObjectName,
/// Parenthesized parameter list, `None` when absent.
Expand Down Expand Up @@ -5612,14 +5614,16 @@ impl fmt::Display for Statement {
Statement::CreateProcedure {
name,
or_alter,
or_replace,
params,
language,
body,
} => {
write!(
f,
"CREATE {or_alter}PROCEDURE {name}",
"CREATE {or_alter}{or_replace}PROCEDURE {name}",
or_alter = if *or_alter { "OR ALTER " } else { "" },
or_replace = if *or_replace { "OR REPLACE " } else { "" },
name = name
)?;

Expand Down
29 changes: 20 additions & 9 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5527,16 +5527,16 @@ impl<'a> Parser<'a> {
let persistent = dialect_of!(self is DuckDbDialect)
&& self.parse_one_of_keywords(&[Keyword::PERSISTENT]).is_some();
let create_view_params = self.parse_create_view_params()?;
// `or_replace` is caught by an earlier arm. It stays so that reordering cannot bypass it.
let has_modifier = or_replace
|| or_alter
let has_other_modifier = or_alter
|| temporary
|| global.is_some()
|| transient
|| volatile
|| multiset.is_some()
|| persistent
|| create_view_params.is_some();
// `or_replace` is caught by an earlier arm. It stays so that reordering cannot bypass it.
let has_modifier = or_replace || has_other_modifier;
if self.peek_keywords(&[Keyword::SNAPSHOT, Keyword::TABLE]) {
self.parse_create_snapshot_table().map(Into::into)
} else if self.peek_keywords(&[Keyword::TEXT, Keyword::SEARCH]) {
Expand Down Expand Up @@ -5599,9 +5599,19 @@ impl<'a> Parser<'a> {
self.parse_create_schema(or_replace)
} else if self.parse_keyword(Keyword::WAREHOUSE) {
self.parse_create_warehouse(or_replace).map(Into::into)
} else if self.parse_keyword(Keyword::PROCEDURE)
|| self.dialect.supports_create_proc_syntax() && self.parse_keyword(Keyword::PROC)
{
if or_replace && has_other_modifier {
return parser_err!(
"CREATE OR REPLACE PROCEDURE does not accept this modifier",
modifier_loc
);
}
self.parse_create_procedure(or_alter, or_replace)
} else if or_replace {
self.expected_ref(
"[EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION or AGGREGATE or SCHEMA or WAREHOUSE after CREATE OR REPLACE",
"[EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION or PROCEDURE or AGGREGATE or SCHEMA or WAREHOUSE after CREATE OR REPLACE",
self.peek_token_ref(),
)
} else if self.parse_keyword(Keyword::EXTENSION) {
Expand All @@ -5624,10 +5634,6 @@ impl<'a> Parser<'a> {
self.parse_create_collation().map(Into::into)
} else if self.parse_keyword(Keyword::TYPE) {
self.parse_create_type()
} else if self.parse_keyword(Keyword::PROCEDURE)
|| self.dialect.supports_create_proc_syntax() && self.parse_keyword(Keyword::PROC)
{
self.parse_create_procedure(or_alter)
} else if self.parse_keyword(Keyword::CONNECTOR) {
self.parse_create_connector().map(Into::into)
} else if self.parse_keyword(Keyword::OPERATOR) {
Expand Down Expand Up @@ -21971,7 +21977,11 @@ impl<'a> Parser<'a> {
}

/// Parse `CREATE PROCEDURE` statement.
pub fn parse_create_procedure(&mut self, or_alter: bool) -> Result<Statement, ParserError> {
pub fn parse_create_procedure(
&mut self,
or_alter: bool,
or_replace: bool,
) -> Result<Statement, ParserError> {
let name = self.parse_object_name(false)?;
let params = self.parse_optional_procedure_parameters()?;

Expand All @@ -21992,6 +22002,7 @@ impl<'a> Parser<'a> {
Ok(Statement::CreateProcedure {
name,
or_alter,
or_replace,
params,
language,
body,
Expand Down
48 changes: 48 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23481,3 +23481,51 @@ fn parse_trailing_end_keyword_is_not_silently_dropped() {
ParserError::ParserError("Expected: end of statement, found: END".to_string())
);
}

#[test]
fn parse_create_or_replace_procedure() {
match verified_stmt("CREATE OR REPLACE PROCEDURE p () LANGUAGE sql BEGIN ATOMIC SELECT 1; END")
{
Statement::CreateProcedure {
or_replace,
or_alter,
..
} => {
assert!(or_replace);
assert!(!or_alter);
}
stmt => panic!("unexpected statement: {stmt:?}"),
}

match verified_stmt("CREATE PROCEDURE p () LANGUAGE sql BEGIN ATOMIC SELECT 1; END") {
Statement::CreateProcedure { or_replace, .. } => assert!(!or_replace),
stmt => panic!("unexpected statement: {stmt:?}"),
}

verified_stmt("CREATE OR REPLACE PROCEDURE p (a INT) AS BEGIN SELECT 1; END");

for (sql, err) in [
(
"CREATE OR REPLACE PROCEDURE",
"Expected: identifier, found: EOF",
),
(
"CREATE OR REPLACE OR ALTER PROCEDURE p AS BEGIN SELECT 1; END",
"CREATE OR REPLACE PROCEDURE does not accept this modifier",
),
(
"CREATE OR REPLACE TEMPORARY PROCEDURE p AS BEGIN SELECT 1; END",
"CREATE OR REPLACE PROCEDURE does not accept this modifier",
),
(
"CREATE OR REPLACE EXTENSION e",
"Expected: [EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION or PROCEDURE or AGGREGATE or SCHEMA or WAREHOUSE after CREATE OR REPLACE, found: EXTENSION",
),
] {
assert_eq!(
parse_sql_statements(sql).unwrap_err(),
ParserError::ParserError(err.to_string()),
"{sql}"
);
}
}
1 change: 1 addition & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ fn parse_create_procedure() {
ms().verified_stmt(sql),
Statement::CreateProcedure {
or_alter: true,
or_replace: false,
body: CreateProcedureBody::As(ConditionalStatements::BeginEnd(BeginEndStatements {
begin_token: AttachedToken::empty(),
statements: vec![Statement::Query(Box::new(Query {
Expand Down
Loading