andygrove commented on code in PR #11553:
URL: https://github.com/apache/datafusion/pull/11553#discussion_r1685436569
##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -344,6 +355,38 @@ impl CaseExpr {
internal_err!("predicate did not evaluate to an array")
}
}
+
+ fn scalar_or_scalar(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
+ let return_type = self.data_type(&batch.schema())?;
+
+ // evaluate when expression
+ let when_value = self.when_then_expr[0].0.evaluate(batch)?;
+ let when_value = when_value.into_array(batch.num_rows())?;
+ let when_value = as_boolean_array(&when_value).map_err(|e| {
+ DataFusionError::Context(
+ "WHEN expression did not return a BooleanArray".to_string(),
+ Box::new(e),
+ )
+ })?;
+
+ // Treat 'NULL' as false value
+ let when_value = match when_value.null_count() {
+ 0 => Cow::Borrowed(when_value),
+ _ => Cow::Owned(prep_null_mask_filter(when_value)),
+ };
+
+ // evaluate then_value
+ let then_value = self.when_then_expr[0].1.evaluate(batch)?;
+ let then_value = Scalar::new(then_value.into_array(1)?);
+
+ // keep `else_expr`'s data type and return type consistent
+ let e = self.else_expr.as_ref().unwrap();
+ let expr = try_cast(Arc::clone(e), &batch.schema(),
return_type.clone())
+ .unwrap_or_else(|_| Arc::clone(e));
+ let else_ = Scalar::new(expr.evaluate(batch)?.into_array(1)?);
+
+ Ok(ColumnarValue::Array(zip(&when_value, &then_value, &else_)?))
Review Comment:
I wonder if it would make sense (in a separate PR) to produce a dictionary
array in this case since it will only even contain two distinct values?
:thinking:
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]