doki23 commented on a change in pull request #2068:
URL: https://github.com/apache/arrow-datafusion/pull/2068#discussion_r834894674
##########
File path: datafusion-physical-expr/src/expressions/case.rs
##########
@@ -323,39 +323,46 @@ impl CaseExpr {
let base_value = expr.evaluate(batch)?;
let base_type = expr.data_type(&batch.schema())?;
let base_value = base_value.into_array(batch.num_rows());
+ let base_nulls = is_null(base_value.as_ref())?;
- // start with the else condition, or nulls
- let mut current_value: Option<ArrayRef> = if let Some(e) =
&self.else_expr {
- // keep `else_expr`'s data type and return type consistent
- let expr = try_cast(e.clone(), &*batch.schema(),
return_type.clone())
- .unwrap_or_else(|_| e.clone());
- Some(expr.evaluate(batch)?.into_array(batch.num_rows()))
- } else {
- Some(new_null_array(&return_type, batch.num_rows()))
- };
-
- // walk backwards through the when/then expressions
- for i in (0..self.when_then_expr.len()).rev() {
+ // start with nulls as default output
+ let mut current_value = new_null_array(&return_type, batch.num_rows());
+ // We only consider non-null values while comparing with whens
+ let mut remainder = not(&base_nulls)?;
+ for i in 0..self.when_then_expr.len() {
let i = i as usize;
Review comment:
```suggestion
```
##########
File path: datafusion-physical-expr/src/expressions/case.rs
##########
@@ -368,44 +375,54 @@ impl CaseExpr {
fn case_when_no_expr(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
let return_type = self.when_then_expr[0].1.data_type(&batch.schema())?;
- // start with the else condition, or nulls
- let mut current_value: Option<ArrayRef> = if let Some(e) =
&self.else_expr {
- let expr = try_cast(e.clone(), &*batch.schema(),
return_type.clone())
- .unwrap_or_else(|_| e.clone());
- Some(expr.evaluate(batch)?.into_array(batch.num_rows()))
- } else {
- Some(new_null_array(&return_type, batch.num_rows()))
- };
-
- // walk backwards through the when/then expressions
- for i in (0..self.when_then_expr.len()).rev() {
+ // start with nulls as default output
+ let mut current_value = new_null_array(&return_type, batch.num_rows());
+ let mut remainder = BooleanArray::from(vec![true; batch.num_rows()]);
+ for i in 0..self.when_then_expr.len() {
let i = i as usize;
Review comment:
```suggestion
```
--
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]