jayzhan211 commented on code in PR #27143:
URL: https://github.com/apache/datafusion/pull/27143#discussion_r4614688347


##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -1307,6 +1348,18 @@ impl PhysicalExpr for CaseExpr {
         }
     }
 
+    fn return_field(&self, input_schema: &Schema) -> Result<FieldRef> {

Review Comment:
   @james-willis thanks for tracking down #27139 and sharing the type rule! A 
cast between nested CASE levels is still exponential, and this PR makes it grow 
faster than `main`: at 8 levels one `return_field` call takes 842 µs vs 56 µs 
on `main`, and `ScalarFunctionExpr` makes that call for every batch (evaluating 
this CASE on 8,192 rows takes about 0.15 ms). `CastExpr::nullable` calls both 
the child's `nullable` and `return_field`, and `return_field` here calls 
`self.nullable` plus each branch's `return_field`, so each level repeats the 
one below about 2.6 times (2 times on `main`). Building the field from one 
visit of each branch, with the nullability taken from the branch fields as 
`to_field` now does, brings it to 7 µs. `EXPLAIN` at 16 levels drops from 21.9 
s (13.3 s on `main`) to 0.12 s, with the same plan:
   
   ```rs
   fn return_field(&self, input_schema: &Schema) -> Result<FieldRef> {
       Ok(Arc::new(
           self.unnamed_field(input_schema)?
               .with_name(format!("{self}")),
       ))
   }
   ```
   
   <details>
   <summary>Full diff (replaces <code>return_metadata</code>; 
<code>nullable_from</code> is the current <code>nullable</code> body, moved 
unchanged with its comments, except where noted), the shape and 
timings</summary>
   
   ```diff
   -use datafusion_common::metadata::{FieldMetadata, coerced_fields_metadata};
   +use datafusion_common::metadata::coerced_fields_metadata;
   @@ impl CaseExpr
   -    /// The metadata of [`PhysicalExpr::return_field`]. ...
   -    fn return_metadata(&self, input_schema: &Schema) -> 
Result<FieldMetadata> {
   +    /// [`PhysicalExpr::return_field`] without the name, from one visit of 
each value
   +    /// branch. A nested CASE branch is visited the same way, since its 
name would
   +    /// format its whole subtree again at every level.
   +    fn unnamed_field(&self, input_schema: &Schema) -> Result<Field> {
            let fields = self
                ...
                .map(|expr| match expr.downcast_ref::<CaseExpr>() {
   -                Some(case) => { ... case.return_metadata(input_schema)? ... 
}
   +                Some(case) => 
case.unnamed_field(input_schema).map(Arc::new),
                    None => expr.return_field(input_schema),
                })
                .collect::<Result<Vec<_>>>()?;
   -        coerced_fields_metadata(fields.iter().map(|f| f.as_ref()))
   +        let (then_fields, else_field) = 
fields.split_at(self.body.when_then_expr.len());
   +        let data_type = then_fields
   +            .iter()
   +            .map(|f| f.data_type())
   +            .find(|t| !t.is_null())
   +            .or_else(|| else_field.first().map(|f| f.data_type()))
   +            .unwrap_or(&DataType::Null)
   +            .clone();
   +        let nullable =
   +            self.nullable_from(input_schema, |i| 
Ok(fields[i].is_nullable()))?;
   +        let metadata = coerced_fields_metadata(fields.iter().map(|f| 
f.as_ref()))?;
   +        Ok(metadata.add_to_field(Field::new("", data_type, nullable)))
   +    }
   +
   +    /// The nullability of this CASE, given the nullability of each value 
branch:
   +    /// the THEN expressions in order, then the ELSE.
   +    fn nullable_from(
   +        &self,
   +        input_schema: &Schema,
   +        mut branch_nullable: impl FnMut(usize) -> Result<bool>,
   +    ) -> Result<bool> {
   +        // the current `nullable` body, with `.enumerate()` on the THEN 
iterator,
   +        // `t.nullable(input_schema)` -> `branch_nullable(i)`, and the ELSE 
arm:
   +        } else if self.body.else_expr.is_some() {
   +            branch_nullable(self.body.when_then_expr.len())
   +        } else {
   +            Ok(true)
   +        }
   +    }
   @@ impl PhysicalExpr for CaseExpr
        fn nullable(&self, input_schema: &Schema) -> Result<bool> {
   -        (body moved to `nullable_from`)
   +        self.nullable_from(input_schema, |i| match 
self.body.when_then_expr.get(i) {
   +            Some((_, then)) => then.nullable(input_schema),
   +            None => self
   +                .body
   +                .else_expr
   +                .as_ref()
   +                .map_or(Ok(true), |e| e.nullable(input_schema)),
   +        })
        }
   ```
   
   The shape, built with `case` and `cast` in a release-mode test, alternating 
`Utf8` and `LargeUtf8`:
   
   ```sql
   CASE WHEN a > k THEN arrow_cast('x', T_k) ELSE arrow_cast(<inner>, T_k) END
   ```
   
   | depth | `main` | this PR | with the change |
   |---|---|---|---|
   | 4 | 3.4 µs | 12 µs | 2.5 µs |
   | 8 | 56 µs | 842 µs | 7.3 µs |
   | 16 | 16 ms | 1.8 s | 24 µs |
   
   CASE nested directly in its ELSE stays as fast as now (2.5 µs vs 5.6 µs at 
depth 8). With the change, the physical-expr lib tests, the full sqllogictest 
suite and `core_integration` `sql::union_schema` pass.
   </details>
   



-- 
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]

Reply via email to