This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 349c586ea7 Expr to sql : Case (#9798)
349c586ea7 is described below
commit 349c586ea7c680c802f36c750e5f1e2dc7beb65b
Author: Junhao Liu <[email protected]>
AuthorDate: Mon Mar 25 20:49:54 2024 -0600
Expr to sql : Case (#9798)
---
datafusion/sql/src/unparser/expr.rs | 51 +++++++++++++++++++++++++++++++------
1 file changed, 43 insertions(+), 8 deletions(-)
diff --git a/datafusion/sql/src/unparser/expr.rs
b/datafusion/sql/src/unparser/expr.rs
index d007d4a843..49a940060b 100644
--- a/datafusion/sql/src/unparser/expr.rs
+++ b/datafusion/sql/src/unparser/expr.rs
@@ -113,10 +113,38 @@ impl Unparser<'_> {
}
Expr::Case(Case {
expr,
- when_then_expr: _,
- else_expr: _,
+ when_then_expr,
+ else_expr,
}) => {
- not_impl_err!("Unsupported expression: {expr:?}")
+ let conditions = when_then_expr
+ .iter()
+ .map(|(w, _)| self.expr_to_sql(w))
+ .collect::<Result<Vec<_>>>()?;
+ let results = when_then_expr
+ .iter()
+ .map(|(_, t)| self.expr_to_sql(t))
+ .collect::<Result<Vec<_>>>()?;
+ let operand = match expr.as_ref() {
+ Some(e) => match self.expr_to_sql(e) {
+ Ok(sql_expr) => Some(Box::new(sql_expr)),
+ Err(_) => None,
+ },
+ None => None,
+ };
+ let else_result = match else_expr.as_ref() {
+ Some(e) => match self.expr_to_sql(e) {
+ Ok(sql_expr) => Some(Box::new(sql_expr)),
+ Err(_) => None,
+ },
+ None => None,
+ };
+
+ Ok(ast::Expr::Case {
+ operand,
+ conditions,
+ results,
+ else_result,
+ })
}
Expr::Cast(Cast { expr, data_type }) => {
let inner_expr = self.expr_to_sql(expr)?;
@@ -565,7 +593,7 @@ mod tests {
use datafusion_common::TableReference;
use datafusion_expr::{
- col, expr::AggregateFunction, lit, ColumnarValue, ScalarUDF,
ScalarUDFImpl,
+ case, col, expr::AggregateFunction, lit, ColumnarValue, ScalarUDF,
ScalarUDFImpl,
Signature, Volatility,
};
@@ -622,6 +650,13 @@ mod tests {
.gt(lit(4)),
r#"("a"."b"."c" > 4)"#,
),
+ (
+ case(col("a"))
+ .when(lit(1), lit(true))
+ .when(lit(0), lit(false))
+ .otherwise(lit(ScalarValue::Null))?,
+ r#"CASE "a" WHEN 1 THEN true WHEN 0 THEN false ELSE NULL END"#,
+ ),
(
Expr::Cast(Cast {
expr: Box::new(col("a")),
@@ -698,17 +733,17 @@ mod tests {
}),
"COUNT(DISTINCT *)",
),
- (Expr::IsNotNull(Box::new(col("a"))), r#""a" IS NOT NULL"#),
+ (col("a").is_not_null(), r#""a" IS NOT NULL"#),
(
- Expr::IsTrue(Box::new((col("a") + col("b")).gt(lit(4)))),
+ (col("a") + col("b")).gt(lit(4)).is_true(),
r#"(("a" + "b") > 4) IS TRUE"#,
),
(
- Expr::IsFalse(Box::new((col("a") + col("b")).gt(lit(4)))),
+ (col("a") + col("b")).gt(lit(4)).is_false(),
r#"(("a" + "b") > 4) IS FALSE"#,
),
(
- Expr::IsUnknown(Box::new((col("a") + col("b")).gt(lit(4)))),
+ (col("a") + col("b")).gt(lit(4)).is_unknown(),
r#"(("a" + "b") > 4) IS UNKNOWN"#,
),
];