Skip to content

Commit 14eaf1c

Browse files
r1bayman-sigma
authored andcommitted
fix: parse error on unnamed arg with default syntax (apache#2091)
1 parent 349fa95 commit 14eaf1c

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
@@ -5553,7 +5553,21 @@ impl<'a> Parser<'a> {
55535553
// peek the next token, which if it is another type keyword, then the
55545554
// first token is a name and not a type in itself.
55555555
let data_type_idx = self.get_current_index();
5556-
if let Some(next_data_type) = self.maybe_parse(|parser| parser.parse_data_type())? {
5556+
5557+
// DEFAULT will be parsed as `DataType::Custom`, which is undesirable in this context
5558+
fn parse_data_type_no_default(parser: &mut Parser) -> Result<DataType, ParserError> {
5559+
if parser.peek_keyword(Keyword::DEFAULT) {
5560+
// This dummy error is ignored in `maybe_parse`
5561+
parser_err!(
5562+
"The DEFAULT keyword is not a type",
5563+
parser.peek_token().span.start
5564+
)
5565+
} else {
5566+
parser.parse_data_type()
5567+
}
5568+
}
5569+
5570+
if let Some(next_data_type) = self.maybe_parse(parse_data_type_no_default)? {
55575571
let token = self.token_at(data_type_idx);
55585572

55595573
// 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)