Skip to content

Commit

Permalink
Implement unparse IS_NULL to String and enhance the tests (apache#1…
Browse files Browse the repository at this point in the history
…0529)

* implement unparse is_null and add test

* format the code
  • Loading branch information
goldmedal authored and findepi committed Jul 16, 2024
1 parent fcf0ecf commit e330bff
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions datafusion/sql/src/unparser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,9 @@ impl Unparser<'_> {
Expr::ScalarVariable(_, _) => {
not_impl_err!("Unsupported Expr conversion: {expr:?}")
}
Expr::IsNull(_) => not_impl_err!("Unsupported Expr conversion: {expr:?}"),
Expr::IsNull(expr) => {
Ok(ast::Expr::IsNull(Box::new(self.expr_to_sql(expr)?)))
}
Expr::IsNotFalse(_) => not_impl_err!("Unsupported Expr conversion: {expr:?}"),
Expr::GetIndexedField(_) => {
not_impl_err!("Unsupported Expr conversion: {expr:?}")
Expand Down Expand Up @@ -863,7 +865,7 @@ mod tests {
use datafusion_expr::{
case, col, exists,
expr::{AggregateFunction, AggregateFunctionDefinition},
lit, not, not_exists, table_scan, wildcard, ColumnarValue, ScalarUDF,
lit, not, not_exists, table_scan, when, wildcard, ColumnarValue, ScalarUDF,
ScalarUDFImpl, Signature, Volatility, WindowFrame, WindowFunctionDefinition,
};

Expand Down Expand Up @@ -933,6 +935,14 @@ mod tests {
.otherwise(lit(ScalarValue::Null))?,
r#"CASE "a" WHEN 1 THEN true WHEN 0 THEN false ELSE NULL END"#,
),
(
when(col("a").is_null(), lit(true)).otherwise(lit(false))?,
r#"CASE WHEN "a" IS NULL THEN true ELSE false END"#,
),
(
when(col("a").is_not_null(), lit(true)).otherwise(lit(false))?,
r#"CASE WHEN "a" IS NOT NULL THEN true ELSE false END"#,
),
(
Expr::Cast(Cast {
expr: Box::new(col("a")),
Expand All @@ -959,6 +969,18 @@ mod tests {
ScalarUDF::new_from_impl(DummyUDF::new()).call(vec![col("a"), col("b")]),
r#"dummy_udf("a", "b")"#,
),
(
ScalarUDF::new_from_impl(DummyUDF::new())
.call(vec![col("a"), col("b")])
.is_null(),
r#"dummy_udf("a", "b") IS NULL"#,
),
(
ScalarUDF::new_from_impl(DummyUDF::new())
.call(vec![col("a"), col("b")])
.is_not_null(),
r#"dummy_udf("a", "b") IS NOT NULL"#,
),
(
Expr::Like(Like {
negated: true,
Expand Down Expand Up @@ -1081,6 +1103,7 @@ mod tests {
r#"COUNT(*) OVER (ORDER BY "a" DESC NULLS FIRST RANGE BETWEEN 6 PRECEDING AND 2 FOLLOWING)"#,
),
(col("a").is_not_null(), r#""a" IS NOT NULL"#),
(col("a").is_null(), r#""a" IS NULL"#),
(
(col("a") + col("b")).gt(lit(4)).is_true(),
r#"(("a" + "b") > 4) IS TRUE"#,
Expand Down

0 comments on commit e330bff

Please sign in to comment.