Skip to content

feat(query): add a relationship method for related document queries - #14502

Merged
abnegate merged 2 commits into
mainfrom
feat-query-relationship-method
Oct 15, 2026
Merged

abnegate merged 2 commits into
mainfrom
feat-query-relationship-method

Conversation

@abnegate

Copy link
Copy Markdown
Member

What

Adds Method::Relationship and Query::relationship(string $attribute, array $queries) to packages/query, released as utopia-php/query 0.7.1.

Query::relationship('comments', [
    Query::equal('approved', [true]),
    Query::orderDesc('$createdAt'),
    Query::limit(25),
]);
  • isNested() is true, so the children parse and serialize as queries.
  • It is in LOGICAL_TYPES, so the children count toward shape() and fingerprint(). Two relationship queries with different inner shapes don't collide.
  • Query::validate() skips it, because its children name another collection's attributes.
  • No bundled builder compiles it. A builder that gets one throws UnsupportedException, the same as for elemMatch.

Why

utopia-php/database#968 (nested relationship queries) lets callers filter, order, select and paginate the documents populated for one relationship attribute. Since 0.7 the database library builds its queries on this package's Method enum. A backed enum can't be extended downstream, so the method has to be added here. Encoding it some other way in the database library, for example by reusing elemMatch, would give one method two meanings.

Verification

  • bin/monorepo check query: pint, phpstan and rector are clean.
  • phpunit --testsuite unit --exclude-group=performance in packages/query: 5459 tests pass. New tests cover the factory, isNested(), the parse round trip, shape and fingerprint, and the validate() skip.

Not verified

🤖 Generated with Claude Code

utopia-php/database populates related documents itself and needs a query
that carries queries written against the related collection, so a caller
can filter, order, select and paginate the documents populated for one
relationship attribute. The method set is this enum, which consumers
cannot extend, so the method has to live here.

It is nested so its children parse, serialize and fingerprint like an
and/or's. validate() skips it because its children name another
collection's attributes, and no bundled builder compiles it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR introduces a Relationship query method in the packages/query library, letting callers express filters over relationship attributes with nested child queries.

Files Change
packages/query/src/Method.php, packages/query/src/Query.php Adds the Relationship enum case and a Query::relationship() factory; validate() now checks $skipTypes before nested recursion so relationship queries skip child validation, and shape()/fingerprint()/parsing handle the nested children.
packages/query/tests/API/HelperTest.php, packages/query/tests/API/LogicalTest.php Adds tests covering the factory, nesting, parse round-trip, and shape/fingerprint contribution of relationship queries.
packages/query/README.md, packages/query/CHANGELOG.md Documents the new method, its validate-skip behavior, and that no bundled database builder compiles it yet (bundled builders still throw UnsupportedException).

The implementation matches the documented contract: no bundled builder silently miscompiles the new method, and the parse/serialize round-trip and shape handling check out. One advisory note: the new public API ships in a 0.7.1 patch release, which is technically semver-noncompliant for 0.x (a minor bump would be conventional), though it follows this package's existing 0.x practice.

Reviewed commit: bbb4479

@ghost

ghost commented Oct 15, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The new tests exercise the unsupported behavior in each targeted builder, and I found no additional issue in these commits.

Adds Method::Relationship and Query::relationship() to represent queries against documents populated through a relationship attribute. Relationship children are parsed and serialized as queries, included in shape and fingerprint generation, and skipped during validation against the root collection's attributes; the README and changelog document the API. Builder tests also check that ClickHouse, MongoDB, and MySQL reject relationship filters as unsupported.

Latest changes: The newest commits add unsupported-filter regression tests for relationship queries in the ClickHouse, MongoDB, and MySQL builders.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 9
File Change
packages/query/CHANGELOG.md Documents the relationship query method and its behavior for the 0.7.1 release.
packages/query/README.md Adds the relationship query API example and explains its handling.
packages/query/src/Method.php Adds the Relationship enum case and classifies it as nested.
packages/query/src/Query.php Adds the relationship factory and includes relationship children in query shape/fingerprint handling while skipping root-attribute validation.
packages/query/tests/API/HelperTest.php Covers validation skipping relationship children.
packages/query/tests/API/LogicalTest.php Covers relationship construction, nesting, parse round-trip, shape, and fingerprint behavior.
packages/query/tests/Builder/ClickHouseTest.php Checks that relationship filters are rejected as unsupported.
packages/query/tests/Builder/MongoDBTest.php Checks that relationship filters are rejected as unsupported.
packages/query/tests/Builder/MySQLTest.php Checks that relationship filters are rejected as unsupported.

Reviewed the commits since bbb4479 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost 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.

🔵 Tier A · Looks good to merge. Summary

Comment thread packages/query/src/Query.php Resolved
@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → feat-query-relationship-method (after).

Metric Before After Change
🚀 Requests/sec 212.44 217.57 ⚪ +2.4%
⏱️ Latency P50 81.57 ms 80.15 ms ⚪ -1.7%
⏱️ Latency P95 187.96 ms 186.45 ms ⚪ -0.8%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 80.15 186.45 13,680 217.57 -1.52
Account 154.45 296.56 720 12.19 +6.48
TablesDB 78.71 149.11 7,440 120.99 +5.06
Storage 70.89 160.86 3,600 60.2 -31.14
Functions 110.27 216.88 1,920 32.85 +6.38

Top API waits (after)

API request Max wait (ms)
functions.create 504.93
account.name.update 482.6
account.prefs.update 384.3
tablesdb.rows.update 334.26
functions.delete 331.14

The README promises no bundled builder compiles a relationship query, as
with elemMatch, so the MySQL, ClickHouse and MongoDB builder tests now
assert the UnsupportedException a dialect throws for one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@ghost ghost 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.

🟢 Tier S · Looks good to merge. Summary

abnegate added a commit to utopia-php/database that referenced this pull request Oct 15, 2026
…nship

Relationship queries need a method in the query library's Method enum,
which this library cannot extend. appwrite/appwrite#14502 adds it; until
it is released as 0.7.1 this tracks its dev split.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@abnegate
abnegate merged commit 3f744a8 into main Oct 15, 2026
230 checks passed
@abnegate
abnegate deleted the feat-query-relationship-method branch October 15, 2026 06:11
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
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.

1 participant