fix: sort from_many? relationships in aggregate joins - #266
Merged
zachdaniel merged 1 commit intoSep 28, 2026
Merged
Conversation
A duplicate `:sort?` key in the `related_subquery/3` call meant the `from_many?` branch never took effect: `Keyword.get/3` returns the first match, which is the caller's flag, and that is `false` for aggregates. The relationship still received `LIMIT 1` from `limit_from_many/5`, so it resolved to an arbitrary row rather than the sorted one. Loading the same relationship is unaffected, since Ash applies the sort on that path, so a load and an aggregate through it could disagree.
DGollings
added a commit
to sephianl/ash_postgres
that referenced
this pull request
Sep 28, 2026
…aggregate The existing coverage in `combination_test.exs` creates a single comment, so every ordering of the relationship agrees and the sort goes untested. These use two comments, and add a two-hop aggregate so the `from_many?` hop is not the first in the path. Requires ash-project/ash_sql#266.
2 tasks done
Contributor
|
🚀 Thank you for your contribution! 🚀 |
zachdaniel
pushed a commit
to ash-project/ash_postgres
that referenced
this pull request
Sep 28, 2026
…aggregate (#873) The existing coverage in `combination_test.exs` creates a single comment, so every ordering of the relationship agrees and the sort goes untested. These use two comments, and add a two-hop aggregate so the `from_many?` hop is not the first in the path. Requires ash-project/ash_sql#266.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Apologies for the quick AI PR, but confirmed against my own code plus had it TDDd.
Should solve a simple ordering bug in ash_sql
Description
Aggregating through a
has_one ... from_many? true, sort: ...resolves to an arbitrary row instead of the sorted one. The generated lateral isLIMIT 1with noORDER BY.related_subquery/3is given two:sort?keys in the same keyword list.Keyword.get/3takes the first — the caller's flag, which isfalsefor aggregates — so thefrom_many?one below it never applies.limit_from_many/5adds theLIMIT 1regardless.Loading the relationship is unaffected, since Ash applies the sort on that path, so a load and an aggregate through the same relationship can disagree.
Regression test in ash-project/ash_postgres#873 — it needs a SQL data layer, so it cannot live here. Reverting this commit fails it, with the aggregate returning the oldest row.
Contributor checklist