Skip to content

Conversation

@sevenseacat
Copy link
Contributor

On PostgreSQL 16+, any_value() was used for first aggregates as an optimization. However, any_value() by PostgreSQL design returns "an arbitrary value from the non-null input values" - it inherently skips NULLs. This broke include_nil?: true for first aggregates.

When include_nil? is true, we now always use array_agg with [1] indexing, since only array_agg can include NULL values in the result.

Reference: https://www.postgresql.org/docs/16/functions-aggregate.html

This makes the newly-failing test in ash-project/ash_postgres#683 pass.

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

On PostgreSQL 16+, `any_value()` was used for `first` aggregates as an
optimization. However, `any_value()` by PostgreSQL design returns "an
arbitrary value from the non-null input values" - it inherently skips
NULLs. This broke `include_nil?: true` for `first` aggregates.

When `include_nil?` is true, we now always use `array_agg` with `[1]`
indexing, since only `array_agg` can include NULL values in the result.

Reference: https://www.postgresql.org/docs/16/functions-aggregate.html
@zachdaniel zachdaniel merged commit d118c1b into main Feb 4, 2026
23 checks passed
@zachdaniel
Copy link
Contributor

🚀 Thank you for your contribution! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants