From 6180482de3fa63c54e80ffcef411e12a3834067b Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Sat, 7 Nov 2026 19:03:44 +0100 Subject: [PATCH 1/2] PostgreSQL: Parse schema-qualified calls to role keyword functions --- .../postgres_qualified_role_keyword_call.sql | 1 + src/parser/mod.rs | 18 +++---- tests/sqlparser_common.rs | 52 +++++++++++++++++++ 3 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 fuzz/fuzz_seeds/postgres_qualified_role_keyword_call.sql diff --git a/fuzz/fuzz_seeds/postgres_qualified_role_keyword_call.sql b/fuzz/fuzz_seeds/postgres_qualified_role_keyword_call.sql new file mode 100644 index 0000000000..16fae55ae3 --- /dev/null +++ b/fuzz/fuzz_seeds/postgres_qualified_role_keyword_call.sql @@ -0,0 +1 @@ +SELECT app.current_user() \ No newline at end of file diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 1d6cc53417..0314ecae18 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -2210,18 +2210,14 @@ impl<'a> Parser<'a> { // recursive `parse_subexpr` would re-walk the rest of the chain at // every dot. _ => { - // For a plain `Word` field (not followed by `(`), skip the - // speculative `parse_prefix`. The only result the validator - // below would accept is `Identifier`, which `parse_identifier` - // in the None branch produces directly. This avoids 2^N work + // For a plain `Word` field, skip the + // speculative `parse_prefix`. The only results the validator + // below would accept are `Identifier` and `Function`, which + // the None branch produces directly. This avoids 2^N work // on chains like `.not-b.not-b...` where `parse_prefix` would // descend into `parse_not` and re-walk the remaining chain at // every segment. - let word_field_no_lparen = - matches!(self.peek_token_ref().token, Token::Word(_)) - && self.peek_nth_token_ref(1).token != Token::LParen; - - let expr = if word_field_no_lparen { + let expr = if matches!(self.peek_token_ref().token, Token::Word(_)) { None } else { self.maybe_parse(|parser| { @@ -2261,7 +2257,9 @@ impl<'a> Parser<'a> { } None => { let ident = self.parse_identifier()?; - let field = if self.peek_token_ref().token == Token::LParen { + let field = if self.peek_token_ref().token == Token::LParen + && !self.peek_outer_join_operator() + { self.parse_function(ObjectName::from(vec![ident]))? } else { Expr::Identifier(ident) diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index aeb3e67a47..d7e0570d0a 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -23481,3 +23481,55 @@ fn parse_trailing_end_keyword_is_not_silently_dropped() { ParserError::ParserError("Expected: end of statement, found: END".to_string()) ); } + +#[test] +fn parse_qualified_role_keyword_function_calls() { + let dialects = all_dialects(); + let pg_and_generic = TestedDialects::new(vec![ + Box::new(PostgreSqlDialect {}), + Box::new(GenericDialect {}), + ]); + + for func in ["current_user", "session_user", "user", "current_catalog"] { + for (sql_args, expected_args) in [ + ("", vec![]), + ( + "1", + vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::value( + number("1"), + )))], + ), + ] { + let sql = format!("SELECT app.{func}({sql_args})"); + match expr_from_projection(&dialects.verified_only_select(&sql).projection[0]) { + Expr::Function(Function { + name, + args: FunctionArguments::List(FunctionArgumentList { args, .. }), + .. + }) => { + assert_eq!( + name, + &ObjectName::from(vec![Ident::new("app"), Ident::new(func)]) + ); + assert_eq!(args, &expected_args); + } + other => panic!("Expected a qualified function call, got: {other:?}"), + } + } + + let sql = format!("SELECT d.{func} FROM d"); + match expr_from_projection(&dialects.verified_only_select(&sql).projection[0]) { + Expr::CompoundIdentifier(idents) => { + assert_eq!(idents, &vec![Ident::new("d"), Ident::new(func)]) + } + other => panic!("Expected CompoundIdentifier, got: {other:?}"), + } + + assert_eq!( + pg_and_generic + .parse_sql_statements(&format!("SELECT {func}()")) + .unwrap_err(), + ParserError::ParserError("Expected: end of statement, found: (".to_string()) + ); + } +} From 7608c28e0fc3e0e47ae29a6a9c84c9c22afc29af Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Wed, 25 Nov 2026 07:39:39 +0100 Subject: [PATCH 2/2] Reuse pg_and_generic in the qualified role keyword test --- tests/sqlparser_common.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 71455ac92f..cf53638d4b 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -23674,10 +23674,6 @@ fn parse_pgq_keywords_as_identifiers() { #[test] fn parse_qualified_role_keyword_function_calls() { let dialects = all_dialects(); - let pg_and_generic = TestedDialects::new(vec![ - Box::new(PostgreSqlDialect {}), - Box::new(GenericDialect {}), - ]); for func in ["current_user", "session_user", "user", "current_catalog"] { for (sql_args, expected_args) in [ @@ -23715,7 +23711,7 @@ fn parse_qualified_role_keyword_function_calls() { } assert_eq!( - pg_and_generic + pg_and_generic() .parse_sql_statements(&format!("SELECT {func}()")) .unwrap_err(), ParserError::ParserError("Expected: end of statement, found: (".to_string())