Repository navigation
Commit 54f8e16
fix(analytics): a dimension through a lookup is one LEFT JOIN … GROUP BY on SQL drivers, no 500 past the bind limit (#34430)
Fixes #33664
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 改了什么:分析查询里"经 lookup 的维度"(如明细立方体上的 `opportunity.stage`)在 SQL
驱动上改为数据库内一条 `LEFT JOIN … GROUP BY`,不再先按外键分组、再用 `id IN (所有外键)` 读关联对象。
- 为什么改:关联记录数超过数据库绑定参数上限时(SQLite 约 3.2 万、PostgreSQL 65535),原路径直接报 500
`DATABASE_ERROR`,大数据量的看板无法出数。
-
风险与回滚:关联读取仍按调用者身份规划(CRUD、读中间件、行级范围、字段级权限、租户墙全部生效),分桶与度量与原路径逐格对等;内存/MongoDB/OData/Turso
驱动不变;任何 4xx
拒绝都回落原路径重判,报错信封不变;基础对象上的外键字段本身仍作为分组键交给中间件,字段级权限看不到它的调用者在两条路径上得到同一个
403。回滚即撤销本 PR,无数据或元数据迁移。
- 先看:`packages/drivers/driver-sql/src/sql-driver-aggregation.ts`(JOIN
子查询的租户墙与范围拼接)、`packages/objectql/src/relation-group-join.ts`(规划与拒绝条件)、`packages/services/service-analytics/src/strategies/objectql-strategy.ts`(`executeCrossObjectJoined`
的回落与重分桶)。
- 两分钟验证:`pnpm --filter @objectstack/service-analytics exec vitest run
src/__tests__/cross-object-relation-group-join.test.ts`,预期 23 个用例全绿,其中
33000 个外键的用例原路径 500、新路径每个 stage 11000 行。
- 需要你做的:无,席位落地。
## What changes
A dimension through a lookup on the ObjectQL analytics path (`POST
/analytics/query`, cube `bench_line_item`, `dimensions:
['opportunity.stage']`) is now one SQL statement on the SQL drivers
instead of an FK-expand whose `id IN (every distinct FK)` overflowed the
dialect's bind limit.
- **objectql, an engine-internal single-hop relation group key (option
A1).** `relation-group-join.ts` adds a `Symbol.for` read option
(`RELATION_GROUP_BY`) that no wire request can carry, so the public
`groupBy` still refuses a dotted path. `aggregate()` reads it, plans the
related object's read **as the caller** through the existing
`RELATION_SCOPE_PLAN` / `planRelationSemiJoinLeaf` /
`plannedRelationLeafVerdict` (no second copy of the read-scope leaf,
ADR-0139 D6), and hands the driver a `ReadableIds.compose(leaf,
plannedWhere)` parent per relation under a second symbol
(`RELATION_GROUP_JOINS`). The planned read is an `aggregate` grouped by
`['id', ...attrs]`, the same read `resolveFkAttr` ran minus its id list,
so the CRUD gate, read middlewares, row scope and the FLS group-by gate
all apply. A deny-sentinel scope hands a `null` parent (nothing
readable).
- **The base relation field is still a group key to every door.** The
FK-expand's base read groups by the relation field (`opportunity`), so
plugin-security's aggregate input gate (step 2.5b) and every other
group-key guard judge it. On the joined path the strategy drops that
key, so the engine now hands the middleware chain the base read grouped
by each relation field `groupBy` does not already name
(`relationFieldsJudgedAsGroupKeys`), and takes those keys back out only
in the statement it hands the driver (`withoutJudgedRelationFields`),
where the join replaces them. No field gate is copied: the field goes
through the chain's existing guards, so a caller who may not read
`line.opportunity` is refused on the joined path by the same gate, with
the same 403, as on the FK-expand. The docblock now says this rather
than only "Holding the key grants nothing" (L1 S1).
- **The related object's API exposure (#22661 pin).**
`relation-group-join.ts` calls `referenceTargetOf`, so
`second-object-read-exposure.pin.test.ts` must classify it. It is
classified `decided`: the analytics door asks the related object's
`enable` block through the spec's decision (`assertObjectsExposed`,
`api-exposure-door.ts`, for `aggregate`) over `queryObjects`, which
holds every hop's object, before any strategy runs. So the joined path
and the FK-expand get one verdict, and the engine is never handed the
request for an unexposed related object. The row adds `aggregate` to the
row type's operation union and `assertObjectsExposed` to the roster of
decision functions, which the `decided` form requires.
- **Decline, never guess.** The engine writes `request.outcome` = joined
or declined. It declines, with nothing read, for a driver outside
`RELATION_GROUP_JOIN_COMPILING_DRIVERS` (`driver.sql`,
`driver.sqlite-wasm`), a federated object, a multi-valued relation, a
relation whose target is not the requested object, a related object
`relationSemiJoinServes` keeps the id read for, a read middleware that
answers without the driver, and an aggregate the driver does not answer
natively (a non-UTC date bucket).
- **driver-sql.** `aggregate()` compiles each join as `LEFT JOIN (SELECT
id, attr… FROM parent WHERE planned scope AND parent tenant wall) __rgN
ON child.fk = __rgN.id`, groups by the attributes and a two-state `CASE
WHEN __rgN.id IS NULL THEN 0 ELSE 1 END` resolved column. The parent's
tenant wall comes from the read's own options, as for a composed
`ReadableIds` subquery. A parent the engine did not compose is refused
with INTERNAL_ERROR / 500 before anything compiles.
- **service-analytics.** The plugin's auto-bridge wires
`executeRelationGroupAggregate`. `ObjectQLStrategy#executeCrossObject`
tries the joined path first and maps each row through the same
`rebucketCrossObject` (an unresolved row is `(restricted)`, exactly as
an id the FK-expand's read did not return). On no hook, a decline, or
any 4xx it falls back to the unchanged FK-expand, which re-derives a
refusal in its own order and envelope.
- MongoDB, OData, memory and Turso keep today's FK-expand unchanged.
**Divergence from the card's Expected.** The card says "MongoDB/OData
refuse with a stable 400". Per seat route B (record 6917462284) they
keep today's FK-expand unchanged and nothing here refuses. That refusal,
if it lands, is owned by #33876.
## Why
The FK-expand bound one parameter per distinct related record. Past the
dialect's ceiling, SQLite answered `too many SQL variables` and
PostgreSQL a bind count overflow, both surfaced as `500 DATABASE_ERROR`.
The join binds only the scope's comparands and the two tenant walls,
whatever the row count.
## Tests
- `packages/objectql/src/relation-group-join.test.ts` (15, recording
driver). The middleware chain sees the base read grouped by the relation
field (for a `groupBy` with other keys, with none, and with the FK
column already named), and the driver's statement does not. Joined: one
driver call, no read of the related object reaches the driver, and the
parent scope is the request's condition as the related read's own doors
lowered it AND the middleware's condition. A deny sentinel gives a
`null` parent. A virtual attribute gives INVALID_FIELD / 400 with no
driver call. A driver row missing the resolved column gives
INTERNAL_ERROR / 500, as does an alias collision. Declines: memory,
MongoDB, OData, Turso, multi-valued, object mismatch, short-circuit
middleware, non-UTC bucket.
-
`packages/drivers/driver-sql/src/sql-driver-relation-group-join.test.ts`,
dialect matrix. Buckets with an empty FK, an orphan FK, a scope-hidden
parent and another tenant's parent all unresolved, every child counted
once. A base group key beside the join. A `null` parent. At most 3
bindings (child wall, parent wall, one comparand). A forged parent gives
500.
-
`packages/services/service-analytics/src/__tests__/cross-object-relation-group-join.test.ts`
(23, real ObjectQL + SqliteWasmDriver, isolated tenancy). New in this
round: a caller whose field permissions hide `line.opportunity` gets the
same `PERMISSION_DENIED` / 403 with the join wired as without it, and
the engine is handed no request. The engine, handed the relation group
request directly (no analytics door), refuses that caller with the same
envelope as the FK-expand's base read grouped by the field; an admin
control joins, and its rows carry no `opportunity` key. A related object
declaring `apiEnabled: false` answers `OBJECT_API_DISABLED` / 404, and
one whose `apiMethods` does not grant the aggregate read answers
`OBJECT_API_METHOD_NOT_ALLOWED` / 405, on both paths, with no request
handed to the engine. The field gate in this suite is a test middleware
that judges the AST's `groupBy` and `aggregations` the way step 2.5b
does, because this package does not depend on plugin-security. Parity of
the joined path against the FK-expand for 3 callers (admin org A; member
org A with a policy scope and a read middleware hiding parents; admin
org B) × 4 dimension shapes. Explicit buckets for `(restricted)`, null
FK, orphan FK and a cross-tenant FK. The read scope and the tenant wall
on the joined parent. 33,000 distinct FKs: the FK-expand answers
DATABASE_ERROR / 500, the joined path answers 11,000 per stage.
- `packages/objectql/src/federated-injected-column-readers.test.ts`: two
disposition rows for the new module's federated seams (`exempt`).
- `packages/core/src/security/second-object-read-exposure.pin.test.ts`:
the `decided` row above (4/4).
<details><summary>Ablations (each disable → red → restore; restore
proven by blob == HEAD and an empty git diff HEAD)</summary>
All run through `scripts/ablation-replace.mjs` in WRAP mode against
`src` (every subject is resolved from source in its suite). The first
six rows were measured at `c87aab92d`, the last two at `2f6eef1ff`.
| pin | mutation | suite | result |
|:--|:--|:--|:--|
| parity / `(restricted)` | resolved column read as always 1 | analytics
join test | 15 failed / 18 |
| bind limit + routing | plugin wires no `executeRelationGroupAggregate`
| analytics join test | 13 failed / 18 (incl. the 33,000-FK joined case)
|
| read scope on the parent | compose with `undefined` instead of the
planned scope | objectql join test | 1 failed / 14 |
| tenant wall on the parent | `applyTenantScope(sub, …)` deleted |
driver-sql join test (sqlite) | 3 failed |
| non-SQL unchanged | memory driver added to the compiling set |
objectql join test | 1 failed / 14 |
| refusal envelope unchanged | the 4xx fallback disabled |
`objectql-echo-non-date-bucket.test.ts` | 1 failed / 22 (`groupBy.0` vs
`groupBy.1`) |
| base relation field judged as a group key |
`relationFieldsJudgedAsGroupKeys(…)` replaced by `[]` | analytics join
test; objectql join test | 1 failed / 23 (the engine-level FLS pin); 1
failed / 15 |
| related object's exposure | `assertObjectsExposed` answers every
object as served | analytics join test | 2 failed / 23 (the two exposure
cases) |
The first parity attempt replaced the anchor with the single character
`i`. The tool refused it because the replacement count could not rise,
so that attempt measured nothing; it was rerun with a distinct
replacement (row above).
</details>
## Acceptance notes
- **Bench (M0, `packages/qa/dogfood/bench/sql-perf`, `--only
analytics_cross_object_dim`, admin, SQLite, 20 iterations).** "Before"
is this head with the hook unwired in the built `service-analytics` dist
(the ablation above, proven present in `dist/` and absent after the
restore and rebuild). That path is today's FK-expand.
| scale (line items / opportunities) | path | status | p50 ms | p95 ms |
db ms p50 | SQL statements |
|:--|:--|:--|--:|--:|--:|--:|
| 400,000 / 40,000 | before | 500 DATABASE_ERROR | 284.1 | 350.0 | 205.8
| 10 |
| 400,000 / 40,000 | after | 200 | 528.6 | 591.6 | 498 | 9 |
| 200,000 / 20,000 | before | 200 | 220.0 | 261.3 | 146.7 | 10 |
| 200,000 / 20,000 | after | 200 | 241.0 | 256.7 | 220.5 | 9 |
These are shared-box seconds: the bench ran outside the verify lock
after about 40 minutes queued behind another battery. Under the bind
limit, SQLite pays more database time for the joined statement than for
the two FK-expand reads (about 1.5x db time, about 1.1x request p50 at
20k parents). Past the limit, the request answers instead of failing.
PostgreSQL bench: NOT MEASURED (the local PG server died mid-session;
see Verification).
- Turso is not served (its remote transport compiles its own
aggregates). It keeps the FK-expand.
- Optional L1 N2 was not taken: building the derived table from
`readableIdsSubquery(…)` needs that method on the driver's aggregation
host, which is bound in `sql-driver.ts`, not in the same file.
- `main`'s `check:platform-checklist` red (approver-resolution-matrix
VARIANTS STALE, since #34300) is not this PR's and is not fixed here.
`Lint & Repo Gates` reads success on `2f6eef1ff`.
- The analytics-suite field gate stands in for plugin-security's step
2.5b, whose own input contract (`ast.groupBy` / `ast.aggregations`) is
pinned in `security-plugin.test.ts`.
- Files beyond the claim's literal names: `objectql/src/engine.ts` (one
host-wiring line), `service-analytics/src/analytics-service.ts` (config
pass-through of the hook), and
`objectql/src/federated-injected-column-readers.test.ts` (two ledger
rows its census demanded) are wiring for the claimed doors and the
plugin bridge.
`core/src/security/second-object-read-exposure.pin.test.ts` (one CALLERS
row, its operation literal and its roster entry) is the census the new
module's `referenceTargetOf` call joins.
- Out of scope, noted and not fixed: the ObjectQL path buckets a line
whose FK is not set under `(restricted)`, a label that reads as a
permission outcome. This is pre-existing, and parity is kept here on
purpose.
- Serial constraint with #34198: it merged into `main` before this
branch's last merge of `origin/main`. `git merge-tree` against its head
and against `origin/main` is clean.
## Verification
Head of record: `2f6eef1ff`.
<details><summary>Suites and typechecks (at 2f6eef1, under the verify
lock)</summary>
- `pnpm --filter @objectstack/objectql test`: 604/604 files, 13485/13485
tests.
- `pnpm --filter @objectstack/driver-sql test`: 256 files passed, 13
skipped; 4331 tests passed, 247 skipped. The PostgreSQL cell of the join
test ran in round 1 against a local PostgreSQL 16. MySQL, MSSQL and
Oracle cells: NOT MEASURED locally.
- `pnpm --filter @objectstack/service-analytics test`: 218/218 files;
5318 tests passed, 335 skipped.
- `@objectstack/core` repo project,
`second-object-read-exposure.pin.test.ts`: 4/4. It was red on
`c87aab92d` in CI (Test Core 7/8).
- `typecheck` for objectql (with `check:test-typecheck`), driver-sql,
service-analytics and core: exit 0 each.
</details>
<details><summary>Derived gates (at 2f6eef1)</summary>
`node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack` derived 82 commands. 81 exited 0 on the
first run. `check:dual-build-cjs-loads` exited 3 (PREREQUISITE NOT MET,
no workspace `dist/`), and after `pnpm build` (exit 0) it exited 0.
`--ran` reconciliation with an exit code on every line: 82 derived, 82
run, 0 NOT-MEASURED, 0 UNRUN, a derived zero. The working tree was clean
after the build.
</details>
<details><summary>CI on 2f6eef1</summary>
Check-runs: 55 success, 11 skipped, 0 failed. `Test Core` (all 8
shards), `Lint & Repo Gates`, `TypeScript Type Check`, `Build Core`,
`Governed Surface Queue Guard`, `Type Check UI`, `Test UI`, `Build & E2E
UI`, `Live E2E UI` and `Dogfood Regression Gate` all report success.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01FEkQ5nNrLRm8ssuwWcBVcw)_
<!-- signed: repo:objectstack#1 · os-project-manager ·
session_01FEkQ5nNrLRm8ssuwWcBVcw -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent d4e4dfb commit 54f8e16
14 files changed
Lines changed: 1844 additions & 58 deletions
File tree
- .changeset
- packages
- core/src/security
- drivers/driver-sql/src
- objectql/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 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
100 | | - | |
| 100 | + | |
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
198 | 209 | | |
199 | 210 | | |
200 | 211 | | |
| |||
489 | 500 | | |
490 | 501 | | |
491 | 502 | | |
| 503 | + | |
492 | 504 | | |
493 | 505 | | |
494 | 506 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | | - | |
| 34 | + | |
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
82 | 86 | | |
83 | 87 | | |
84 | 88 | | |
| |||
95 | 99 | | |
96 | 100 | | |
97 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
98 | 109 | | |
99 | 110 | | |
100 | 111 | | |
| |||
240 | 251 | | |
241 | 252 | | |
242 | 253 | | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
243 | 309 | | |
244 | 310 | | |
245 | 311 | | |
| |||
457 | 523 | | |
458 | 524 | | |
459 | 525 | | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
460 | 572 | | |
461 | 573 | | |
462 | 574 | | |
| |||
0 commit comments