Skip to content

Apache DataFusion SQL Query Engine

Add booleans support to the CASE statement (#1161)

Carlos2b002e41601a
diff --git a/datafusion/src/physical_plan/expressions/case.rs b/datafusion/src/physical_plan/expressions/case.rs
index a46522d69d..f577d6c0ea 100644
--- a/datafusion/src/physical_plan/expressions/case.rs
+++ b/datafusion/src/physical_plan/expressions/case.rs
@@ -227,6 +227,13 @@ fn if_then_else(
true_values,
false_values
),
+ DataType::Boolean => if_then_else!(
+ array::BooleanBuilder,
+ array::BooleanArray,
+ bools,
+ true_values,
+ false_values
+ ),
other => Err(DataFusionError::Execution(format!(
"CASE does not support '{:?}'",
other
diff --git a/datafusion/tests/sql.rs b/datafusion/tests/sql.rs
index 283033bcde..67270c5cfb 100644
--- a/datafusion/tests/sql.rs
+++ b/datafusion/tests/sql.rs
@@ -5200,3 +5200,13 @@ async fn union_all_with_aggregate() -> Result<()> {
assert_eq!(expected, actual);
Ok(())
}
+
+#[tokio::test]
+async fn case_with_bool_type_result() -> Result<()> {
+ let mut ctx = ExecutionContext::new();
+ let sql = "select case when 'cpu' != 'cpu' then true else false end";
+ let actual = execute(&mut ctx, sql).await;
+ let expected = vec![vec!["false"]];
+ assert_eq!(expected, actual);
+ Ok(())
+}