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_function_begin_atomic.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE FUNCTION f(a INT) RETURNS INT LANGUAGE sql BEGIN ATOMIC INSERT INTO t VALUES (a); RETURN a + 1; END
28 changes: 19 additions & 9 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ use sqlparser_derive::{Visit, VisitMut};

use crate::ast::value::escape_single_quote_string;
use crate::ast::{
display_comma_separated, display_separated,
display_comma_separated, display_separated, format_statement_list,
table_constraints::{
CheckConstraint, ForeignKeyConstraint, PrimaryKeyConstraint, TableConstraint,
UniqueConstraint,
},
ArgMode, AttachedToken, CommentDef, ConditionalStatements, CreateFunctionBody,
CreateFunctionUsing, CreateServerOption, CreateTableLikeKind, CreateTableOptions,
CreateViewParams, DataType, Expr, FileFormat, FunctionBehavior, FunctionCalledOnNull,
FunctionDefinitionSetParam, FunctionDesc, FunctionDeterminismSpecifier, FunctionParallel,
FunctionSecurity, HiveDistributionStyle, HiveFormat, HiveIOFormat, HiveRowFormat,
HiveSetLocation, Ident, InitializeKind, MySQLColumnPosition, ObjectName, OnCommit,
OneOrManyWithParens, OperateFunctionArg, OrderByExpr, ProjectionSelect, Query, RefreshModeKind,
ResetConfig, RowAccessPolicy, SequenceOptions, Spanned, SqlOption, Statement,
ArgMode, AttachedToken, BeginEndStatements, CommentDef, ConditionalStatements,
CreateFunctionBody, CreateFunctionUsing, CreateServerOption, CreateTableLikeKind,
CreateTableOptions, CreateViewParams, DataType, Expr, FileFormat, FunctionBehavior,
FunctionCalledOnNull, FunctionDefinitionSetParam, FunctionDesc, FunctionDeterminismSpecifier,
FunctionParallel, FunctionSecurity, HiveDistributionStyle, HiveFormat, HiveIOFormat,
HiveRowFormat, HiveSetLocation, Ident, InitializeKind, MySQLColumnPosition, ObjectName,
OnCommit, OneOrManyWithParens, OperateFunctionArg, OrderByExpr, ProjectionSelect, Query,
RefreshModeKind, ResetConfig, RowAccessPolicy, SequenceOptions, Spanned, SqlOption, Statement,
StorageLifecyclePolicy, StorageSerializationPolicy, TableVersion, Tag, TriggerEvent,
TriggerExecBody, TriggerObject, TriggerPeriod, TriggerReferencing, Value, ValueWithSpan,
WrappedCollection,
Expand Down Expand Up @@ -3867,6 +3867,16 @@ impl fmt::Display for CreateFunction {
if let Some(CreateFunctionBody::Return(function_body)) = &self.function_body {
write!(f, " RETURN {function_body}")?;
}
if let Some(CreateFunctionBody::BeginAtomic(BeginEndStatements { statements, .. })) =
&self.function_body
{
write!(f, " BEGIN ATOMIC")?;
if !statements.is_empty() {
write!(f, " ")?;
format_statement_list(f, statements)?;
}
write!(f, " END")?;
}
if let Some(CreateFunctionBody::AsReturnExpr(function_body)) = &self.function_body {
write!(f, " AS RETURN {function_body}")?;
}
Expand Down
4 changes: 4 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10496,6 +10496,10 @@ pub enum CreateFunctionBody {
///
/// [PostgreSQL]: https://www.postgresql.org/docs/current/sql-createfunction.html
Return(Expr),
/// SQL-standard function body enclosed by `BEGIN ATOMIC` and `END`.
///
/// [PostgreSQL]: https://www.postgresql.org/docs/current/sql-createfunction.html
BeginAtomic(BeginEndStatements),

/// Function body expression using the 'AS RETURN' keywords
///
Expand Down
11 changes: 11 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6145,6 +6145,17 @@ impl<'a> Parser<'a> {
} else if self.parse_keyword(Keyword::RETURN) {
ensure_not_set(&body.function_body, "RETURN")?;
body.function_body = Some(CreateFunctionBody::Return(self.parse_expr()?));
} else if self.peek_keywords(&[Keyword::BEGIN, Keyword::ATOMIC]) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostgreSQL accepts the same body for procedures (CREATE PROCEDURE p() LANGUAGE sql BEGIN ATOMIC INSERT INTO t VALUES (1); END), which parse_create_procedure still rejects at AS. Fine as a follow-up.

ensure_not_set(&body.function_body, "BEGIN ATOMIC")?;
let begin_token = self.next_token();
self.advance_token();
let statements = self.parse_statement_list(&[Keyword::END])?;
let end_token = self.expect_keyword(Keyword::END)?;
body.function_body = Some(CreateFunctionBody::BeginAtomic(BeginEndStatements {
begin_token: AttachedToken(begin_token),
statements,
end_token: AttachedToken(end_token),
}));
} else {
break;
}
Expand Down
90 changes: 90 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11115,3 +11115,93 @@ fn display_column_unique_nulls_distinct_propagates_formatter_errors() {
let stmt = pg_and_generic().verified_stmt("CREATE TABLE t (u INT UNIQUE NULLS NOT DISTINCT)");
assert_eq!(write!(RejectingWriter, "{stmt}"), Err(fmt::Error));
}

#[test]
fn parse_create_function_begin_atomic() {
let sql = "CREATE FUNCTION f() RETURNS INT LANGUAGE sql BEGIN ATOMIC SELECT 1; END";
pg_and_generic().verified_stmt(sql);
match pg_and_generic().run_parser_method(sql, |parser| parser.parse_statement().unwrap()) {
Statement::CreateFunction(CreateFunction {
function_body: Some(CreateFunctionBody::BeginAtomic(body)),
..
}) => {
assert_eq!(
body.statements,
vec![pg_and_generic().verified_stmt("SELECT 1")]
);
assert_eq!(
body.span(),
Span::new(Location::new(1, 46), Location::new(1, 72))
);
}
stmt => panic!("unexpected statement: {stmt:?}"),
}

for sql in [
"CREATE FUNCTION f(a INT) RETURNS INT LANGUAGE sql IMMUTABLE BEGIN ATOMIC SELECT a; RETURN a + 1; END",
"CREATE OR REPLACE FUNCTION f(a INT) RETURNS INT LANGUAGE sql BEGIN ATOMIC SELECT CASE WHEN a > 0 THEN 1 END; END",
"CREATE FUNCTION f() RETURNS VOID LANGUAGE sql BEGIN ATOMIC END",
] {
pg_and_generic().verified_stmt(sql);
}

pg_and_generic().one_statement_parses_to(
r#"CREATE OR REPLACE FUNCTION public.f(a integer)
RETURNS integer
LANGUAGE sql
BEGIN ATOMIC
INSERT INTO t VALUES (a);
RETURN a;
END"#,
"CREATE OR REPLACE FUNCTION public.f(a INTEGER) RETURNS INTEGER LANGUAGE sql BEGIN ATOMIC INSERT INTO t VALUES (a); RETURN a; END",
);

for (sql, err) in [
(
"CREATE FUNCTION f() RETURNS INT LANGUAGE sql BEGIN ATOMIC SELECT 1;",
"Expected: END, found: EOF",
),
(
"CREATE FUNCTION f() RETURNS INT LANGUAGE sql BEGIN ATOMIC SELECT 1 END",
"Expected: ;, found: END",
),
(
"CREATE FUNCTION f() RETURNS INT LANGUAGE sql RETURN 1 BEGIN ATOMIC SELECT 1; END",
"BEGIN ATOMIC specified more than once",
),
(
"CREATE FUNCTION f() RETURNS INT LANGUAGE sql BEGIN SELECT 1; END",
"Expected: end of statement, found: BEGIN",
),
] {
assert_eq!(
pg_and_generic().parse_sql_statements(sql).unwrap_err(),
ParserError::ParserError(err.to_string()),
"{sql}"
);
}
}

#[test]
fn display_create_function_begin_atomic_propagates_write_errors() {
use core::fmt::{self, Write};

struct RejectingWriter(&'static str);

impl Write for RejectingWriter {
fn write_str(&mut self, value: &str) -> fmt::Result {
if value == self.0 {
Err(fmt::Error)
} else {
Ok(())
}
}
}

let stmt = pg_and_generic()
.verified_stmt("CREATE FUNCTION f() RETURNS INT LANGUAGE sql BEGIN ATOMIC SELECT 1; END");
for rejected in [" BEGIN ATOMIC", " ", ";", " END"] {
let mut writer = RejectingWriter(rejected);
assert_eq!(write!(writer, "{stmt}"), Err(fmt::Error), "{rejected:?}");
}
}
Loading