Support named arguments in function invocations (#250)

This commit supports functions with argument names.

the format is :
"Select some_function( a => exp, b => exp2 .. ) FROM table1
OR
"select * from table(function(a => exp)) f;"

see:
https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#named-argument-assignment-token
or the motivating example from snowflake:
https://docs.snowflake.com/en/sql-reference/functions/flatten.html
This commit is contained in:
eyalleshem 2020-08-02 08:04:55 +03:00 committed by GitHub
parent 580e4b1d64
commit 1cc3bf4099
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 10 deletions

View file

@ -325,7 +325,7 @@ fn parse_select_count_wildcard() {
assert_eq!(
&Expr::Function(Function {
name: ObjectName(vec![Ident::new("COUNT")]),
args: vec![Expr::Wildcard],
args: vec![FunctionArg::Unnamed(Expr::Wildcard)],
over: None,
distinct: false,
}),
@ -340,10 +340,10 @@ fn parse_select_count_distinct() {
assert_eq!(
&Expr::Function(Function {
name: ObjectName(vec![Ident::new("COUNT")]),
args: vec![Expr::UnaryOp {
args: vec![FunctionArg::Unnamed(Expr::UnaryOp {
op: UnaryOperator::Plus,
expr: Box::new(Expr::Identifier(Ident::new("x")))
}],
})],
over: None,
distinct: true,
}),
@ -883,7 +883,7 @@ fn parse_select_having() {
Some(Expr::BinaryOp {
left: Box::new(Expr::Function(Function {
name: ObjectName(vec![Ident::new("COUNT")]),
args: vec![Expr::Wildcard],
args: vec![FunctionArg::Unnamed(Expr::Wildcard)],
over: None,
distinct: false
})),
@ -1589,7 +1589,32 @@ fn parse_scalar_function_in_projection() {
assert_eq!(
&Expr::Function(Function {
name: ObjectName(vec![Ident::new("sqrt")]),
args: vec![Expr::Identifier(Ident::new("id"))],
args: vec![FunctionArg::Unnamed(Expr::Identifier(Ident::new("id")))],
over: None,
distinct: false,
}),
expr_from_projection(only(&select.projection))
);
}
#[test]
fn parse_named_argument_function() {
let sql = "SELECT FUN(a => '1', b => '2') FROM foo";
let select = verified_only_select(sql);
assert_eq!(
&Expr::Function(Function {
name: ObjectName(vec![Ident::new("FUN")]),
args: vec![
FunctionArg::Named {
name: Ident::new("a"),
arg: Expr::Value(Value::SingleQuotedString("1".to_owned()))
},
FunctionArg::Named {
name: Ident::new("b"),
arg: Expr::Value(Value::SingleQuotedString("2".to_owned()))
},
],
over: None,
distinct: false,
}),