feat: aggregate annotations (annotate / having / order_by)#1742
Open
collerek wants to merge 16 commits into
Open
feat: aggregate annotations (annotate / having / order_by)#1742collerek wants to merge 16 commits into
collerek wants to merge 16 commits into
Conversation
…ion; dedup dialect quoting
Locks down that .annotate(...).values() with no explicit field list still carries the annotation alongside the regular model columns on every row.
Add docs/queries/aggregate-annotations.md covering annotate/having, ordering by an annotation (closes #566), Count/Sum/Avg/Min/Max, the zero-vs-NULL empty-relation semantics, many-to-many restrictions, and composition with select_related/pagination; link it from mkdocs.yml nav and add a one-line feature entry to README.md/docs/index.md. Also fixes a dialect-specific bug surfaced while verifying docs examples against PostgreSQL: the pagination subquery used for select_related + limit/offset ordered by an annotation's raw (non-coalesced) derived column instead of its own already-coalesced result column, so a childless parent's NULL count sorted as the largest value under PostgreSQL's default DESC ordering, ranking it ahead of real matches. Ordering now reuses the same coalesced column used in the main query, closing #266's example.
… annotation names
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.
Adds per-parent aggregate annotations to querysets:
annotate(task_count=ormar.Count("tasks"))withCount/Sum/Avg/Min/Maxover a relation or related column, compiled as a single pre-grouped derived-table join (one pass, 1:1 with the parent, so it composes withselect_relatedand pagination). Supports ordering by an annotation (order_by("-task_count")), filtering withhaving(task_count__gt=0), and reading results throughvalues()/values_list().Countover empty children is0(COALESCE); other functions areNULL. Works over reverse ForeignKey and — for bareCount— many-to-many. Verified on SQLite, PostgreSQL and MySQL at 100% coverage.Closes #566.
Partially addresses #266: this covers aggregate functions, ordering and
havingfor per-parent aggregates, but notgroup_by/rollups (grouping by a non-unique column), which remains as follow-up work.