james-willis opened a new issue, #7843:
URL: https://github.com/apache/datafusion-comet/issues/7843
### Describe the bug
Comet reconciles the branch types of `CASE WHEN`, `IF`, `greatest` and
`least` itself, in `merge_branch_types`
(`native/spark-expr/src/conditional_funcs/case_when.rs`), because it bypasses
DataFusion's type coercion. For nested `Struct`, `List` and `Map` fields it
clones the first branch's field with the merged type and nullability, so the
nested metadata of every other branch is ignored: keys that only a later branch
carries are dropped, and when two branches' nested fields carry different
`ARROW:extension:name` values, `coerce_branch` casts the later branch to the
first branch's type and its values come out labelled as the first branch's
extension type, silently.
DataFusion settled the same question in apache/datafusion#27536 (DataFusion
57): struct coercion reconciles child fields by name with
`coerced_fields_metadata`, so metadata is merged with the earlier input winning
per key, the extension name and its parameters travel together from the input
that supplied the name, and differing extension names are a planning error.
#7763 uses that same function for the top-level output field of `CaseWhenExpr`
and `IfExpr`, so after it Comet applies one rule at the top level and a
different one a level down.
### Steps to reproduce
Rust, against the planner's `create_case_when`, on the #7763 branch (the
behaviour is the same on `main`):
```rust
let tagged = |ext: &str| {
Field::new("g", DataType::Binary, true)
.with_metadata(HashMap::from([("ARROW:extension:name".to_string(),
ext.to_string())]))
};
let schema = Schema::new(vec![
Field::new("p", DataType::Boolean, true),
Field::new("s1", DataType::Struct(vec![tagged("geoarrow.wkb")].into()),
true),
Field::new("s2", DataType::Struct(vec![tagged("arrow.json")].into()),
true),
]);
let c = |name: &str| col(name, &schema).unwrap();
let expr = create_case_when(vec![(c("p"), c("s1"))], Some(c("s2")),
&schema)?;
// CASE WHEN p@0 THEN s1@1 ELSE Cast [data_type: Struct("g": Binary,
metadata: {"ARROW:extension:name": "geoarrow.wkb"}), child: s2@2] END
let batch = RecordBatch::try_new(
Arc::new(schema.clone()),
vec![
Arc::new(BooleanArray::from(vec![false])),
Arc::new(StructArray::new(vec![tagged("geoarrow.wkb")].into(),
vec![Arc::new(BinaryArray::from_vec(vec![b"wkb"]))], None)),
Arc::new(StructArray::new(vec![tagged("arrow.json")].into(),
vec![Arc::new(BinaryArray::from_vec(vec![b"{}"]))], None)),
],
)?;
let out = expr.evaluate(&batch)?.into_array(1)?;
let out = out.as_any().downcast_ref::<StructArray>().unwrap();
assert_eq!(out.fields()[0].metadata()["ARROW:extension:name"],
"geoarrow.wkb"); // passes today
assert_eq!(out.column(0).as_any().downcast_ref::<BinaryArray>().unwrap().value(0),
b"{}"); // JSON bytes labelled as WKB
```
### Expected behavior
`create_case_when` returns a planning error that names both extension types,
as `coerced_fields_metadata` does for the top-level field and as DataFusion
57's struct coercion does. For nested fields whose metadata differs only in
unrelated keys, the merged field carries the union with the earlier branch
winning per key, instead of the first branch's keys only.
### Additional context
Spark's type system makes the differing-extension case unreachable for Spark
types backed by Arrow extension types today, so this is a policy alignment and
a safety net rather than a user-visible failure. It came up in the review of
#7774, where `CreateNamedStruct` filtering its children's metadata to
`ARROW:extension:*` is a workaround for the branch merge not reconciling nested
metadata. The fix is to compute the merged nested field's metadata with
`coerced_fields_metadata` and propagate its error, which needs DataFusion 56
(#7249), so it follows #7763.
### 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]