Repository navigation
fix(databases): type, name and shape aggregate, distinct and join rows - #14537
Conversation
Query::distinct() takes nothing: it is a flag that deduplicates the
attributes the read's select() names. parseQuery() still accepted
{"method":"distinct","values":["grp"]} and dropped the values, so a
caller that meant "distinct on grp" got a distinct over whatever the
select named instead, with no sign anything was ignored.
Honouring values as a column list would give distinct a second way to
name its columns that disagrees with select() whenever both are given,
so the parser refuses it, and an API built on it answers 400.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Database::aggregate() hands back each row as the driver fetched it: no
casting, decoding or key mapping, unlike find(). The list action wrapped
those rows in a Document as they were, so the API returned counts as
"7", variances as "6.405119350733394e17", grouped booleans as "1"/"0",
arrays as their JSON text and datetimes in storage format. A group on a
system attribute came back under its storage column (_uid, _id,
_createdAt, _permissions), and a joined group came back bare ("grp")
unless another group shared its name, when it was "o.grp".
Aggregation decodes each row against the read's schema and queries:
count, countDistinct and the bitwise aggregates are integers; avg,
stddev and variance floats; sum, min and max take the type of the
attribute they read; each group is typed as a row types the attribute
(bool, int, float, decoded array, ISO 8601 datetime) and named as a read
names it, with a joined one always qualified by its join alias. It runs
after aggregate(), so a join the Joins hook refuses fails as before.
The rows come back as Documents\Aggregate, so the response models can
tell a row of an aggregation from a stored record without guessing from
its keys; the TTL cache rewraps cached aggregate rows the same way.
This belongs in utopia-php/database: aggregate() should return rows as
find() does. Until it does, the API decodes them here.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Row and Document models filled $sequence, $createdAt and $updatedAt with "" on every non-empty row. Aggregate rows have no record behind them, so they came back with three fake system fields; distinct rows carried $id: null beside $databaseId and $tableId; and a right or full join row that no row of the main table matched carried $id: "" and $permissions: []. The contract is now that a row carries a record attribute only when it has one: - aggregate and groupBy rows (typed as Documents\Aggregate) hold only their aggregates and groups, and a group on $id or $createdAt keeps it; - a row whose $id is empty or null (a distinct read that does not select $id, a join row with no main match) drops every record attribute, $databaseId and $tableId/$collectionId included; - a record keeps the "" fill, because staged transaction writes rely on it to hydrate SDK models before they are committed. An empty string is not a valid id, and a placeholder that typed SDKs can parse is still wrong data: a client that reads $id from an aggregate row gets "" and treats it as a row. A separate response model was not used, because listRows and listDocuments return one model and swapping it for a union would change every SDK's return type. The Row and Document specs keep their required system fields, so typed SDKs need to make them optional (a major SDK change) before they can parse these rows; the list endpoints' descriptions now state the contract. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… join rows Asserts, through TablesDB and the legacy documents API, the exact keys and types staging QA found wrong: aggregate values by method and attribute, grouped values typed like row values, system groups under their public names, joined groups qualified by alias, distinct rows holding only their selected columns, distinct with values refused, and an unmatched right join row with no record attributes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟠 1 high 💬 Inline comments (1)
🧹 Nitpicks (1) — 🟢 1 low
This PR makes aggregation queries on list-documents/list-rows return typed, correctly named aggregate rows instead of raw driver output. It introduces a new
Reviewed commit: 76a6dfc |
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
There was a problem hiding this comment.
Adds typed aggregation result decoding and response shaping for the Databases list endpoints, guarded by a distinct-query parse validation.
Key findings
- 🟠
$parsed->isAggregation()is called but never defined — XList.php:159
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
🟢 Tier S · Ready to mergeThis PR fixes the response shape for aggregate, distinct, and unmatched join results, including typed aggregate values and consistent field names. It also rejects unsupported distinct arguments, adjusts response models to omit synthetic system fields when appropriate, and documents the resulting API contract. Latest changes: The latest commits remove app-side aggregate decoding and custom list pagination/count handling, instead wrapping the database library’s aggregate rows and passing the original queries through to its native read and count behavior.
📂 Walkthrough · 12
Reviewed the commits since |
AGENTS.md forbids new regular expressions. The default alias only maps every byte outside [A-Za-z0-9_] to an underscore, which ctype_alnum expresses per byte with the same result. Database::aliasAggregates() is private, so there is no helper to reuse. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A group was only JSON decoded and cast, so an attribute with a filter or codec, such as an encrypted string, came back from groupBy in its stored form while a normal read of the same row decrypts it. Groups are now passed through Database::decode() for the collection, or join, they are read from, which applies every filter the attribute declares, honours disabled filters and resolves registered codecs the same way find() does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The findings that requested changes are resolved. See the summary comment for what is still open.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php
# Conflicts: # src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php # tests/e2e/Services/Databases/DatabasesBase.php
utopia-php/database#1024 (8.3.0) types, names and decodes aggregate rows the way find() returns documents, and owns the tests for it, so Aggregation and its unit tests go. The list wraps each row in an Aggregate, which the Document and Row response models use to leave out the stored attributes an aggregate row does not group. The AggregateShape e2e tests now cover the library's shaping end to end. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Reworked onto utopia-php/database 8.3.0, now on main through #14560.
What stays here is what the server owns:
The |
Release blocker for #11649, found by staging QA of appwrite-labs/cloud#5410.
Problem
Staging evidence:
"7", variance"6.405119350733394e17", grouped booleans"1"/"0", arrays as JSON strings, datetimes"2023-06-01 00:00:00.000"._uid,_createdAt,_id,_permissions.groupBy(['o.grp'])gavegrp, whilegroupBy(['grp','o.grp'])gaveo.grp.$sequence:"",$createdAt:"",$updatedAt:"",$id:null/"",$permissions:[].distinctwith values was silently accepted and the values ignored.Fix
Aggregationclass this PR first carried has been deleted.aggregate()now returns:Appwrite\Utopia\Database\Documents\Aggregate, on both the read and the cached path, so the response models know it is not a stored record.$idand unmatched join rows drop all system fields. Normal rows keep the existing""fill, which transactions depend on. Documented in list-rows.md and list-documents.md.select()columns.Tests
tests/unit/Utopia/Response/Model/RowTest.php,DocumentTest.phpandpackages/query/tests/API/ParseTest.php. They fail without the fix. The row-shaping unit tests moved to the library with the code (tests/unit/AggregationTest.phpin feat(aggregate): type, name and decode rows as a read returns them utopia-php/database#1024).tests/e2e/Services/Databases/Queries/AggregateShape.php(TablesDB and Legacy) asserts the response shape end to end. It now passes on the library's shaping alone.Follow-ups
$id,$sequenceand timestamps as required on Row/Document, so typed SDKs can't parse aggregate rows until those become optional (they already failed before this PR).$idstill gets""timestamps.🤖 Generated with Claude Code