Skip to content

fix: sort from_many? relationships in aggregate joins - #266

Merged
zachdaniel merged 1 commit into
ash-project:mainfrom
sephianl:fix-from-many-aggregate-sort
Sep 28, 2026
Merged

zachdaniel merged 1 commit into
ash-project:mainfrom
sephianl:fix-from-many-aggregate-sort

Conversation

@DGollings

@DGollings DGollings commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

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 is LIMIT 1 with no ORDER BY.

related_subquery/3 is given two :sort? keys in the same keyword list. Keyword.get/3 takes the first — the caller's flag, which is false for aggregates — so the from_many? one below it never applies. limit_from_many/5 adds the LIMIT 1 regardless.

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

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests

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.
@zachdaniel
zachdaniel merged commit d4fad6a into ash-project:main Sep 28, 2026
23 of 25 checks passed
@zachdaniel

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