Skip to content

Commit d981b09

Browse files
feat: support = operator in function args (#1128)
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent 6245231 commit d981b09

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

src/ast/mod.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4287,18 +4287,46 @@ impl fmt::Display for FunctionArgExpr {
42874287
}
42884288
}
42894289

4290+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4291+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4292+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4293+
/// Operator used to separate function arguments
4294+
pub enum FunctionArgOperator {
4295+
/// function(arg1 = value1)
4296+
Equals,
4297+
/// function(arg1 => value1)
4298+
RightArrow,
4299+
}
4300+
4301+
impl fmt::Display for FunctionArgOperator {
4302+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4303+
match self {
4304+
FunctionArgOperator::Equals => f.write_str("="),
4305+
FunctionArgOperator::RightArrow => f.write_str("=>"),
4306+
}
4307+
}
4308+
}
4309+
42904310
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
42914311
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
42924312
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
42934313
pub enum FunctionArg {
4294-
Named { name: Ident, arg: FunctionArgExpr },
4314+
Named {
4315+
name: Ident,
4316+
arg: FunctionArgExpr,
4317+
operator: FunctionArgOperator,
4318+
},
42954319
Unnamed(FunctionArgExpr),
42964320
}
42974321

42984322
impl fmt::Display for FunctionArg {
42994323
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43004324
match self {
4301-
FunctionArg::Named { name, arg } => write!(f, "{name} => {arg}"),
4325+
FunctionArg::Named {
4326+
name,
4327+
arg,
4328+
operator,
4329+
} => write!(f, "{name} {operator} {arg}"),
43024330
FunctionArg::Unnamed(unnamed_arg) => write!(f, "{unnamed_arg}"),
43034331
}
43044332
}

src/parser/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8106,7 +8106,22 @@ impl<'a> Parser<'a> {
81068106
self.expect_token(&Token::RArrow)?;
81078107
let arg = self.parse_wildcard_expr()?.into();
81088108

8109-
Ok(FunctionArg::Named { name, arg })
8109+
Ok(FunctionArg::Named {
8110+
name,
8111+
arg,
8112+
operator: FunctionArgOperator::RightArrow,
8113+
})
8114+
} else if self.peek_nth_token(1) == Token::Eq {
8115+
let name = self.parse_identifier(false)?;
8116+
8117+
self.expect_token(&Token::Eq)?;
8118+
let arg = self.parse_wildcard_expr()?.into();
8119+
8120+
Ok(FunctionArg::Named {
8121+
name,
8122+
arg,
8123+
operator: FunctionArgOperator::Equals,
8124+
})
81108125
} else {
81118126
Ok(FunctionArg::Unnamed(self.parse_wildcard_expr()?.into()))
81128127
}

tests/sqlparser_common.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3971,12 +3971,48 @@ fn parse_named_argument_function() {
39713971
arg: FunctionArgExpr::Expr(Expr::Value(Value::SingleQuotedString(
39723972
"1".to_owned()
39733973
))),
3974+
operator: FunctionArgOperator::RightArrow
39743975
},
39753976
FunctionArg::Named {
39763977
name: Ident::new("b"),
39773978
arg: FunctionArgExpr::Expr(Expr::Value(Value::SingleQuotedString(
39783979
"2".to_owned()
39793980
))),
3981+
operator: FunctionArgOperator::RightArrow
3982+
},
3983+
],
3984+
null_treatment: None,
3985+
filter: None,
3986+
over: None,
3987+
distinct: false,
3988+
special: false,
3989+
order_by: vec![],
3990+
}),
3991+
expr_from_projection(only(&select.projection))
3992+
);
3993+
}
3994+
3995+
#[test]
3996+
fn parse_named_argument_function_with_eq_operator() {
3997+
let sql = "SELECT FUN(a = '1', b = '2') FROM foo";
3998+
let select = verified_only_select(sql);
3999+
assert_eq!(
4000+
&Expr::Function(Function {
4001+
name: ObjectName(vec![Ident::new("FUN")]),
4002+
args: vec![
4003+
FunctionArg::Named {
4004+
name: Ident::new("a"),
4005+
arg: FunctionArgExpr::Expr(Expr::Value(Value::SingleQuotedString(
4006+
"1".to_owned()
4007+
))),
4008+
operator: FunctionArgOperator::Equals
4009+
},
4010+
FunctionArg::Named {
4011+
name: Ident::new("b"),
4012+
arg: FunctionArgExpr::Expr(Expr::Value(Value::SingleQuotedString(
4013+
"2".to_owned()
4014+
))),
4015+
operator: FunctionArgOperator::Equals
39804016
},
39814017
],
39824018
null_treatment: None,

0 commit comments

Comments
 (0)