james-willis opened a new issue, #7768:
URL: https://github.com/apache/datafusion-comet/issues/7768

   ### Describe the bug
   
   `CaseWhenExpr` and `IfExpr` (`native/spark-expr/src/conditional_funcs/`) 
implement `data_type` and `nullable` but not `return_field`. The `PhysicalExpr` 
default builds the output `Field` from the type alone, so the metadata on the 
branch fields is lost.
   
   That includes Arrow extension types. A `CASE WHEN`, `IF` or `COALESCE` over 
a column tagged `ARROW:extension:name = geoarrow.wkb` (a WKB geometry) or 
`arrow.json` returns a plain `Binary` column, and the JVM receives an untagged 
column where it expects the extension type. The FFI export keeps field metadata 
(`move_to_spark` has a test for it), so the loss is in the expression's output 
field.
   
   DataFusion fixed the same for `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 a 
DataFusion upgrade does not fix it in Comet.
   
   ### Steps to reproduce
   
   No Spark type that Comet ships carries Arrow extension metadata, so the 
shape is a Rust unit test:
   
   ```rust
   let tagged = |name: &str| {
       Field::new(name, DataType::Binary, true).with_metadata(HashMap::from([(
           "ARROW:extension:name".to_string(),
           "geoarrow.wkb".to_string(),
       )]))
   };
   let schema = Schema::new(vec![
       Field::new("p", DataType::Boolean, true),
       tagged("g1"),
       tagged("g2"),
   ]);
   let c = |name: &str| col(name, &schema).unwrap();
   
   let case_when = CaseWhenExpr::try_new(vec![(c("p"), c("g1"))], 
Some(c("g2")))?;
   assert!(case_when.return_field(&schema)?.metadata().is_empty()); // passes 
today
   
   let if_expr = IfExpr::new(c("p"), c("g1"), c("g2"));
   assert!(if_expr.return_field(&schema)?.metadata().is_empty()); // passes 
today
   ```
   
   The output schema of a `ProjectionExec` over either expression has no 
metadata either, which is what the JVM sees.
   
   ### Expected behavior
   
   The output field carries the metadata merged from the branch fields, with 
the policy of DataFusion's `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`.
   
   ### Additional context
   
   Found with a geometry UDT mapped to the `geoarrow.wkb` extension type in a 
downstream build of Comet. Fix in #7763, based on the `datafusion-56` branch 
because DataFusion 56.0.0 ships `coerced_fields_metadata`, so it merges after 
#7249.
   
   ### Willingness to contribute
   
   I can contribute a fix for this bug independently
   


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