diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 0b2158e6f..45cc9b751 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -5522,7 +5522,23 @@ impl<'a> Parser<'a> { // peek the next token, which if it is another type keyword, then the // first token is a name and not a type in itself. let data_type_idx = self.get_current_index(); - if let Some(next_data_type) = self.maybe_parse(|parser| parser.parse_data_type())? { + + // DEFAULT will be parsed as `DataType::Custom`, which is undesirable in this context + fn parse_data_type_no_default( + parser: &mut Parser, + ) -> Result { + if parser.peek_keyword(Keyword::DEFAULT) { + // This dummy error is ignored in `maybe_parse` + parser_err!( + "The DEFAULT keyword is not a type", + parser.peek_token().span.start + ) + } else { + parser.parse_data_type() + } + } + + if let Some(next_data_type) = self.maybe_parse(parse_data_type_no_default)? { let token = self.token_at(data_type_idx); // We ensure that the token is a `Word` token, and not other special tokens. diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 75d567c10..3bdf6d189 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -4475,7 +4475,12 @@ fn parse_create_function_detailed() { pg_and_generic().verified_stmt(r#"CREATE OR REPLACE FUNCTION increment(i INTEGER) RETURNS INTEGER LANGUAGE plpgsql AS $$ BEGIN RETURN i + 1; END; $$"#); pg_and_generic().verified_stmt(r#"CREATE OR REPLACE FUNCTION no_arg() RETURNS VOID LANGUAGE plpgsql AS $$ BEGIN DELETE FROM my_table; END; $$"#); 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; $$"#); + pg_and_generic().one_statement_parses_to( + "CREATE FUNCTION add(INTEGER, INTEGER DEFAULT 1) RETURNS INTEGER AS 'select $1 + $2;'", + "CREATE FUNCTION add(INTEGER, INTEGER = 1) RETURNS INTEGER AS 'select $1 + $2;'", + ); } + #[test] fn parse_incorrect_create_function_parallel() { let sql = "CREATE FUNCTION add(INTEGER, INTEGER) RETURNS INTEGER LANGUAGE SQL PARALLEL BLAH AS 'select $1 + $2;'";