comphead commented on code in PR #7774:
URL: https://github.com/apache/datafusion-comet/pull/7774#discussion_r4743964642
##########
native/spark-expr/src/struct_funcs/create_named_struct.rs:
##########
@@ -42,11 +42,7 @@ impl CreateNamedStruct {
self.values
.iter()
.zip(&self.names)
- .map(|(expr, name)| {
- let data_type = expr.data_type(schema)?;
- let nullable = expr.nullable(schema)?;
- Ok(Field::new(name, data_type, nullable))
- })
+ .map(|(expr, name)|
Ok(expr.return_field(schema)?.as_ref().clone().with_name(name)))
Review Comment:
`fields` is also called from `evaluate` (line 80), so every batch now calls
`return_field` on every child. For a `Column` that is a schema lookup, but a
child that relies on the default `PhysicalExpr::return_field` formats the
expression and allocates a field on each batch. Separately, this copies the
child's whole metadata map into the struct field, not only the
`ARROW:extension:*` keys. A source column that carries `PARQUET:field_id`
(Iceberg or field-id Parquet reads) now gives a `DataType::Struct` whose child
field differs from one built over a different source column. `Field` equality
includes metadata and arrow's `zip` requires equal data types, so I wonder
whether a `CASE WHEN` or `IF` over two such `named_struct` values can now fail.
I did not run this. Could you add a test with field-id metadata on the child
and decide whether only the extension keys should be carried?
##########
native/spark-expr/src/array_funcs/list_extract.rs:
##########
@@ -95,8 +95,8 @@ impl ListExtract {
}
fn child_field(&self, input_schema: &Schema) -> DataFusionResult<FieldRef>
{
- match self.child.data_type(input_schema)? {
- DataType::List(field) | DataType::LargeList(field) => Ok(field),
+ match self.child.return_field(input_schema)?.data_type() {
Review Comment:
`child_field` now calls `self.child.return_field(...)` only to read the data
type. The item field with its metadata already sits inside
`DataType::List(field)`, so `self.child.data_type(input_schema)` returned the
same `FieldRef` before this change and the new `return_field` override is the
only change needed. This also costs time on every batch. `evaluate` calls
`self.data_type(&batch.schema())`, which now reaches the child's
`return_field`, and the default `PhysicalExpr::return_field` does
`format!("{self}")` and allocates a `Field`. Could `child_field` go back to
`data_type`?
##########
native/spark-expr/src/struct_funcs/get_struct_field.rs:
##########
@@ -51,8 +51,8 @@ impl GetStructField {
Self { child, ordinal }
}
- fn child_field(&self, input_schema: &Schema) ->
DataFusionResult<Arc<Field>> {
- match self.child.data_type(input_schema)? {
+ fn child_field(&self, input_schema: &Schema) -> DataFusionResult<FieldRef>
{
+ match self.child.return_field(input_schema)?.data_type() {
Review Comment:
Same as in `ListExtract::child_field`. `DataType::Struct(fields)` already
carries each field with its metadata, so `self.child.data_type(input_schema)`
gave the same field. Only the `return_field` override is needed.
##########
native/core/src/execution/planner.rs:
##########
@@ -7672,4 +7703,132 @@ mod tests {
let allowed = library.parent().unwrap().to_str().unwrap().to_string();
plan_with(NativeUdfPolicy::new(true, &allowed)).expect(BUILD_HINT);
}
+
+ #[test]
+ fn map_extract_preserves_field_metadata_through_planner() {
Review Comment:
The composed `CreateNamedStruct` half of this test repeats
`get_struct_field_preserves_field_metadata`,
`test_list_extract_preserves_field_metadata` and
`test_create_struct_preserves_field_metadata` with about 100 lines of extra
setup. The part the unit tests cannot cover is the planner path for
`map_extract`, meaning the `FieldRef` kept from `return_field_from_args` with
both values of `null_short_circuit`. Could this shrink to `map_extract` alone?
##########
native/core/src/execution/planner.rs:
##########
@@ -2257,10 +2269,22 @@ impl PhysicalPlanner {
Err(_) => input_expr_types.clone(),
};
- let arg_fields: Vec<_> = coerced_types
+ let arg_fields: Vec<_> = input_expr_fields
.iter()
+ .zip(&coerced_types)
.enumerate()
- .map(|(i, dt)| Arc::new(Field::new(format!("arg{i}"),
dt.clone(), true)))
+ .map(|(i, (f, dt))| {
+ if f.data_type() == dt {
+ Arc::new(
+ f.as_ref()
+ .clone()
+ .with_name(format!("arg{i}"))
+ .with_nullable(true),
+ )
+ } else {
+ Arc::new(Field::new(format!("arg{i}"),
dt.clone(), true))
+ }
+ })
Review Comment:
Propagating the argument fields (`input_expr_fields`, this rebuild, and the
matching one in the `fields_with_udf` branch) is not needed for `map_extract`.
`SparkMapExtract::return_field_from_args` reads the value field out of
`map_field.data_type()`, and its `coerce_types` returns the map type unchanged,
so the metadata already reached it through the old `Field::new("argN", dt,
true)` fields. What was lost was the `FieldRef` it returned, and keeping that
(`return_field` below) is the fix. As written this applies to every scalar
function in the planner. For example
`spark_array_slice::return_field_from_args` returns
`Arc::clone(&args.arg_fields[0])`, so a derived column now takes the input
column's top-level metadata, including `PARQUET:field_id`. I did not run this.
Could the argument plumbing be dropped and only the returned `FieldRef` kept?
That also removes the duplicated
`clone().with_name(format!("arg{i}")).with_nullable(true)` blocks.
--
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]