|
1 | 1 | use sqlparser::ast::*;
|
2 | 2 | use sqlparser::dialect::DatabricksDialect;
|
| 3 | +use sqlparser::parser::ParserError; |
3 | 4 | use test_utils::*;
|
4 | 5 |
|
5 | 6 | #[macro_use]
|
@@ -28,3 +29,98 @@ fn test_databricks_identifiers() {
|
28 | 29 | SelectItem::UnnamedExpr(Expr::Value(Value::DoubleQuotedString("Ä".to_owned())))
|
29 | 30 | );
|
30 | 31 | }
|
| 32 | + |
| 33 | +#[test] |
| 34 | +fn test_databricks_exists() { |
| 35 | + // exists is a function in databricks |
| 36 | + assert_eq!( |
| 37 | + databricks().verified_expr("exists(array(1, 2, 3), x -> x IS NULL)"), |
| 38 | + call( |
| 39 | + "exists", |
| 40 | + [ |
| 41 | + call( |
| 42 | + "array", |
| 43 | + [ |
| 44 | + Expr::Value(number("1")), |
| 45 | + Expr::Value(number("2")), |
| 46 | + Expr::Value(number("3")) |
| 47 | + ] |
| 48 | + ), |
| 49 | + Expr::Lambda(LambdaFunction { |
| 50 | + params: OneOrManyWithParens::One(Ident::new("x")), |
| 51 | + body: Box::new(Expr::IsNull(Box::new(Expr::Identifier(Ident::new("x"))))) |
| 52 | + }) |
| 53 | + ] |
| 54 | + ), |
| 55 | + ); |
| 56 | + |
| 57 | + let res = databricks().parse_sql_statements("SELECT EXISTS ("); |
| 58 | + assert_eq!( |
| 59 | + // TODO: improve this error message... |
| 60 | + ParserError::ParserError("Expected an expression:, found: EOF".to_string()), |
| 61 | + res.unwrap_err(), |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +#[test] |
| 66 | +fn test_databricks_lambdas() { |
| 67 | + #[rustfmt::skip] |
| 68 | + let sql = concat!( |
| 69 | + "SELECT array_sort(array('Hello', 'World'), ", |
| 70 | + "(p1, p2) -> CASE WHEN p1 = p2 THEN 0 ", |
| 71 | + "WHEN reverse(p1) < reverse(p2) THEN -1 ", |
| 72 | + "ELSE 1 END)", |
| 73 | + ); |
| 74 | + pretty_assertions::assert_eq!( |
| 75 | + SelectItem::UnnamedExpr(call( |
| 76 | + "array_sort", |
| 77 | + [ |
| 78 | + call( |
| 79 | + "array", |
| 80 | + [ |
| 81 | + Expr::Value(Value::SingleQuotedString("Hello".to_owned())), |
| 82 | + Expr::Value(Value::SingleQuotedString("World".to_owned())) |
| 83 | + ] |
| 84 | + ), |
| 85 | + Expr::Lambda(LambdaFunction { |
| 86 | + params: OneOrManyWithParens::Many(vec![Ident::new("p1"), Ident::new("p2")]), |
| 87 | + body: Box::new(Expr::Case { |
| 88 | + operand: None, |
| 89 | + conditions: vec![ |
| 90 | + Expr::BinaryOp { |
| 91 | + left: Box::new(Expr::Identifier(Ident::new("p1"))), |
| 92 | + op: BinaryOperator::Eq, |
| 93 | + right: Box::new(Expr::Identifier(Ident::new("p2"))) |
| 94 | + }, |
| 95 | + Expr::BinaryOp { |
| 96 | + left: Box::new(call( |
| 97 | + "reverse", |
| 98 | + [Expr::Identifier(Ident::new("p1"))] |
| 99 | + )), |
| 100 | + op: BinaryOperator::Lt, |
| 101 | + right: Box::new(call( |
| 102 | + "reverse", |
| 103 | + [Expr::Identifier(Ident::new("p2"))] |
| 104 | + )) |
| 105 | + } |
| 106 | + ], |
| 107 | + results: vec![ |
| 108 | + Expr::Value(number("0")), |
| 109 | + Expr::UnaryOp { |
| 110 | + op: UnaryOperator::Minus, |
| 111 | + expr: Box::new(Expr::Value(number("1"))) |
| 112 | + } |
| 113 | + ], |
| 114 | + else_result: Some(Box::new(Expr::Value(number("1")))) |
| 115 | + }) |
| 116 | + }) |
| 117 | + ] |
| 118 | + )), |
| 119 | + databricks().verified_only_select(sql).projection[0] |
| 120 | + ); |
| 121 | + |
| 122 | + databricks().verified_expr( |
| 123 | + "map_zip_with(map(1, 'a', 2, 'b'), map(1, 'x', 2, 'y'), (k, v1, v2) -> concat(v1, v2))", |
| 124 | + ); |
| 125 | + databricks().verified_expr("transform(array(1, 2, 3), x -> x + 1)"); |
| 126 | +} |
0 commit comments