james-willis opened a new pull request, #7763:
URL: https://github.com/apache/datafusion-comet/pull/7763

   ## Which issue does this PR close?
   
   No issue is filed for this yet.
   
   ## Rationale for this change
   
   `CaseWhenExpr` and `IfExpr` implement `data_type` and `nullable` but not 
`return_field`, so the `PhysicalExpr` default builds their output field from 
the type alone, and the metadata of the branch fields is lost. That includes 
Arrow extension types, such as `ARROW:extension:name = geoarrow.wkb` on a WKB 
geometry column or `arrow.json` on a JSON column: a `CASE WHEN`, `IF` or 
`COALESCE` over such a column hands the JVM a plain `Binary` column, although 
the FFI export itself keeps field metadata (`move_to_spark`). The case that 
found this is a geometry UDT mapped to a `geoarrow.wkb` extension type in a 
downstream build of Comet.
   
   DataFusion fixed the same for its `coalesce` in apache/datafusion#24865 and 
for its `CaseExpr` in apache/datafusion#27143. Since #6350 Comet evaluates 
Spark's `CASE WHEN`, `IF` and `COALESCE` through its own `CaseWhenExpr`, so 
those fixes do not reach Comet with a DataFusion upgrade.
   
   ## What changes are included in this PR?
   
   - `CaseWhenExpr::return_field` keeps the type and nullability that 
`data_type` and `nullable` report, and adds the metadata merged from the branch 
values' fields with the policy of `coerced_fields_metadata`: the earlier branch 
wins for each key, an extension name is kept together with the extension 
parameters of the branch that supplied it, a branch without metadata or an 
untyped `NULL` does not drop another branch's metadata, and differing extension 
names are a planning error, as they are for DataFusion's `coalesce` and `CASE`.
   - `IfExpr::return_field` does the same with its own type and nullability.
   - `merge_branch_metadata` is a copy of DataFusion's 
`coerced_fields_metadata`, which is not in DataFusion 55. It can be replaced by 
the upstream function once Comet upgrades past apache/datafusion#27143.
   
   ## How are these changes tested?
   
   New unit tests in `case_when.rs` and `if_expr.rs` cover metadata precedence 
and the pairing of the extension keys; no ELSE, an untyped `NULL`, a typed 
`NULL` and a branch without metadata; a `NULL` THEN; an `IF` nested in the 
ELSE; the error for differing extension names; and the output schema of a 
`ProjectionExec` over both expressions, which is what reaches the JVM. The 
field's type, nullability and name are checked against `data_type`, `nullable` 
and `Display`. Each metadata assertion fails without the change.
   
   `cargo test -p datafusion-comet-spark-expr` and `cargo clippy --all-targets 
--workspace -- -D warnings` pass locally.
   
   There is no Spark SQL test because no Spark type that Comet ships carries 
Arrow extension metadata.
   


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