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/generic_2050_function_span.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT foo();
3 changes: 3 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8397,6 +8397,9 @@ impl fmt::Display for FunctionArguments {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct FunctionArgumentList {
/// The closing parenthesis token.
#[cfg_attr(feature = "serde", serde(default = "AttachedToken::empty"))]
pub closing_token: AttachedToken,
/// `[ ALL | DISTINCT ]`
pub duplicate_treatment: Option<DuplicateTreatment>,
/// The function arguments.
Expand Down
4 changes: 3 additions & 1 deletion src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,7 @@ impl Spanned for FunctionArguments {
impl Spanned for FunctionArgumentList {
fn span(&self) -> Span {
let FunctionArgumentList {
closing_token,
duplicate_treatment: _, // enum
args,
clauses,
Expand All @@ -1873,7 +1874,8 @@ impl Spanned for FunctionArgumentList {
// # todo: duplicate-treatment span
args.iter()
.map(|i| i.span())
.chain(clauses.iter().map(|i| i.span())),
.chain(clauses.iter().map(|i| i.span()))
.chain(iter::once(closing_token.0.span)),
)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ where
/// name: ObjectName::from(vec![Ident::new("f")]),
/// uses_odbc_syntax: false,
/// args: FunctionArguments::List(FunctionArgumentList {
/// closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
/// duplicate_treatment: None,
/// args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(old_expr))],
/// clauses: vec![],
Expand Down
10 changes: 7 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2578,8 +2578,9 @@ impl<'a> Parser<'a> {
arg: FunctionArgExpr::Expr(self.parse_expr()?),
operator: FunctionArgOperator::Space,
};
self.expect_token(&Token::RParen)?;
let closing_token = self.expect_token(&Token::RParen)?.into();
Ok(FunctionArgumentList {
closing_token,
duplicate_treatment: None,
args: vec![arg],
clauses: vec![],
Expand Down Expand Up @@ -19691,8 +19692,10 @@ impl<'a> Parser<'a> {
));
}

if self.consume_token(&Token::RParen) {
if self.peek_token_ref().token == Token::RParen {
let closing_token = self.next_token().into();
return Ok(FunctionArgumentList {
closing_token,
duplicate_treatment: None,
args: vec![],
clauses,
Expand Down Expand Up @@ -19765,8 +19768,9 @@ impl<'a> Parser<'a> {
));
}

self.expect_token(&Token::RParen)?;
let closing_token = self.expect_token(&Token::RParen)?.into();
Ok(FunctionArgumentList {
closing_token,
duplicate_treatment,
args,
clauses,
Expand Down
1 change: 1 addition & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ pub fn call(function: &str, args: impl IntoIterator<Item = Expr>) -> Expr {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: crate::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: args
.into_iter()
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2281,6 +2281,8 @@ fn parse_map_access_expr() {
)]),
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token:
sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(
number("2").with_empty_span(),
Expand Down
5 changes: 5 additions & 0 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,8 @@ fn parse_create_table_with_primary_key() {
assert_eq!(
actual.args,
FunctionArguments::List(FunctionArgumentList {
closing_token:
sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(Identifier(
Ident::new(arg)
)),)],
Expand Down Expand Up @@ -849,6 +851,7 @@ fn parse_create_table_with_variant_default_expressions() {
name: ObjectName::from(vec![Ident::new("now")]),
uses_odbc_syntax: false,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
args: vec![],
duplicate_treatment: None,
clauses: vec![],
Expand All @@ -870,6 +873,7 @@ fn parse_create_table_with_variant_default_expressions() {
name: ObjectName::from(vec![Ident::new("now")]),
uses_odbc_syntax: false,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
args: vec![],
duplicate_treatment: None,
clauses: vec![],
Expand Down Expand Up @@ -899,6 +903,7 @@ fn parse_create_table_with_variant_default_expressions() {
name: ObjectName::from(vec![Ident::new("toString")]),
uses_odbc_syntax: false,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Identifier(Ident::new("c"))
))],
Expand Down
48 changes: 48 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,7 @@ fn parse_select_count_wildcard() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Wildcard)],
clauses: vec![],
Expand All @@ -1461,6 +1462,7 @@ fn parse_select_count_distinct() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: Some(DuplicateTreatment::Distinct),
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::UnaryOp {
op: UnaryOperator::Plus,
Expand Down Expand Up @@ -3569,6 +3571,7 @@ fn parse_select_having() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Wildcard)],
clauses: vec![],
Expand Down Expand Up @@ -3600,6 +3603,7 @@ fn parse_select_qualify() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![],
clauses: vec![],
Expand Down Expand Up @@ -4021,6 +4025,7 @@ fn parse_listagg() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: Some(DuplicateTreatment::Distinct),
args: vec![
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Identifier(Ident::new(
Expand Down Expand Up @@ -6378,6 +6383,7 @@ fn parse_named_argument_function() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![
FunctionArg::Named {
Expand Down Expand Up @@ -6418,6 +6424,7 @@ fn parse_named_argument_function_with_eq_operator() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![
FunctionArg::Named {
Expand Down Expand Up @@ -6493,6 +6500,7 @@ fn parse_window_functions() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![],
clauses: vec![],
Expand Down Expand Up @@ -6635,6 +6643,8 @@ fn test_parse_named_window() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token:
sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Expr::Identifier(Ident {
Expand Down Expand Up @@ -6670,6 +6680,8 @@ fn test_parse_named_window() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token:
sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Expr::Identifier(Ident {
Expand Down Expand Up @@ -11640,6 +11652,7 @@ fn parse_time_functions() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![],
clauses: vec![],
Expand Down Expand Up @@ -13407,6 +13420,7 @@ fn parse_call() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(
(Value::SingleQuotedString("a".to_string())).with_empty_span()
Expand Down Expand Up @@ -13926,6 +13940,8 @@ fn parse_map_access_expr() {
)]),
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token:
sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(
(number("2")).with_empty_span(),
Expand Down Expand Up @@ -14266,6 +14282,7 @@ fn test_selective_aggregation() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Expr::Identifier(Ident::new("name"))
Expand All @@ -14285,6 +14302,8 @@ fn test_selective_aggregation() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token:
sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Expr::Identifier(Ident::new("name"))
Expand Down Expand Up @@ -17078,6 +17097,7 @@ fn parse_composite_access_expr() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Expr::Identifier(Ident::new("a"))
Expand All @@ -17102,6 +17122,7 @@ fn parse_composite_access_expr() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Expr::Identifier(Ident::new("a"))
Expand All @@ -17128,6 +17149,7 @@ fn parse_composite_access_expr() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Expr::Identifier(Ident::new("a")),
Expand Down Expand Up @@ -22100,6 +22122,31 @@ fn parse_qualified_literal_prefix_requires_adjacent_period() {
}
}

#[test]
fn function_spans_include_closing_parentheses() {
for call in ["foo()", "foo(1)", "foo(bar())", "foo( /* comment */ )"] {
let sql = format!("SELECT {call}");
let statements = all_dialects()
.run_parser_method(&sql, |parser| parser.parse_statements())
.unwrap();
let Statement::Query(query) = &statements[0] else {
panic!("expected a query");
};
let SetExpr::Select(select) = query.body.as_ref() else {
panic!("expected a select");
};
let UnnamedExpr(Expr::Function(function)) = &select.projection[0] else {
panic!("expected a function");
};
assert_eq!(function.span().start, Location::new(1, 8), "{call}");
assert_eq!(
function.span().end,
Location::new(1, sql.len() as u64 + 1),
"{call}"
);
}
}

#[test]
fn parse_cache_table_options_without_flag() {
let dialects = all_dialects();
Expand Down Expand Up @@ -22322,6 +22369,7 @@ fn parse_delimited_identifiers() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![],
clauses: vec![],
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ fn test_duckdb_named_argument_function_with_assignment_operator() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![
FunctionArg::Named {
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,7 @@ fn parse_create_table_with_valid_options() {
parameters: FunctionArguments::None,
args: FunctionArguments::List(
FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![
FunctionArg::Unnamed(
Expand Down
5 changes: 5 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3401,6 +3401,8 @@ fn parse_create_indices_with_operator_classes() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token:
sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Identifier(
Expand Down Expand Up @@ -4360,6 +4362,7 @@ fn test_composite_value() {
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
closing_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Array(
Array {
Expand Down Expand Up @@ -6237,6 +6240,8 @@ fn parse_create_table_with_partition_by() {
assert_eq!("RANGE", f.name.to_string());
assert_eq!(
FunctionArguments::List(FunctionArgumentList {
closing_token:
sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
duplicate_treatment: None,
clauses: vec![],
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Expand Down
Loading
Loading