Repository navigation
feat(query): read a join without an alias under its collection - #14594
Conversation
An alias is only needed to tell two joins of one collection apart, so requiring it on every join made the common single join of a table more verbose than it needs to be. The factories and parse() now read a join given an empty alias under the collection it joins, in every accepted layout (the alias property, the legacy alias-first and column forms, the nested ON form, cross and natural joins). The single typed factory signature from 0.7.0 stays, so nothing silently drops an alias again; '' just means "use the collection". Only a join with neither a collection nor an alias still throws. Whether a join is read under its collection's name is exposed as isAliasDefaulted() so consumers can explain why that name cannot be an alias (their own naming rules live with them). It is derived in the constructor as "the join's alias equals its collection" rather than serialised as a new key: - toArray() keeps emitting the alias itself, so a round trip through toArray()/parse() re-derives the same flag without a new wire field that untrusted input could set out of step with the alias. - Consumers that point a join at another table after parsing (Appwrite swaps the public collection ID for its internal table via clone + setAttribute()) keep both the alias and the flag, since neither is recomputed from the renamed collection. Omitting the alias from toArray() instead would have re-defaulted it to the internal table name on a later parse. - An explicit alias equal to the collection reads exactly like a defaulted one, so treating it as defaulted is accurate for any message built on it. Builder joins (new Query(Method::Join, ...) with no alias) are left unaliased, so generated SQL for them is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟡 1 medium 📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to🟡 Medium — Persist // packages/query/src/Query.php
454 public function toArray(): array
455 {
456 $array = ['method' => $this->method->value];
457
458 if (! empty($this->attribute)) {
459 $array['attribute'] = $this->attribute;
460 }
461
462 if ($this->alias !== '') {
463 $array['alias'] = $this->alias;
464 }
465
The change fixes a bug where a join without an explicit alias lost its identity when the joined collection was renamed: the alias is now defaulted to the collection name and tracked via
One medium-severity issue was found: the Reviewed commit: 35c311d |
🟢 Tier S · Ready to merge
This PR lets joins with an empty alias be read under their collection name across the factory and parsing forms, and adds Latest changes: The latest commits clarify that a renamed join preserves its alias when serialized but is parsed as explicitly aliased, and add tests for that behavior and for parsing an unaliased builder join.
📂 Walkthrough · 4
Reviewed the commits since |
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
The defaulted flag is derived from the serialized form, so a join serialized after setAttribute() renamed its collection is parsed as an explicit alias. That is the intended trade-off: the alias itself is kept (rows stay keyed by the public name), and the flag only feeds consumers' wording, which they read from the in-memory query. The README and changelog claimed the flag survived that combination; say what actually happens and pin it, along with the round trip of an unaliased builder join. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@tenki-reviewer re the serialization finding: same answer as the inline thread. This is intended, and the docs overstated it. Fixed in 8f95a91: the README and changelog now say what happens. The flag is derived from the serialized form (alias equals collection) on purpose. A join serialized after
|
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
The change relaxes the join DSL so that an empty alias no longer throws: joins without an explicit alias now default to their collection name, and a Reviewed commit: 8f95a91 |
There was a problem hiding this comment.
This PR lets Query::join() default an omitted alias to the collection name while preserving a defaulted-alias flag across toArray()/parse() round trips, with matching tests and docs.
Key findings
- 🟡 Defaulted alias can shadow the base table or duplicate another join alias — Query.php:1464
What
A join given no alias is now read under the name of the collection it joins, instead of throwing
Join alias is required. This covers the factories (join,leftJoin,rightJoin,fullOuterJoin,crossJoin,naturalJoinwith'') andparse()/parseQuery()in every layout they accept: thealiasproperty, the legacy alias-first and column forms, the nested ON form, and cross/natural joins. A join with neither a collection nor an alias still throws.The factory signature from 0.7.0 is unchanged (
join(string $collection, string $alias, array $on));''now means "use the collection".Query::isAliasDefaulted()(new) tells consumers that a join is read under its collection's name, so they can say why that name can't be used when their own alias rules reject it. utopia-php/database uses it for its pattern, reserved-name, relationship-key and duplicate checks. None of that knowledge lives here.How the defaulted state is represented
It is derived in the constructor as "this join's alias equals its collection". It is not a new serialised key:
toArray()still emits the alias, so atoArray()/parse()ortoString()/parse()round trip re-derives the same flag. There is no new wire field that untrusted input could set out of step with the alias.Joinshook swaps the public collection ID for the internal table with(clone $query)->setAttribute(...). That keeps both the alias and the flag, because neither is recomputed from the renamed collection. Rows stay keyed"<publicId>.<column>". Leaving the alias out oftoArray()would instead have re-defaulted it to the internal table name on a later parse.testDefaultedAliasSurvivesRenamingTheCollectionpins this.Builder joins (
new Query(Method::Join, ...)with no alias, asBuilder::join()creates) stay unaliased, so their generated SQL is unchanged (testBuilderJoinWithoutAliasStaysUnaliased).Tests: fail before, pass after
New tests are in
packages/query/tests/API/JoinTest.php. Each was run against themainsrc/Query.phpand then against this branch:testConditionedJoinWithoutAliasIsReadUnderItsCollection(join, leftJoin, rightJoin, fullOuterJoin)Join alias is required)testUnconditionedJoinWithoutAliasIsReadUnderItsCollection(crossJoin, naturalJoin)testParsedJoinWithoutAliasIsReadUnderItsCollection(nested ON, emptyaliasproperty, empty alias-first, column form, column form with empty alias, crossJoin, crossJoin with empty legacy alias, naturalJoin)testParsedJoinWithoutAliasRoundTrips(same 8 layouts,toArray()andtoString()round trips)testDefaultedAliasSurvivesRenamingTheCollectiontestDefaultedJoinCompilesUnderItsCollectiontestConditionedJoinWithAliasIsNotDefaulted/testUnconditionedJoinWithAliasIsNotDefaulted/testParsedJoinWithAliasRoundTripsAsNotDefaulted/testJoinAliasedAsItsCollectionCountsAsDefaulted/testQueriesOtherThanJoinsAreNeverDefaulted/testBuilderJoinWithoutAliasStaysUnaliasedisAliasDefaulted())testConditionedJoinWithoutAliasOrCollectionIsRejected/testUnconditionedJoinWithoutAliasOrCollectionIsRejected/testParseRejectsJoinWithoutAliasOrCollectionBefore: 41 tests, 34 errors. After: the full
bin/monorepo test queryunit tier passes (5568 tests).bin/monorepo check query(Pint, PHPStan, Rector) passes.Next, once this is released as
query/0.7.2: a utopia-php/database PR bumps to^0.7.2and adds the validator rules and messages, plus e2e tests on every adapter.🤖 Generated with Claude Code