Skip to content

docs(zql): document aggregates - #328

Merged
arv merged 5 commits into
mainfrom
arv/aggregates
Nov 6, 2026
Merged

arv merged 5 commits into
mainfrom
arv/aggregates

Conversation

@arv

@arv arv commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Adds an Aggregates section to the ZQL page covering count(), sum(), avg(), min(), and max() on queries and relationships:

  • A top-level aggregate returns a single value. On a relationship, each row gets the value in place of the related rows.
  • Only the result is synced, and it stays up to date as rows change.
  • where(), start(), and limit() apply before the aggregate, as in SQL.
  • Column types, empty results (0 for count(), null otherwise), and which aggregates update immediately when a mutator runs.

It also adds Distinct and Group By subsections, for distinct() and groupBy(...).count()/sum()/avg()/min()/max(), which depend on rocicorp/mono-internal#295 and rocicorp/mono-internal#296:

  • distinct() returns one row per combination of its columns, and distinct().count() counts them, with null counted as a value, unlike SQL's COUNT(DISTINCT).
  • groupBy() returns one row per group with the aggregate under the function's name. null forms a group, and rows are ordered by the grouped columns only.
  • What they don't support yet: limit(), start(), one(), related(), a further aggregate, use in related()/whereExists(), JSON group columns, and ordering by the aggregate.
  • Which ones update immediately when a mutator runs.

Also regenerates assets/search-index.json.

Draft until aggregates, and #295/#296 for the new subsections, ship in a Zero release. The roadmap and status pages still list aggregates and group-by as future work and should be updated then.

@vercel

vercel Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
zero-docs Ready Ready Preview Nov 6, 2026 11:10am UTC

Request Review

@arv
arv requested a lite review from Copilot October 27, 2026 11:17
@arv
arv requested a review from aboodman October 27, 2026 11:17
@arv
arv marked this pull request as ready for review October 27, 2026 11:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The remaining feedback is limited to non-blocking wording nits.

No open issues 🎉

What changed in this PR

Documents ZQL aggregate queries and regenerates the search index for the new documentation section.

Changes:

  • Adds documentation for count(), sum(), avg(), min(), and max().
  • Covers relationship aggregates, filtering, pagination, result types, synchronization, and mutation updates.
  • Regenerates search-index entries and heading identifiers.
File Summary
contents/​docs/​zql.mdx Adds the Aggregates section with usage examples and behavioral details.
assets/​search-index.json Indexes the new section and updates subsequent section identifiers.

Review effort: Lite

Copilot AI lite review requested due to automatic review settings November 6, 2026 11:08
@arv
arv added this pull request to the merge queue Nov 6, 2026
Merged via the queue into main with commit bf8fc26 Nov 6, 2026
6 checks passed
@arv
arv deleted the arv/aggregates branch November 6, 2026 11:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

An active documentation inconsistency remains: the text broadly prohibits combining distinct() or groupBy() with aggregates despite preceding examples showing valid terminal aggregate usage.

1 open issue

Review effort: Lite

Comment thread contents/docs/zql.mdx

The aggregates work as they do without `groupBy()`, and a group whose values are all `null` gets `null` from `sum()`, `avg()`, `min()`, and `max()`. As with `distinct()`, rows with a `null` value form a group of their own, and the rows can only be ordered by the grouped columns. Ordering groups by their aggregate, such as the creators with the most issues first, isn't supported yet.

Neither `distinct()` nor `groupBy()` can be combined with `limit()`, `start()`, `one()`, `related()`, or another aggregate yet, and neither can group by a JSON column. They can't be used inside `related()` or `whereExists()` either, so counting each issue's distinct commenters isn't possible yet. As with other aggregates, a query defined with `defineQuery()` has to include them in its definition.
Copilot AI lite review requested due to automatic review settings November 6, 2026 11:12
arv added a commit that referenced this pull request Nov 6, 2026
This reverts commit bf8fc26.

Co-authored-by: Erik Arvidsson <erik.arvidsson@gmail.com>
Copilot stopped reviewing on behalf of arv due to an error November 6, 2026 11:13
arv added a commit that referenced this pull request Nov 6, 2026
Regenerate assets/search-index.json after #328 so it keeps the
aggregates sections and drops the JSON filter sections.

Claude-Session: https://claude.ai/code/session_013VzyesbmCjvSPN6qKkKz9X

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The documentation sentence contradicts the stated API behavior and examples by conflating supported aggregate forms, so it needs clarification to avoid misleading readers.

1 open issue

Review effort: Lite

This branch was successfully deployed

1 active deployment
Preview — 89854830 Deployed Nov 6, 2026 by vercel[bot]
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