Skip to content

Commit 2c3f3f1

Browse files
authored
PostgreSQL: COMMENT ON FUNCTION/PROCEDURE/AGGREGATE accepts argmode/argname/VARIADIC (#28)
Postgres permits argmode (IN/OUT/INOUT/VARIADIC) and argname tokens ahead of each argument type in COMMENT ON FUNCTION / PROCEDURE / AGGREGATE; they are ignored for identity resolution but appear in real-world dumps and consumer schema files. The previous parser used bare parse_data_type for the argument list, which hard-failed on those shapes. Switch the function-like arms of parse_comment to parse_function_arg and project to data_type, since Statement::Comment.arguments stores a plain Vec<DataType> and the modes/names are intentionally discarded for identity matching. Add tests covering argname, IN/OUT/INOUT, VARIADIC, and AGGREGATE.
1 parent 4fe6b06 commit 2c3f3f1

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

src/parser/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,9 +970,10 @@ impl<'a> Parser<'a> {
970970
let arguments = match object_type {
971971
CommentObject::Function | CommentObject::Procedure | CommentObject::Aggregate => {
972972
if self.consume_token(&Token::LParen) {
973-
let list = self.parse_comma_separated0(Self::parse_data_type, Token::RParen)?;
973+
let args =
974+
self.parse_comma_separated0(Self::parse_function_arg, Token::RParen)?;
974975
self.expect_token(&Token::RParen)?;
975-
Some(list)
976+
Some(args.into_iter().map(|a| a.data_type).collect())
976977
} else {
977978
None
978979
}

tests/sqlparser_postgres.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10677,6 +10677,46 @@ fn parse_comment_on_function_with_arg_types() {
1067710677
}
1067810678
}
1067910679

10680+
#[test]
10681+
fn parse_comment_on_function_with_argname() {
10682+
pg_and_generic().one_statement_parses_to(
10683+
"COMMENT ON FUNCTION add(a INTEGER, b INTEGER) IS 'adds'",
10684+
"COMMENT ON FUNCTION add(INTEGER, INTEGER) IS 'adds'",
10685+
);
10686+
}
10687+
10688+
#[test]
10689+
fn parse_comment_on_function_with_argmode() {
10690+
pg_and_generic().one_statement_parses_to(
10691+
"COMMENT ON FUNCTION upsert(IN id INTEGER, OUT result TEXT) IS 'upserts'",
10692+
"COMMENT ON FUNCTION upsert(INTEGER, TEXT) IS 'upserts'",
10693+
);
10694+
pg_and_generic().one_statement_parses_to(
10695+
"COMMENT ON FUNCTION swap(INOUT a INTEGER, INOUT b INTEGER) IS 'swap'",
10696+
"COMMENT ON FUNCTION swap(INTEGER, INTEGER) IS 'swap'",
10697+
);
10698+
}
10699+
10700+
#[test]
10701+
fn parse_comment_on_function_with_variadic() {
10702+
pg_and_generic().one_statement_parses_to(
10703+
"COMMENT ON FUNCTION concat_all(VARIADIC arr TEXT[]) IS 'joins'",
10704+
"COMMENT ON FUNCTION concat_all(TEXT[]) IS 'joins'",
10705+
);
10706+
}
10707+
10708+
#[test]
10709+
fn parse_comment_on_aggregate_with_argname_and_variadic() {
10710+
pg_and_generic().one_statement_parses_to(
10711+
"COMMENT ON AGGREGATE my_sum(val INTEGER) IS 'sums'",
10712+
"COMMENT ON AGGREGATE my_sum(INTEGER) IS 'sums'",
10713+
);
10714+
pg_and_generic().one_statement_parses_to(
10715+
"COMMENT ON AGGREGATE concat_agg(VARIADIC arr TEXT[]) IS 'agg'",
10716+
"COMMENT ON AGGREGATE concat_agg(TEXT[]) IS 'agg'",
10717+
);
10718+
}
10719+
1068010720
#[test]
1068110721
fn parse_alter_type_add_attribute() {
1068210722
let sql = "ALTER TYPE public.my_type ADD ATTRIBUTE new_attr INTEGER";

0 commit comments

Comments
 (0)