Skip to content

Commit eddec8e

Browse files
authored
Implement unparse IS_NULL to String and enhance the tests (#10529)
* implement unparse is_null and add test * format the code
1 parent c312ffe commit eddec8e

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

datafusion/sql/src/unparser/expr.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,9 @@ impl Unparser<'_> {
391391
Expr::ScalarVariable(_, _) => {
392392
not_impl_err!("Unsupported Expr conversion: {expr:?}")
393393
}
394-
Expr::IsNull(_) => not_impl_err!("Unsupported Expr conversion: {expr:?}"),
394+
Expr::IsNull(expr) => {
395+
Ok(ast::Expr::IsNull(Box::new(self.expr_to_sql(expr)?)))
396+
}
395397
Expr::IsNotFalse(_) => not_impl_err!("Unsupported Expr conversion: {expr:?}"),
396398
Expr::GetIndexedField(_) => {
397399
not_impl_err!("Unsupported Expr conversion: {expr:?}")
@@ -863,7 +865,7 @@ mod tests {
863865
use datafusion_expr::{
864866
case, col, exists,
865867
expr::{AggregateFunction, AggregateFunctionDefinition},
866-
lit, not, not_exists, table_scan, wildcard, ColumnarValue, ScalarUDF,
868+
lit, not, not_exists, table_scan, when, wildcard, ColumnarValue, ScalarUDF,
867869
ScalarUDFImpl, Signature, Volatility, WindowFrame, WindowFunctionDefinition,
868870
};
869871

@@ -933,6 +935,14 @@ mod tests {
933935
.otherwise(lit(ScalarValue::Null))?,
934936
r#"CASE "a" WHEN 1 THEN true WHEN 0 THEN false ELSE NULL END"#,
935937
),
938+
(
939+
when(col("a").is_null(), lit(true)).otherwise(lit(false))?,
940+
r#"CASE WHEN "a" IS NULL THEN true ELSE false END"#,
941+
),
942+
(
943+
when(col("a").is_not_null(), lit(true)).otherwise(lit(false))?,
944+
r#"CASE WHEN "a" IS NOT NULL THEN true ELSE false END"#,
945+
),
936946
(
937947
Expr::Cast(Cast {
938948
expr: Box::new(col("a")),
@@ -959,6 +969,18 @@ mod tests {
959969
ScalarUDF::new_from_impl(DummyUDF::new()).call(vec![col("a"), col("b")]),
960970
r#"dummy_udf("a", "b")"#,
961971
),
972+
(
973+
ScalarUDF::new_from_impl(DummyUDF::new())
974+
.call(vec![col("a"), col("b")])
975+
.is_null(),
976+
r#"dummy_udf("a", "b") IS NULL"#,
977+
),
978+
(
979+
ScalarUDF::new_from_impl(DummyUDF::new())
980+
.call(vec![col("a"), col("b")])
981+
.is_not_null(),
982+
r#"dummy_udf("a", "b") IS NOT NULL"#,
983+
),
962984
(
963985
Expr::Like(Like {
964986
negated: true,
@@ -1081,6 +1103,7 @@ mod tests {
10811103
r#"COUNT(*) OVER (ORDER BY "a" DESC NULLS FIRST RANGE BETWEEN 6 PRECEDING AND 2 FOLLOWING)"#,
10821104
),
10831105
(col("a").is_not_null(), r#""a" IS NOT NULL"#),
1106+
(col("a").is_null(), r#""a" IS NULL"#),
10841107
(
10851108
(col("a") + col("b")).gt(lit(4)).is_true(),
10861109
r#"(("a" + "b") > 4) IS TRUE"#,

0 commit comments

Comments
 (0)