Skip to content

Commit 848e117

Browse files
Merge branch 'main' into extended-create-func
2 parents 716efdc + 1198c1a commit 848e117

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/parser/mod.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5522,7 +5522,21 @@ impl<'a> Parser<'a> {
55225522
// peek the next token, which if it is another type keyword, then the
55235523
// first token is a name and not a type in itself.
55245524
let data_type_idx = self.get_current_index();
5525-
if let Some(next_data_type) = self.maybe_parse(|parser| parser.parse_data_type())? {
5525+
5526+
// DEFAULT will be parsed as `DataType::Custom`, which is undesirable in this context
5527+
fn parse_data_type_no_default(parser: &mut Parser) -> Result<DataType, ParserError> {
5528+
if parser.peek_keyword(Keyword::DEFAULT) {
5529+
// This dummy error is ignored in `maybe_parse`
5530+
parser_err!(
5531+
"The DEFAULT keyword is not a type",
5532+
parser.peek_token().span.start
5533+
)
5534+
} else {
5535+
parser.parse_data_type()
5536+
}
5537+
}
5538+
5539+
if let Some(next_data_type) = self.maybe_parse(parse_data_type_no_default)? {
55265540
let token = self.token_at(data_type_idx);
55275541

55285542
// We ensure that the token is a `Word` token, and not other special tokens.

tests/sqlparser_postgres.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4475,7 +4475,12 @@ fn parse_create_function_detailed() {
44754475
pg_and_generic().verified_stmt(r#"CREATE OR REPLACE FUNCTION increment(i INTEGER) RETURNS INTEGER LANGUAGE plpgsql AS $$ BEGIN RETURN i + 1; END; $$"#);
44764476
pg_and_generic().verified_stmt(r#"CREATE OR REPLACE FUNCTION no_arg() RETURNS VOID LANGUAGE plpgsql AS $$ BEGIN DELETE FROM my_table; END; $$"#);
44774477
pg_and_generic().verified_stmt(r#"CREATE OR REPLACE FUNCTION return_table(i INTEGER) RETURNS TABLE(id UUID, is_active BOOLEAN) LANGUAGE plpgsql AS $$ BEGIN RETURN QUERY SELECT NULL::UUID, NULL::BOOLEAN; END; $$"#);
4478+
pg_and_generic().one_statement_parses_to(
4479+
"CREATE FUNCTION add(INTEGER, INTEGER DEFAULT 1) RETURNS INTEGER AS 'select $1 + $2;'",
4480+
"CREATE FUNCTION add(INTEGER, INTEGER = 1) RETURNS INTEGER AS 'select $1 + $2;'",
4481+
);
44784482
}
4483+
44794484
#[test]
44804485
fn parse_incorrect_create_function_parallel() {
44814486
let sql = "CREATE FUNCTION add(INTEGER, INTEGER) RETURNS INTEGER LANGUAGE SQL PARALLEL BLAH AS 'select $1 + $2;'";

0 commit comments

Comments
 (0)