Repository navigation
feat(query)!: 0.7.0 — single join form with required alias, getAlias(), typed cursors - #14180
Conversation
The protected dialect hooks were the last abbreviated method names on the builder surface (DEC-52, m12). 0.7.0 already breaks the join and cursor API, so the rename rides the same minor instead of costing another one later. compileSearchExpr, compileJsonContainsExpr, compileJsonOverlapsExpr, compileJsonPathExpr and compileVectorOrderExpr become compile*Expression. Subclasses must rename their overrides: an override of one of the abstract SQL hooks fails to load until renamed, and an override of compileVectorOrderExpression is otherwise no longer called. utopia-php/database overrides the first three and renames them with its query 0.7 bump. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Joins had three overloads (column form, alias-first and an alias-less ON list) whose meaning depended on argument types. Passing the ON list first and the alias last matched none of them cleanly, so the alias was silently dropped, and the ON list accepted any query, so limit(), select() or an ordering inside it was only rejected, or ignored, when the query was compiled (BUG-05). The database layer papered over missing aliases with generated ones (j0, j1, ...) and table-name fallbacks. Every join is now `join|leftJoin|rightJoin|fullOuterJoin($collection, $alias, $on)` or `crossJoin|naturalJoin($collection, $alias)` (DEC-42). The alias is required and the ON list is checked when the join is built: only on() and filter conditions, at any depth of and/or, so a bad member fails where it is written. Builders still validate and quote the alias as an identifier exactly as before; the fluent builder API keeps its optional alias. The alias moves into a property read by getAlias(), which replaces getJoinAlias() and also serves aggregates instead of values[0] (DEC-43). toArray() writes it as "alias"; parseQuery() still reads 0.6 payloads (values[0] for aggregates, cross/natural and alias-first joins, values[3] for column-form joins) so stored or cached queries keep parsing, and routes joins through the same validation as the factories. cursorAfter()/cursorBefore() take array|object (DEC-44 as answered by the user): the audit and usage ClickHouse adapters page with array rows, the database with documents. The builders read the row's _cursor column, the one they page on, instead of binding whatever was passed. The MongoDB builder now reads the single-form join (one on() equality per $lookup, under the join's alias) and rejects filtered ON lists instead of building a broken stage. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The monorepo resolves audit and usage against the query checkout, so their 0.6.* constraint stops resolving once packages/query is 0.7.0 (M5). Their array and object cursors stay valid under the array|object typing, so no call site changes. audit's Query override of the constructor dropped anything past $values; it now passes the alias on, so aliased aggregates built through the audit class keep their alias. abuse does not depend on utopia-php/query and is untouched. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟡 3 medium 💬 Inline comments (2)
📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to🟡 Medium — MongoDB // packages/query/src/Builder/MongoDB.php
1322 /** @var array{0?: string, 1?: string, 2?: string} $columns */
1323 $columns = $conditions[0]->getValues();
1324 $leftCol = $columns[0] ?? '';
1325 $operator = $columns[1] ?? '=';
1326 $rightCol = $columns[2] ?? '';
1327 $alias = $joinQuery->getAlias() !== '' ? $joinQuery->getAlias() : $table;
1328
1329 if ($operator !== '=') {
1330 throw new UnsupportedException(
1331 'MongoDB $lookup in localField/foreignField form only supports equality joins. '
1332 . "Got operator '{$operator}'. Use a pipeline-form \$lookup via a raw stage for non-equality joins."
1333 );
🧹 Nitpicks (3) — 🟢 3 low
The upgrade adopts query 0.7's breaking API changes: joins now carry an explicit
Reviewed commit: 132812c |
🟢 Tier S · Ready to merge
Introduces the query 0.7 API with required join aliases, a single join representation, alias properties, row-typed cursors, legacy parsing support, and renamed compilation hooks. SQL and MongoDB builders and their tests adopt these contracts, while audit and usage allow query ^0.6 or ^0.7. AST join conversion now preserves both OR operands, including nested AND conditions. Latest changes: The newest commits recursively convert OR operands with the join-aware converter and add a regression test covering column comparisons alongside nested AND predicates.
📂 Walkthrough · 14
✅ Fixed since the last review · 1
Reviewed the commits since |
There was a problem hiding this comment.
This PR bumps packages/query to utopia-php/query 0.7 with the refactored join/alias API and cursor typing, cascading the bump to packages/audit and packages/usage, and updates the test suites accordingly.
Key findings
- 🟡 Cursor docs recommend forms the builder rejects — README.md:175
- 🟡 AST join import drops multi-condition ON clauses — Builder.php:2616
- 🟡 MongoDB
$lookupaccepts emptyon()columns — MongoDB.php:1322
The factories accepted search, regex, spatial, JSON and containsAll members in an ON list, which every builder then rejected at compile time, so the early check promised joins that could never build. Method::isJoinCondition() is now the one set both use: on(), the comparisons, between, null checks, contains/containsAny/notContains, the startsWith/endsWith family, and and/or of those. It is the set the builders and utopia-php/database's join validator already allow. Errors use the builders' existing message. Co-Authored-By: Claude Opus 5.5 <[email protected]>
bin/monorepo check installs a package registry-resolved, and 0.7.0 is not on Packagist until this PR merges and is released, so a bare ^0.7 cannot resolve. audit and usage run on both lines: they page with array and object cursors and use none of the join or alias API. Linked test runs still pick the newest line (0.7.999), so CI tests them against this checkout. Co-Authored-By: Claude Opus 5.5 <[email protected]>
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
…okup columns applyAstJoins turned a parsed join condition into one on() only when it was a single comparison, so `ON a.x = b.x AND a.y = b.y` became on(..., 'AND') and failed to compile, and any other shape left the ON list empty. It now walks the AND chain: column-to-column comparisons become on(), other leaves become the filter queries the WHERE import already produces, and a leaf that is not a join condition throws instead of vanishing. The MongoDB $lookup took an on() with an empty column as an empty localField/foreignField; it now throws the same "left and right columns" error the SQL builders do. The README and the upgrade table showed cursor rows the bundled builders reject; they now show the `_cursor` row and say custom compilers read their own columns. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… SQL The join-condition import handed OR to the WHERE converter, which drops operands it cannot represent, so `a.id = o.a_id OR o.status = 'paid'` lost the column comparison and joined different rows. OR operands now go through the join-aware converter, so a column comparison stays an on() and an AND group stays an and(); anything unrepresentable still throws. Co-Authored-By: Claude Opus 5.5 <[email protected]>
What
utopia-php/query0.7.0: one join form with a required alias, an alias property, typed cursors, a validated ON list, and thecompile*Expr→compile*Expressionhook renames.packages/auditandpackages/usageacceptutopia-php/query ^0.6 || ^0.7in the same PR.bin/monorepo checkresolves packages against the registry, so a bare^0.7cannot resolve until 0.7.0 is tagged. Linked test runs still pick the 0.7 line from this checkout. Both packages work on either line.Why
join($table, [$on], 'alias')matched none of the three join overloads, so the alias was dropped without an error. The ON list also accepted any query, solimit(),select()or an ordering inside it was only rejected, or ignored, at compile time. Both are now impossible: there is a single join form, and the ON list is checked when the join is built.values[0]orvalues[3], depending on the overload, made every reader guess the position. The alias is now a property, serialised as"alias", and aggregates use the same property._cursorcolumn, which is the column they page on.Breaking
join($t, $left, $right, $op, $alias), the alias-less form and the alias-last form are removed. An empty alias throwsValidationException.Method::isJoinCondition():on(), comparisons,between, null checks,contains/containsAny/notContains, thestartsWith/endsWithfamily, orand/orof those, at any depth. This is the same set the builders compile, and they now use the same method. Anything else throwsValidationException(Unsupported join ON condition: <method>), includingsearch(),regex(), spatial, JSON and JSON strings.getJoinAlias()is removed. An aggregate's alias is no longer invalues[0].cursorAfter()/cursorBefore()takearray|object. The builders bind the row's_cursorvalue and throw if the row has none. A scalar cursor parsed from JSON still binds as it is.compileSearchExpr,compileJsonContainsExpr,compileJsonOverlapsExpr,compileJsonPathExprandcompileVectorOrderExprbecomecompile*Expression.parseQuery()reads"alias", falls back tovalues[0](aggregates, cross/natural joins and alias-first joins) and tovalues[3](column-form joins, which become a singleon()), and applies the same validation as the factories.on()equality per$lookup, under the join's alias. A filtered ON list throwsUnsupportedExceptioninstead of building a broken stage.->join($table, $left, $right, $operator, $alias = ''),->crossJoin(), …) is unchanged, and so is its SQL. Builders validate and quote aliases exactly as before.The CHANGELOG has the full entry and a 0.6 → 0.7 upgrade table. The README documents join queries, aliases and cursor rows.
Tests
tests/API/JoinTest.phptestOnListRejectsMemberThatIsNotACondition, with 19 non-condition members, at top level and nested inor(). Run againstmain3a08617, the limit, select and orderAsc cases fail with "Failed asserting that exception of type ValidationException is thrown". The[on], 'alias'order compilesJOIN \orders` ON …with noAS `ord``. Both pass on this branch.toArray/parseQuery/parsefor each factory. Legacyvalues[0](cross/natural, alias-first, aggregates) andvalues[3](column form). The"alias"key winning over a legacy position. Parse rejections.Query::cursorAfter/cursorBeforewith an array and with an object. A scalar is aTypeError. Newtests/Builder/CursorTest.phpcovers array,ArrayAccessand plain-object rows on MySQL and PostgreSQL, a missing_cursor, and a parsed scalar.testJoinAlias*,testCrossJoinAlias*,testNaturalJoinWithAlias,testSelfJoinWithAlias,testJoinWhereWithAlias*,JoinTenancy-style hook tests) keep their assertions. The only test edits are the new call form and cursor rows.packages/queryunit file except the performance group, and the audit and usage unit files, all pass. PHPStan is clean with the package config (level max, src and tests; the only errors are the pre-existing ones in theMongoDB\*classes of the E2E client, whose vendor package is missing locally). A full root PHPStan run is clean.Packages / query(check and test), plusauditandusagelinked against this checkout. abuse does not depend onutopia-php/query, so it is not in the matrix.Release:
bin/monorepo release query 0.7.0after merge. utopia-php/database picks it up in its 8.0 train.🤖 Generated with Claude Code