Skip to content

fix: preserve field metadata through max_by, min_by, mode, min and max - #7847

Draft
james-willis wants to merge 1 commit into
apache:mainfrom
james-willis:aggregate-return-field
Draft

james-willis wants to merge 1 commit into
apache:mainfrom
james-willis:aggregate-return-field

Conversation

@james-willis

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #7846

Rationale for this change

MaxMinBy, Mode, SparkMinMax and SparkStructMinMax return their value argument's type but implement only return_type, so the default return_field builds the output field from the type alone and the value field's metadata, extension types included, is lost. DataFusion's first_value and last_value keep it; these kernels should report the same field.

What changes are included in this PR?

  • return_field on the four aggregates: the field udaf_default_return_field built, with the function's name, the type return_type reports and nullable, plus the value argument's metadata, cloned whole.
  • The accumulators are unchanged. They build their output from the value's DataType, which already embeds nested fields, so for a struct value the output array has exactly the declared type.

Comet's Final and PartialMerge stages take their inputs as UnboundColumns typed from Spark's declared type, without metadata, so the output of a two-stage aggregate does not carry the tag to the JVM yet, for these kernels or for DataFusion's own. Carrying it across the exchange is a planner change and is out of scope here.

How are these changes tested?

New unit tests in each file. return_field_keeps_value_metadata, return_field_keeps_input_metadata (twice) and return_field_keeps_struct_metadata check the returned field's name, type, nullability and metadata, and fail without the change. struct_value_keeps_nested_metadata in max_min_by.rs and the accumulator part of return_field_keeps_struct_metadata check that the array an accumulator produces for a struct value with a tagged child has exactly the declared type; they pass before and after and document the contract.

cargo test -p datafusion-comet-spark-expr, cargo clippy -p datafusion-comet-spark-expr --all-targets -- -D warnings and cargo fmt --all -- --check pass locally.

There is no new Spark SQL test because no Spark type that Comet ships carries Arrow extension metadata. The aggregates' values, types and nullability are unchanged, which the existing suites cover.

@github-actions github-actions Bot added bug Something isn't working area:aggregation Hash aggregates, aggregate expressions labels Dec 3, 2026
@james-willis
james-willis marked this pull request as ready for review December 3, 2026 23:51
@james-willis
james-willis force-pushed the aggregate-return-field branch from f999322 to 2431086 Compare December 4, 2026 07:10
@james-willis
james-willis marked this pull request as draft December 4, 2026 18:43
@james-willis james-willis reopened this Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:aggregation Hash aggregates, aggregate expressions bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

max_by, min_by, mode and the float and struct min/max drop the field metadata of their value argument

1 participant