Skip to content

Commit cf49649

Browse files
committed
PostgreSQL: Support CREATE OR REPLACE PROCEDURE
1 parent 5201fb1 commit cf49649

5 files changed

Lines changed: 75 additions & 10 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE OR REPLACE PROCEDURE p (a INT) LANGUAGE sql BEGIN ATOMIC INSERT INTO t VALUES (a); END

‎src/ast/mod.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4591,6 +4591,8 @@ pub enum Statement {
45914591
CreateProcedure {
45924592
/// `OR ALTER` flag.
45934593
or_alter: bool,
4594+
/// `OR REPLACE` flag.
4595+
or_replace: bool,
45944596
/// Procedure name.
45954597
name: ObjectName,
45964598
/// Parenthesized parameter list, `None` when absent.
@@ -5612,14 +5614,16 @@ impl fmt::Display for Statement {
56125614
Statement::CreateProcedure {
56135615
name,
56145616
or_alter,
5617+
or_replace,
56155618
params,
56165619
language,
56175620
body,
56185621
} => {
56195622
write!(
56205623
f,
5621-
"CREATE {or_alter}PROCEDURE {name}",
5624+
"CREATE {or_alter}{or_replace}PROCEDURE {name}",
56225625
or_alter = if *or_alter { "OR ALTER " } else { "" },
5626+
or_replace = if *or_replace { "OR REPLACE " } else { "" },
56235627
name = name
56245628
)?;
56255629

‎src/parser/mod.rs‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5527,16 +5527,16 @@ impl<'a> Parser<'a> {
55275527
let persistent = dialect_of!(self is DuckDbDialect)
55285528
&& self.parse_one_of_keywords(&[Keyword::PERSISTENT]).is_some();
55295529
let create_view_params = self.parse_create_view_params()?;
5530-
// `or_replace` is caught by an earlier arm. It stays so that reordering cannot bypass it.
5531-
let has_modifier = or_replace
5532-
|| or_alter
5530+
let has_other_modifier = or_alter
55335531
|| temporary
55345532
|| global.is_some()
55355533
|| transient
55365534
|| volatile
55375535
|| multiset.is_some()
55385536
|| persistent
55395537
|| create_view_params.is_some();
5538+
// `or_replace` is caught by an earlier arm. It stays so that reordering cannot bypass it.
5539+
let has_modifier = or_replace || has_other_modifier;
55405540
if self.peek_keywords(&[Keyword::SNAPSHOT, Keyword::TABLE]) {
55415541
self.parse_create_snapshot_table().map(Into::into)
55425542
} else if self.peek_keywords(&[Keyword::TEXT, Keyword::SEARCH]) {
@@ -5599,9 +5599,19 @@ impl<'a> Parser<'a> {
55995599
self.parse_create_schema(or_replace)
56005600
} else if self.parse_keyword(Keyword::WAREHOUSE) {
56015601
self.parse_create_warehouse(or_replace).map(Into::into)
5602+
} else if self.parse_keyword(Keyword::PROCEDURE)
5603+
|| self.dialect.supports_create_proc_syntax() && self.parse_keyword(Keyword::PROC)
5604+
{
5605+
if or_replace && has_other_modifier {
5606+
return parser_err!(
5607+
"CREATE OR REPLACE PROCEDURE does not accept this modifier",
5608+
modifier_loc
5609+
);
5610+
}
5611+
self.parse_create_procedure(or_alter, or_replace)
56025612
} else if or_replace {
56035613
self.expected_ref(
5604-
"[EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION or AGGREGATE or SCHEMA or WAREHOUSE after CREATE OR REPLACE",
5614+
"[EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION or PROCEDURE or AGGREGATE or SCHEMA or WAREHOUSE after CREATE OR REPLACE",
56055615
self.peek_token_ref(),
56065616
)
56075617
} else if self.parse_keyword(Keyword::EXTENSION) {
@@ -5624,10 +5634,6 @@ impl<'a> Parser<'a> {
56245634
self.parse_create_collation().map(Into::into)
56255635
} else if self.parse_keyword(Keyword::TYPE) {
56265636
self.parse_create_type()
5627-
} else if self.parse_keyword(Keyword::PROCEDURE)
5628-
|| self.dialect.supports_create_proc_syntax() && self.parse_keyword(Keyword::PROC)
5629-
{
5630-
self.parse_create_procedure(or_alter)
56315637
} else if self.parse_keyword(Keyword::CONNECTOR) {
56325638
self.parse_create_connector().map(Into::into)
56335639
} else if self.parse_keyword(Keyword::OPERATOR) {
@@ -22012,7 +22018,11 @@ impl<'a> Parser<'a> {
2201222018
}
2201322019

2201422020
/// Parse `CREATE PROCEDURE` statement.
22015-
pub fn parse_create_procedure(&mut self, or_alter: bool) -> Result<Statement, ParserError> {
22021+
pub fn parse_create_procedure(
22022+
&mut self,
22023+
or_alter: bool,
22024+
or_replace: bool,
22025+
) -> Result<Statement, ParserError> {
2201622026
let name = self.parse_object_name(false)?;
2201722027
let params = self.parse_optional_procedure_parameters()?;
2201822028

@@ -22047,6 +22057,7 @@ impl<'a> Parser<'a> {
2204722057
Ok(Statement::CreateProcedure {
2204822058
name,
2204922059
or_alter,
22060+
or_replace,
2205022061
params,
2205122062
language,
2205222063
body,

‎tests/sqlparser_common.rs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23579,3 +23579,51 @@ $$"#,
2357923579
);
2358023580
}
2358123581
}
23582+
23583+
#[test]
23584+
fn parse_create_or_replace_procedure() {
23585+
match verified_stmt("CREATE OR REPLACE PROCEDURE p () LANGUAGE sql BEGIN ATOMIC SELECT 1; END")
23586+
{
23587+
Statement::CreateProcedure {
23588+
or_replace,
23589+
or_alter,
23590+
..
23591+
} => {
23592+
assert!(or_replace);
23593+
assert!(!or_alter);
23594+
}
23595+
stmt => panic!("unexpected statement: {stmt:?}"),
23596+
}
23597+
23598+
match verified_stmt("CREATE PROCEDURE p () LANGUAGE sql BEGIN ATOMIC SELECT 1; END") {
23599+
Statement::CreateProcedure { or_replace, .. } => assert!(!or_replace),
23600+
stmt => panic!("unexpected statement: {stmt:?}"),
23601+
}
23602+
23603+
verified_stmt("CREATE OR REPLACE PROCEDURE p (a INT) AS BEGIN SELECT 1; END");
23604+
23605+
for (sql, err) in [
23606+
(
23607+
"CREATE OR REPLACE PROCEDURE",
23608+
"Expected: identifier, found: EOF",
23609+
),
23610+
(
23611+
"CREATE OR REPLACE OR ALTER PROCEDURE p AS BEGIN SELECT 1; END",
23612+
"CREATE OR REPLACE PROCEDURE does not accept this modifier",
23613+
),
23614+
(
23615+
"CREATE OR REPLACE TEMPORARY PROCEDURE p AS BEGIN SELECT 1; END",
23616+
"CREATE OR REPLACE PROCEDURE does not accept this modifier",
23617+
),
23618+
(
23619+
"CREATE OR REPLACE EXTENSION e",
23620+
"Expected: [EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION or PROCEDURE or AGGREGATE or SCHEMA or WAREHOUSE after CREATE OR REPLACE, found: EXTENSION",
23621+
),
23622+
] {
23623+
assert_eq!(
23624+
parse_sql_statements(sql).unwrap_err(),
23625+
ParserError::ParserError(err.to_string()),
23626+
"{sql}"
23627+
);
23628+
}
23629+
}

‎tests/sqlparser_mssql.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ fn parse_create_procedure() {
134134
ms().verified_stmt(sql),
135135
Statement::CreateProcedure {
136136
or_alter: true,
137+
or_replace: false,
137138
body: CreateProcedureBody::As(ConditionalStatements::BeginEnd(BeginEndStatements {
138139
begin_token: AttachedToken::empty(),
139140
statements: vec![Statement::Query(Box::new(Query {

0 commit comments

Comments
 (0)