Skip to content

Conversation

kosiew
Copy link
Contributor

@kosiew kosiew commented Aug 8, 2025

Which issue does this PR close?

Rationale for this change

Previously, aggregate UDFs could not easily access field-level metadata (e.g., Arrow extension types) when invoked with literal arguments only, because the AccumulatorArgs.schema was always derived from the physical schema — which is empty for literal-only inputs.
This change ensures that in such cases, a schema is synthesized from the literal expressions, preserving metadata and enabling richer accumulator behavior. It also clarifies API documentation for AccumulatorArgs and AggregateUDFImpl.

What changes are included in this PR?

  • Added args_schema() helper to AggregateFunctionExpr to return either the physical input schema or a synthesized schema from literals when the physical schema is empty.
  • Updated create_accumulator, create_sliding_accumulator, groups_accumulator_supported, and create_groups_accumulator to use the new schema logic via make_acc_args().
  • Enhanced AccumulatorArgs and AggregateUDFImpl documentation to explain how to access input field metadata and when synthesized schemas are used.
  • Introduced SchemaBasedAggregateUdf test in user_defined_aggregates.rs to validate metadata handling in literal-only aggregates.
  • Added unit test in aggregate.rs to verify correct schema behavior for both literal-only and physical-schema-present cases.

Are these changes tested?

Yes.

  • New integration test test_schema_based_aggregate_udf_metadata ensures that metadata from literals is accessible in the accumulator.
  • New unit test in aggregate.rs validates that args_schema() returns an owned schema for literal-only inputs and a borrowed schema for non-empty physical schemas.

Are there any user-facing changes?

Yes:

  • Aggregate UDF implementations can now reliably access input field metadata in AccumulatorArgs.schema for literal-only inputs.
  • No breaking API changes; only additional guarantees and improved documentation.

@github-actions github-actions bot added logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates core Core DataFusion crate labels Aug 8, 2025
kosiew added 7 commits August 12, 2025 12:39
- Improve documentation for AccumulatorArgs.schema and exprs:
  - Add example showing how to retrieve field metadata and return field.
  - Explain synthesized schema behavior for literal-only inputs.
  - Clarify precedence when inputs are mixed (physical schema metadata wins; synthesized metadata used only when physical schema is empty).
- Update AggregateFunctionExpr::args_schema docs:
  - Explain field order guarantees, synthesized schema usage, and that std::borrow::Cow is used to avoid allocations when possible.
- Add a TODO to factor AccumulatorArgs construction into a private helper.

Documentation-only changes; no behavioral changes.
@kosiew kosiew force-pushed the udaf-schema-16997 branch from fb6c9a8 to 2991acc Compare August 12, 2025 07:50
@kosiew kosiew marked this pull request as ready for review August 12, 2025 07:54
kosiew added 13 commits August 12, 2025 16:17
- Updated the `DummyUdf::new` function to accept a `Signature` parameter for initialization.
- Modified test cases to reflect the new initialization method for `DummyUdf`.
- Improved the `args_schema` method to better manage when to borrow the existing schema or create a new one, ensuring correct correspondence between expressions and schema fields.
- Added comments and examples for improved clarity on the schema handling behavior.
- Introduced a `build_acc_args` method to encapsulate the logic for building `AccumulatorArgs` and executing a closure with them.
- Updated the `create_accumulator`, `create_sliding_accumulator`, `groups_accumulator_supported`, and `create_groups_accumulator` methods to utilize the new method for better readability and maintainability.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AccumulatorArgs.schema is empty when passing in scalar input
1 participant