-
Notifications
You must be signed in to change notification settings - Fork 605
Databricks: support for lambda functions #1257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use sqlparser::ast::*; | ||
use sqlparser::dialect::DatabricksDialect; | ||
use sqlparser::parser::ParserError; | ||
use test_utils::*; | ||
|
||
#[macro_use] | ||
|
@@ -28,3 +29,98 @@ fn test_databricks_identifiers() { | |
SelectItem::UnnamedExpr(Expr::Value(Value::DoubleQuotedString("Ä".to_owned()))) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_databricks_exists() { | ||
// exists is a function in databricks | ||
assert_eq!( | ||
databricks().verified_expr("exists(array(1, 2, 3), x -> x IS NULL)"), | ||
call( | ||
"exists", | ||
[ | ||
call( | ||
"array", | ||
[ | ||
Expr::Value(number("1")), | ||
Expr::Value(number("2")), | ||
Expr::Value(number("3")) | ||
] | ||
), | ||
Expr::Lambda(LambdaFunction { | ||
params: OneOrManyWithParens::One(Ident::new("x")), | ||
body: Box::new(Expr::IsNull(Box::new(Expr::Identifier(Ident::new("x"))))) | ||
}) | ||
] | ||
), | ||
); | ||
|
||
let res = databricks().parse_sql_statements("SELECT EXISTS ("); | ||
assert_eq!( | ||
// TODO: improve this error message... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. was the todo meant for this PR or as a general improvement/followup with error reporting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the latter |
||
ParserError::ParserError("Expected an expression:, found: EOF".to_string()), | ||
res.unwrap_err(), | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_databricks_lambdas() { | ||
#[rustfmt::skip] | ||
let sql = concat!( | ||
"SELECT array_sort(array('Hello', 'World'), ", | ||
"(p1, p2) -> CASE WHEN p1 = p2 THEN 0 ", | ||
"WHEN reverse(p1) < reverse(p2) THEN -1 ", | ||
"ELSE 1 END)", | ||
); | ||
pretty_assertions::assert_eq!( | ||
SelectItem::UnnamedExpr(call( | ||
"array_sort", | ||
[ | ||
call( | ||
"array", | ||
[ | ||
Expr::Value(Value::SingleQuotedString("Hello".to_owned())), | ||
Expr::Value(Value::SingleQuotedString("World".to_owned())) | ||
] | ||
), | ||
Expr::Lambda(LambdaFunction { | ||
params: OneOrManyWithParens::Many(vec![Ident::new("p1"), Ident::new("p2")]), | ||
body: Box::new(Expr::Case { | ||
operand: None, | ||
conditions: vec![ | ||
Expr::BinaryOp { | ||
left: Box::new(Expr::Identifier(Ident::new("p1"))), | ||
op: BinaryOperator::Eq, | ||
right: Box::new(Expr::Identifier(Ident::new("p2"))) | ||
}, | ||
Expr::BinaryOp { | ||
left: Box::new(call( | ||
"reverse", | ||
[Expr::Identifier(Ident::new("p1"))] | ||
)), | ||
op: BinaryOperator::Lt, | ||
right: Box::new(call( | ||
"reverse", | ||
[Expr::Identifier(Ident::new("p2"))] | ||
)) | ||
} | ||
], | ||
results: vec![ | ||
Expr::Value(number("0")), | ||
Expr::UnaryOp { | ||
op: UnaryOperator::Minus, | ||
expr: Box::new(Expr::Value(number("1"))) | ||
} | ||
], | ||
else_result: Some(Box::new(Expr::Value(number("1")))) | ||
}) | ||
}) | ||
] | ||
)), | ||
databricks().verified_only_select(sql).projection[0] | ||
); | ||
|
||
databricks().verified_expr( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pretty neat! |
||
"map_zip_with(map(1, 'a', 2, 'b'), map(1, 'x', 2, 'y'), (k, v1, v2) -> concat(v1, v2))", | ||
); | ||
databricks().verified_expr("transform(array(1, 2, 3), x -> x + 1)"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I was unsure of here is whether to enable this for the generic dialect. Our policy is to enable everything for generic, but this syntax conflicts with the
->
operator from dialects like Postgres. So supporting this would be a breaking change of somewhat dubious benefit, since we both gain and lose a feature in generic. Thoughts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense to not support via GenericDialect when there is a conflict
🤔 that might be a good thing to add to the policy https://github.com/sqlparser-rs/sqlparser-rs?tab=readme-ov-file#new-syntax