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 7ba2a4a186..71a29fd6f8 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -2215,18 +2215,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| { @@ -2266,7 +2262,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 58aa5f662f..cf53638d4b 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -23670,3 +23670,51 @@ fn parse_grant_revoke_all_privileges_columns() { fn parse_pgq_keywords_as_identifiers() { all_dialects().verified_stmt("SELECT edge.label AS property FROM graph AS edge"); } + +#[test] +fn parse_qualified_role_keyword_function_calls() { + let dialects = all_dialects(); + + 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()) + ); + } +}