Skip to content

fix: select optimized first aggregates through their load - #288

Merged
zachdaniel merged 1 commit into
mainfrom
fix/optimized-first-aggregate-load
Nov 26, 2026
Merged

zachdaniel merged 1 commit into
mainfrom
fix/optimized-first-aggregate-load

Conversation

@jimsynz

@jimsynz jimsynz commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

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

Summary

Closes #287.

An optimized first aggregate was selected into the row under its name. A renamed aggregate with no load, such as aggregate_0 loaded only as a calculation dependency, became a key of the resource struct. Ecto then raised struct ... does not have the key :aggregate_0.

The optimized first branch now adds {aggregate.load, aggregate.name, dynamic} to dynamics, as the lateral and exists branches do. add_aggregate_selects/2 then puts an aggregate with no load into the aggregates map, and an aggregate with a load into that field.

Tests

Not in this PR

The two copies of the aggregate are still not merged when only one of them has the tenant filter, so the SQL joins the destination two times. I will look at that separately.

An optimized `first` aggregate was merged into the select under its
name, so a renamed aggregate with no load (for example `aggregate_0`,
loaded only as a calculation dependency) became a key of the resource
struct and Ecto raised. Add it to the dynamics instead, like the other
aggregate paths, so `add_aggregate_selects/2` puts it in the
`aggregates` map or in its load field.

Closes #287
@zachdaniel
zachdaniel merged commit 99a3f17 into main Nov 26, 2026
27 checks passed
@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.

Loading a first aggregate over a belongs_to with a calculation that uses it raises "does not have the key :aggregate_0" under attribute multitenancy

2 participants