Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/postgres_qualified_role_keyword_call.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT app.current_user()
18 changes: 8 additions & 10 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
);
}
}
Loading