Repository navigation
Commit 9a7f8c1
feat(objectql): a nested-relation condition on a read verb is one read-scoped in-database subquery per relationship, up to three, where it is provably the same read as the id read (#33652)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} -->
_Requested by **Elon** · [project
thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DWLrF6ZURGN6BVpSbuDyHTg)_
Part of #33626 — the engine and analytics halves land here; the card
stays open until #33876 lands maintainer ruling Q2 (the engine's
`INVALID_FILTER` / 400 for MongoDB / OData, with the ADR-0139 capability
bit). The other parts are their own follow-up cards citing #33626 and
its rulings: #33875 (skills text, Tier H), #33876 (above, Tier H),
#33877 (multi-valued relation as a JSON-membership semi-join), #33878 (a
dataset measure's own `filter`).
Clause-②: yes (widening) — `{ account: { status: { $ne: 'cancelled' } }
}` on `find`, `findOne`, `count` and `aggregate` (and so every analytics
face that hands the engine the nested form) is served whatever the
number of matching related records on a driver that compiles the
subquery (the SQL drivers; the in-memory driver evaluates it in
process), where more than 1,000 answered `INVALID_FILTER` / 400. The
related read is planned through its whole pipeline as the caller and its
final `where` becomes the subquery's scope; related-object read
restrictions the planned read observes are carried into the subquery, or
the hop falls back to the id read (the related object read as the
caller, then `$in`, under the 1,000 cap). A relation key inside the
condition may now carry a condition of its own, down to ADR-0071's three
relationships (`{ contact: { account: { region: { name: 'NA' } } } }`),
where a second level answered `INVALID_FILTER` / 400; each hop is that
object's own read, and a fourth relationship is refused by name. On the
analytics engine-aggregate path a dotted `where` leaf through up to
three relationships (`order.customer.tier`), and a cross-object leaf in
a compiled dataset's own definition-level `filter`, reach the engine as
that nested condition, where both answered `INVALID_FIELD` / 400. Still
the id read, under the 1,000 cap: a related object on another
datasource, a federated related object, a driver that compiles no
subquery (MongoDB, OData), a related object an object-scoped middleware
or an `afterFind` hook shapes, a related object read under different
driver options than the outer object (decided before planning where the
registry answers it: the two objects' tenancy class under a walled
posture), a related read that a middleware answers itself, a
multi-valued relation and the `update` / `delete` verbs. A hop that
falls back after planning dispatches the related read twice: its
middleware chain and `beforeFind` hooks run for the planned read and
again for the id read. A per-measure `filter` carrying a cross-object
leaf stays refused. The SQL echo of a dataset `filter` carrying a served
cross-object leaf refuses `INVALID_FILTER` / 400 where it refused
`INVALID_FIELD` / 400; neither printed a statement.
Metadata-protocol: added — a compiled dataset's definition-level
`filter` (`@objectstack/service-analytics`) now accepts a cross-object
leaf on the engine-aggregate path, where it answered `INVALID_FIELD` /
400: a new accepted value for a dataset author.
Skills-docs: `content/docs/protocol/objectql/query-syntax.mdx` and
`content/docs/kernel/contracts/data-engine.mdx` describe the subquery,
the three-relationship limit, where the id read and its cap still apply
and which drivers compile the subquery. `skills/objectstack-query` still
says "up to 1000 related ids" and one level for every relation; that is
now stricter than the engine for a single-valued relation on a read verb
(a follow-up skills-only change, governed).
Area-lease pilot #33059, area A3. Contract record 6898853007 (PASS on
`96c23127`, no must-fix) is answered on this head: S2–S5 and N2 below.
The L2 record 6900638852 (PASS on `2d2eaa73`) is answered too: S1 by the
seat's option C (the author-facing rule on both docs pages, the
planned-leaf docblock), S2 by this body, S3 by the seat's rulings
(Acceptance notes), N2 in the pin.
## 维护者速读
-
改了什么:在上一版「只在可证明等同时走库内子查询」的基础上,把注册表能直接判定的回退提前到规划之前:关联对象是联邦对象(external),或在有组织墙的租户姿态下两个对象的租户类别不同,直接走
id 读取,关联读取只执行一次;changeset 与两篇文档列全了所有仍走 id 读取(及 1000
上限)的情形,并写明:在驱动调用之后收窄关联对象行的读规则,只有注册在该对象上(对象级中间件或 afterFind
钩子)才约束关系条件。本轮合并 main,解决与 #33897(View All 读者丢弃必填主表的父链)在
`engine-readable-ids-permission-formula.ts` 的冲突:两者都保留,先执行 View All
丢弃,再只对丢弃后仍保留的关系条件叶子做 id 预读;调用者写在必填主表上的关系条件永不随主表自身的读取范围一起被丢弃。
- 为什么改:合同评审 6898853007 指出联邦关联对象的子查询不能证明等同、回退时关联对象的中间件与 beforeFind
钩子会执行两次、驱动选项比较只钉了一个方向、文档漏列两种回退情形。
- 风险与回滚:所有改动都只把更多情形导向 main 原有的 id
读取(更保守);规划后才能判定的回退(中间件自行应答、只有本次请求决定的驱动选项差异)仍会二次执行,已在文档与 changeset
声明并钉住次数。冲突解决改变了两项改动交汇处的行为,属于已解决的冲突,需补 delta L1 与 L2 评审。回滚即 revert 本 PR。
-
先看:`packages/objectql/src/engine-readable-ids-permission-formula.ts`(`composeReadableIdsChain`
末尾:View All
丢弃与关系条件叶子的交汇)、`packages/objectql/src/relation-semi-join.ts`(`relationSemiJoinServes`
的两条规划前判定)、`packages/rest/src/data-nested-object-door.test.ts`(真实 SQLite
/ PG 上两个方向的驱动选项与租户类别钉子)。
- 两分钟验证:`pnpm --filter @objectstack/rest exec vitest run
src/data-nested-object-door.test.ts`,预期全部通过(PG 单元需
`OS_TEST_POSTGRES_URL`,未设时跳过)。
- 需要你做的:无,席位落地(卡片保持打开,等 #33876;注册时声明的裁决见 #34155)。
## What changes
- **The registry-answered fallbacks are decided before planning**
(`relationSemiJoinServes`), so the related read is dispatched once, as
the id read: a federated related object (ADR-0015 `external`), and,
under a posture that walls organizations, a related object of another
tenancy class than the read's object (one tenant-scoped, the other
platform-global or federated, `isDriverTenantScopedSchema` in
`engine-driver-options.ts`, the one spelling `buildDriverOptions` also
reads).
- **What is still decided after planning dispatches the related read
twice**, declared in the changeset and both docs pages and pinned by
count: a related read a middleware answers itself or adds rows to (after
planning), and driver options that differ only by what the request
records per object (at the executor boundary). Under the `single`
posture two tenancy classes read under the same options and still
compose (CONTROL).
- **The driver-options comparison is pinned in both directions on a real
driver** (SQLite, PostgreSQL 16): the outer read unscoped with the
related read scoped, and the opposite; and the tenancy-class decision in
its other direction (a tenant-scoped read on a related object that is no
tenant's), with the related read dispatched once.
- **Docs and changeset** list every case that keeps the id read and its
1,000 cap, adding the two missing ones (a related object read under
different driver options than the read's own object, most commonly its
tenant scope; a related read a middleware answers itself) and the
federated related object.
- **The author-facing rule for after-driver read rules** is on both docs
pages and in the `planRelationSemiJoinLeaf` docblock: a read rule that
narrows a related object's rows after its driver call bounds a relation
condition on that object only when it is registered for that object, as
an object-keyed middleware or an `afterFind` hook (the bound ADR-0139
D5.2 sets for a `ReadableIds` parent).
- Behaviour on every other path is unchanged from `96c23127`.
## Tests
On head `6062ec9c` (the merge of `origin/main` at `86350e12`, conflict
resolved against #33897): `@objectstack/objectql` full suite **598/598
files, 13278/13278 tests**, typecheck exit 0; `@objectstack/rest` full
suite with `OS_TEST_POSTGRES_URL` (private PostgreSQL 16) **413/413
files, 8672 passed / 144 skipped**, typecheck exit 0;
`@objectstack/service-analytics` full suite **215/215 files, 5229 passed
/ 335 skipped**, typecheck exit 0; #33897's two
`@objectstack/plugin-security` pin files
(`security-plugin-cbp-read-filter.test.ts`,
`controlled-by-parent-leaf-switch.test.ts`) **35/35**;
`engine-readable-ids-nested-composition.test.ts` (#33897's harness, with
the new interaction pin) and `engine-nested-relation-lowering.test.ts`
**48/48**. ESLint `--no-inline-config` on the two changed TS files exit
0.
<details><summary>Ablation of the interaction (one-shot, not
kept)</summary>
With the merge committed, the line that keeps a planned relation leaf
out of the View-All drop was removed with `scripts/ablation-replace.mjs`
(anchor 1 to 0, blob `4bf1bd36` to `e2e97ca8`):
`engine-readable-ids-nested-composition.test.ts` 1 failed / 17 passed,
the one red being the new interaction pin. Restored to the `HEAD` blob
`4bf1bd36`, `git diff HEAD` empty. The pin reads `src/`, so no rebuild
leg.
</details>
Earlier heads, below. Counts on head `2d2eaa73` (the merge of
`origin/main` at `63db7240` included) unless marked; local box, shared
verify lock, private PostgreSQL 16 for the live cells. On `6cf1ce0e`
(docs, a docblock and one pin's counter):
`data-nested-object-door.test.ts` 30 passed / 15 skipped,
`engine-nested-relation-lowering.test.ts` 30/30, objectql and rest
typecheck exit 0, ESLint `--no-inline-config` on the two changed TS
files exit 0, and every docs family of the derived list exit 0
(`check:skill-examples` NOT MEASURED locally: client SDK `dist` not
built in this worktree; it passed on `2d2eaa73`).
- `@objectstack/objectql` full suite: **598/598 files, 13258/13258
tests**; typecheck (`tsc --noEmit` plus `check:test-typecheck`) exit 0.
- `@objectstack/rest` full suite with `OS_TEST_POSTGRES_URL`: **413/413
files, 8672 passed / 144 skipped**; typecheck exit 0.
`data-nested-object-door.test.ts`: 30 passed / 15 skipped (the MySQL
cell, no server), SQLite and PostgreSQL cells each running the
two-tenant describe (now with the other tenancy direction) and the new
executor-boundary describe (both directions).
- `@objectstack/service-analytics` full suite: **215/215 files, 5229
passed / 335 skipped**; typecheck exit 0.
- `engine-nested-relation-lowering.test.ts`: 30/30 — the federated and
the two tenancy-class fallbacks decided before planning with one
dispatch, the `single`-posture CONTROL that still composes, the boundary
fallback and the middleware-answered fallback with two dispatches each.
<details><summary>Ablation (one-shot, not kept)</summary>
Each guard of this round was mutated with `scripts/ablation-replace.mjs`
(anchor hit, blob checked), the pins run, and the file restored to the
`HEAD` blob (`9231631f`, `git diff HEAD` empty) before the next leg;
where a pin reads `dist/`, objectql was rebuilt and
`ablation-dist-preflight.mjs` confirmed the marker (exit 0), and after
the last leg a rebuild with `--absent` confirmed all four markers gone.
- The federated check in `relationSemiJoinServes`: objectql 1 red (the
federated pin).
- The tenancy-class check: objectql 2 red (both directions, dispatch
count 2 where 1 is pinned); rest 2 red (the other tenancy direction,
SQLite and PostgreSQL, dispatch count).
- `sameReadDriverOptions` reading the outer read's keys only: objectql 1
red (the boundary pin); rest 2 red ("the outer read is unscoped, the
related read scoped", both cells).
- `sameReadDriverOptions` reading the related read's keys only: rest 2
red ("the outer read is scoped, the related read unscoped", both cells);
the objectql boundary pin stays green, as that pin's direction predicts.
</details>
## Acceptance notes
- **S1, by the seat's option C.** The author-facing rule is stated on
both docs pages and in the `planRelationSemiJoinLeaf` docblock: a read
rule that narrows a related object's rows after its driver call bounds a
relation condition only when it is registered for that object (an
object-keyed middleware or an `afterFind` hook), as ADR-0139 D5.2 bounds
a `ReadableIds` parent. A registration-time declaration that would let
the engine know such rules by another fact is #34155
(`needs-user-decision`: it widens the spec contracts surface).
- **A second dispatch on a fallback after planning.** A hop whose
fallback is decided only after the related read was planned — a
middleware answering the related read, or driver options only the
request sets — runs the related object's middleware chain and
`beforeFind` hooks twice (planned, then the id read), where main ran
them once. Declared in the changeset and both docs pages; pinned as two
dispatches (`engine-nested-relation-lowering.test.ts`), with one
dispatch pinned for every registry-decided fallback.
- **Seat 6's commits taken.** Four commits from
`claude/project-thread-m4k79t` (tip `39056411`) are cherry-picked with
`-x`: `6e35a5e5` and `d599a82e` with their wording on one after-driver
boundary left out (that boundary is S1, above), `6c7f6aef` and
`39056411` unchanged. An earlier work-in-progress commit of this round
(`777c4d06`) duplicated the first and is reverted on the branch
(`dc2fd16a`), no history rewritten.
- **Where the 1,000 cap stays.** For a federated related object the cap
stays, by the seat's ruling (S3 option A, area S2 thread): no driver
prepares a remote object that appears only inside a subquery. For a
single-valued hop across tenancy classes under an organization wall the
cap stays at this head; serving it as a subquery carrying the related
read's own driver options is #34157.
- **Conflict against #33897, resolved.** `origin/main` (`86350e12`)
carried #33897 (`3c2fb325`, card #33696, ruling 6893666857), which
conflicted with this branch in
`packages/objectql/src/engine-readable-ids-permission-formula.ts` (the
imports and `composeReadableIdsChain`'s tail). Both are kept: the
View-All drop of a required master's leaf over unrestricted hops runs
first; a leaf the nested-relation lowering planned is the caller's own
condition on the master, never the master's read scope, so the drop
never removes it (without that exclusion it would have dropped the
caller's condition, pinned by the new interaction pin and its ablation);
the planned-leaf id read runs only for leaves that survive the drop; the
composition runs over the pruned tree. Both import sets are kept.
`engine.ts` auto-merged, and its change from main is only the
`_registry` accessor #33897 adds to the chain host. This is a resolved
conflict, so delta L1 and L2 reviews are owed on `6062ec9c`.
- **N2 (title).** The title already qualifies the subquery as "where it
is provably the same read as the id read" and carries no "no id cap"
promise; it is unchanged.
- driver-memory serves the form in process (maintainer ruling Q1 on
#33626, comment 6889549817).
- MongoDB / OData keep main's capped id read (no refusal); the ruled
engine `INVALID_FILTER` / 400 and the ADR-0139 capability bit are
#33876, which is why this PR is `Part of`. Until that bit exists the
engine plans the subquery only for the drivers it names.
- `skills/objectstack-query` still teaches "up to 1000 related ids" and
one level (governed, #33875).
- The echo of a dataset `filter` carrying a served cross-object leaf
refuses `INVALID_FILTER` (it refused `INVALID_FIELD` before); both are
400 refusals of a statement that cannot be printed.
- The one hop-limit constant for objectql and service-analytics is not
in this PR: `@objectstack/objectql` is only a devDependency of
`@objectstack/service-analytics`, so sharing it needs a runtime
dependency or a `@objectstack/spec` export; the two stay tied by their
docblocks.
Resume-from:
claude/area-A3-33626@6062ec9c411903fa920343fde05537b5c46a4d9a
## Verification
<details><summary>Gate families (dispatch-gates --commands /
--ran)</summary>
On `6062ec9c`: 106 families derived from the merge-base diff; `--ran`
with an exit code per family: 106 accounted, 104 run exit 0, 2 NOT
MEASURED (derived from a recorded exit 3, PREREQUISITE NOT MET:
`check:skill-examples` and `check:dual-build-cjs-loads` read the `dist/`
of packages outside this diff's build closure, not built in this
worktree; both are CI's, in `Lint & Repo Gates`). `check:nul-bytes` exit
0.
On `2d2eaa73`: 106 accounted, 106 run, 0 NOT MEASURED.
</details>
No CI ran on `6cf1ce0e`: the PR was not mergeable against main (the
conflict above). CI on `6062ec9c`: see the PR checks.
<details><summary>CI on 2d2eaa7 (an earlier head)</summary>
53 check-runs, all completed: 36 success, 15 skipped, 2 failure. `Lint &
Repo Gates`, `TypeScript Type Check`, `Build Core`, `Check Changeset`,
`Governed Surface Queue Guard`, `Dogfood Regression Gate` and `Test
Core` 1–3 and 5–8 are success. `Test Core (4/8)` (and the `Test Core`
roll-up) is red on `@objectstack/scenarios`
`test/runner/run-date.test.ts`, two cases "Test timed out in 5000ms";
that package is outside this diff (tracked as #34081). Every other
package in that shard passed.
</details>
<details><summary>Declaration lines and merges</summary>
The `Clause-②:` and `Metadata-protocol:` lines are copied byte for byte
from `.changeset/33626-relation-semi-join.md`. `git merge-tree
--write-tree` of `6062ec9c` against `origin/main` at `0d1443d0`: no
conflict.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01QsR2jUg7BKfRBcTPyMxbLh)_
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01QsR2jUg7BKfRBcTPyMxbLh
<!-- signed: domain:server#5 · zhuangjianguo ·
session_01QsR2jUg7BKfRBcTPyMxbLh -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent babda9d commit 9a7f8c1
27 files changed
Lines changed: 1623 additions & 223 deletions
File tree
- .changeset
- content/docs
- kernel/contracts
- protocol/objectql
- packages
- objectql/src
- rest/src
- services/service-analytics/src
- __tests__
- strategies
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
196 | | - | |
197 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
198 | 203 | | |
199 | 204 | | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
205 | 233 | | |
206 | 234 | | |
207 | 235 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
687 | 687 | | |
688 | 688 | | |
689 | 689 | | |
690 | | - | |
691 | | - | |
692 | | - | |
693 | | - | |
694 | | - | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
695 | 706 | | |
696 | | - | |
697 | | - | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
698 | 712 | | |
699 | 713 | | |
700 | 714 | | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
701 | 736 | | |
702 | 737 | | |
703 | 738 | | |
704 | | - | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
705 | 742 | | |
706 | 743 | | |
707 | | - | |
708 | | - | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
709 | 752 | | |
710 | 753 | | |
711 | 754 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
157 | | - | |
| 157 | + | |
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1289 | 1289 | | |
1290 | 1290 | | |
1291 | 1291 | | |
1292 | | - | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
1293 | 1297 | | |
1294 | 1298 | | |
1295 | 1299 | | |
| |||
1353 | 1357 | | |
1354 | 1358 | | |
1355 | 1359 | | |
1356 | | - | |
| 1360 | + | |
1357 | 1361 | | |
1358 | 1362 | | |
1359 | 1363 | | |
| |||
1396 | 1400 | | |
1397 | 1401 | | |
1398 | 1402 | | |
1399 | | - | |
| 1403 | + | |
1400 | 1404 | | |
1401 | 1405 | | |
1402 | 1406 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
318 | 318 | | |
319 | 319 | | |
320 | 320 | | |
321 | | - | |
| 321 | + | |
322 | 322 | | |
323 | 323 | | |
324 | 324 | | |
| |||
411 | 411 | | |
412 | 412 | | |
413 | 413 | | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
414 | 425 | | |
415 | 426 | | |
416 | 427 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
545 | 546 | | |
546 | 547 | | |
547 | 548 | | |
548 | | - | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
549 | 561 | | |
550 | 562 | | |
551 | 563 | | |
| |||
837 | 849 | | |
838 | 850 | | |
839 | 851 | | |
840 | | - | |
| 852 | + | |
841 | 853 | | |
842 | 854 | | |
843 | 855 | | |
| |||
0 commit comments