Skip to content

Conversation

@ahey
Copy link
Contributor

@ahey ahey commented Nov 18, 2025

This fixes a bug that occurred in our application when upgrading to the latest ash packanges, in particular ash_sql. A PR with a test reproducing the issue was created on ash_postgres.

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

Fixes a GROUP BY error introduced in bb458d5 where already-computed
aggregates were being merged into the SELECT clause unconditionally.

When aggregates filter on nested first aggregates the nested aggregate
was incorrectly added to the SELECT clause in GROUP BY contexts,
causing PostgreSQL grouping_error "column must appear in the GROUP BY
clause or be used in an aggregate function"

The fix adds a select? guard to only merge already-computed aggregates
when we're in a select context. In internal subqueries with GROUP BY,
aggregates remain available in bindings for FILTER clauses without
being selected, avoiding the conflict.

A test demonstrating the bug was added to ash_postgres/test/aggregate_test.exs
test "multiple aggregates filtering on nested first aggregate"
@zachdaniel zachdaniel merged commit c2717eb into ash-project:main Nov 19, 2025
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.

2 participants