Repository navigation
fix(databases): count result rows for grouped and distinct list totals - #14536
Conversation
Database::count() reads only the filters and joins of a query, so a list that groups, aggregates or selects distinct rows reported the number of documents it read instead of the rows it returned: 7 for 4 groups, the joined row count for a grouped join, and the document count instead of 1 for an ungrouped aggregate. For those reads the total now counts the rows the same aggregate() or find() returns without limit, offset, cursor or order, capped at APP_LIMIT_COUNT. Reusing the read keeps row-level and join permissions identical to the results. A first page shorter than its limit is already the whole result, so it answers the total without a second read. Plain and joined reads keep count(), whose joined row count already matches the rows. Every list path (documents and rows, GET and POST query, cached and uncached) runs this action, so the E2E cover groupBy, distinct, a left join grouped by a joined column, a fan-out join and its distinct rows, and an ungrouped aggregate, against the readable rows of a session user and of a key, page by page. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
The PR replaces the single
Reviewed commit: 77da843 |
🟢 Tier S · Ready to merge
Database list totals now follow the actual result rows for grouped, aggregated, and distinct queries, while ordinary reads retain the existing count path. The shared Databases e2e suite covers totals across permissions, joins, cache usage, query endpoints, and pagination, including reads larger than the default page. Latest changes: The follow-up applies the default page limit when queries omit one, adjusts short-page total detection, silences the unpaged distinct recount, and adds coverage for totals beyond the default page.
📂 Walkthrough · 3
Merged |
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
There was a problem hiding this comment.
This PR fixes total counting for aggregation, distinct, and join fan-out queries in the Documents list endpoint by adding a dedicated countRows() path, plus new e2e total-count tests.
Key findings
- 🟡 Distinct/aggregation totals re-run the query and hydrate up to 5000 rows — XList.php:293
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
A list without a limit query still reads one default page, but the short-page shortcut required an explicit limit, so every unlimited grouped, aggregate or distinct list re-ran its read to count rows the page already held. The read now passes APP_LIMIT_LIST_DEFAULT itself instead of leaning on the library's own literal default of 25, and the shortcut compares the page against that effective limit. The distinct re-read already projects only the selected columns; it now also runs silent and without filters, so its rows skip attribute decoding and the Metadata decorator's per-row relationship walk. Query rewrite hooks, such as join authorization, still apply. The re-read itself stays until utopia-php/database count() wraps grouped, distinct and aggregate reads in a counted subquery, at which point countRows() becomes a plain count(). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Release blocker for the query-lib migration (#11649), found by staging QA of appwrite-labs/cloud#5410.
Problem
totalignored groupBy, distinct and aggregates, because utopia-php/databaseDatabase::count()only forwards filters and joins. Measured on Postgres (count()vs real rows):Fix
countRows().count().aggregate()/find()without limit, offset, cursor or order, capped atAPP_LIMIT_COUNT.Interim
This can load up to 5000 rows. A utopia-php/database fix is in progress (a wrapped
COUNTincount()). Once it's released,countRows()collapses back tocount().Known gap: a distinct total inside a transaction is still wrong (
TransactionState::countDocuments()).Tests
New
tests/e2e/Services/Databases/Queries/Totals.php(Legacy and TablesDB; key, session and console) covers:POST /querytotalis checked on whole reads and on every page. These tests were not run locally (no Docker).🤖 Generated with Claude Code