Skip to content

fix: keep flattened calculations when wrapping a distinct query for aggregates - #284

Merged
zachdaniel merged 1 commit into
ash-project:mainfrom
nat-e:fix/keyset-sort-calculation-with-aggregate
Nov 12, 2026
Merged

zachdaniel merged 1 commit into
ash-project:mainfrom
nat-e:fix/keyset-sort-calculation-with-aggregate

Conversation

@nat-e

@nat-e nat-e commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #283

A read that sorts on a calculation needing DISTINCT ON and loads a lateral aggregate gets two subqueries. The DISTINCT ON one selects the sort calculation as __calculations____calc__0. wrap_in_subquery_for_aggregates/1 then reselected only the fields it flattened itself, so it dropped that column, and the keyset saved nil for it.

It now also reselects the fields an earlier subquery flattened.

The new test in query_test.exs fails without the fix. With it, the ash_postgres test in #283 passes, and so does ash_postgres's full suite.

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

@zachdaniel
zachdaniel merged commit c10abe6 into ash-project:main Nov 12, 2026
@zachdaniel

Copy link
Copy Markdown
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.

Keyset pages lose their sort calculation when the read loads an aggregate

2 participants