Repository navigation
Commit 498c967
feat(service-analytics): serve a one-hop cross-object where on the engine-aggregate path (#33512)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} -->
_Requested by **Elon** · [project
thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DFWmDYdUPFhME7Z7Ky3ySCZ)_
Fixes #33335
Area-lease pilot #33059, area A3.
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
-
改了什么:分析服务的引擎聚合路径(ObjectQLStrategy)现在支持按关联记录字段过滤(`'cb_order.status'`,一跳)。以前这种过滤直接报
400。
- 为什么改:明细行仪表盘没办法排除"父订单已取消"的行。只有原生 SQL 路径能做,而带按月分桶或 controlled_by_parent
的仪表盘恰好走不到原生 SQL。
- 风险与回滚:点号过滤被改写成引擎已有的嵌套关联条件。父对象以调用者身份读取,RLS
与字段权限照常生效。没有父记录或读不到父记录的行一律不选中(`$ne` 也一样),这一点与原生 SQL 在无行级范围时的 LEFT JOIN
语义不同,已写进文档。回滚只需 revert 本 PR,仍不支持的形状照旧报 400。
-
先看:`packages/services/service-analytics/src/strategies/cross-object-where.ts`(改写与拒绝分类,重点是
`negated` 与 `join-target` 两个分支),`objectql-strategy.ts` 的 `servedWhere` 与
`planCrossObject`,以及
`packages/rest/src/analytics-nested-relation-filter.test.ts` 的 #33335
段(真实安全层下的端到端)。
- 两分钟验证:`pnpm --filter @objectstack/service-analytics exec vitest run
--maxWorkers=2
src/__tests__/crossobject-conjunct-refusal.test.ts`,预期全绿,第 ⑨
节能看到卡上的过滤被改写成 `{ cb_order: { status: { $ne: 'cancelled' } } }`。
- 需要你做的:无,席位落地。
## What changes
`ObjectQLStrategy` (the engine-aggregate path, which cannot join) used
to refuse every cross-object `where` leaf with `400 INVALID_FIELD`. It
now serves a **one-hop** dotted leaf in the caller's `where`, which
covers a dataset query's `runtimeFilter`, a dashboard global filter and
a widget filter. The leaf is respelled as the engine's own
nested-relation condition: `{ 'cb_order.status': { $ne: 'cancelled' } }`
becomes `{ cb_order: { status: { $ne: 'cancelled' } } }`.
The engine already serves that form in `where` on every verb
(`relation-filter-lowering.ts`). It works like this:
- It reads the related object through its own `find`, as the caller.
CRUD, row scope and field permissions all apply.
- It then filters the base object with `IN` over the returned ids. That
is the card's "resolve the parent ids first, then `IN`", and the
security rule stays in the one place that owns it.
- It refuses more than 1,000 matching parents (`400 INVALID_FILTER`)
instead of truncating.
- It lowers an empty match to FALSE.
This PR reads no data and judges no permission itself.
- **New:**
`packages/services/service-analytics/src/strategies/cross-object-where.ts`,
the copy-on-write `lowerCrossObjectWhere`.
- It recurses `$and` and `$or`, and it recurses `$not` with the negated
flag set.
- It merges leaves on one relationship in one conjunction into one
condition. The same parent field named twice gives two conditions.
- A base-only `where` comes back as the same object, so it reaches the
engine byte-identical.
- **`objectql-strategy.ts`:** `servedWhere()` runs before the filter
tree is built in both `execute()` and `generateSql()`.
- `planCrossObject` takes the unserved map and refuses each unserved
leaf with a reason-specific message plus the native-SQL route.
- The `/analytics/sql` echo refuses a served leaf `400 INVALID_FILTER`,
as it already does for the authored nested form.
**Semantics, pinned:** a dotted leaf `rel.f OP v` means "the row's `rel`
points at a record the caller can read, and that record satisfies `OP
v`".
- **Orphan or unreadable parent:** never matches, whatever the operator,
`$ne` included. To keep those rows, OR in `{ rel: { $null: true } }`.
- **Inside `$or`:** served.
- **Under `$not`:** refused. Negating the existential test would let the
children of an unreadable parent through. Put the negation inside the
leaf instead.
**Stays refused** (`400 INVALID_FIELD`, `param: 'where'`, each with its
reason and the native route):
- two or more hops (the hop limit is ONE relationship, because the
engine's nested form is one level deep)
- a leaf under `$not`
- a `{ $field }` comparand
- a relationship the cube joins onto a different object than the field
declares
- unchanged: a cross-object **measure**, and a related field in a
dataset's own `filter` or a measure's own `filter`
**Docs:** `content/docs/ui/dashboards.mdx` gets a new "Filtering on a
Related Record's Field" section with the path table, the orphan rule and
its remedy, and what stays refused.
The card's ask 1 (which deployments evaluate the dotted form natively,
and whether multi-hop is in scope there) is answered on the card:
#33335 (comment)
## Why
A dashboard over detail rows could not leave out the rows of a cancelled
parent on every deployment. The native-SQL strategy declines a bucketed
time dimension, a federated object, an engine-middleware object and a
`controlled_by_parent` read scope, and every one of those routes to the
engine-aggregate path, which refused the filter.
## Tests
- **`crossobject-conjunct-refusal.test.ts`**:
- The nested `$or` cases are served and reach the engine with the
relation form. `$not` and `$not`-inside-`$or` stay refused at both
doors.
- Dataset-scope and measure-filter refusals are kept, with a new pin
that a served `where` does not lift the measure filter's refusal.
- New section 9 runs the card's own shape on a `cb_plan` cube: `{
plan_kind: 'recommended', 'cb_order.status': { $ne: 'cancelled' } }`. It
covers the member alias, merge versus two conditions, the FK-expand
dimension beside it, the echo refusal and a base-only control. A REFUSED
table covers two hops, `$not`, `$field`, join-target and a cross-object
measure.
- **Pins repinned from refusal to support:**
`infer-cube-relation-traversal.test.ts`,
`dotted-dimension-unbacked-join-parity.test.ts` and
`where-source-field-gate.test.ts`.
- **Pins that keep their refusal through the `$not` spelling:**
`objectql-read-scope.test.ts`, `unlisted-refusal-envelope.test.ts`, and
in rest `analytics-dataset-unlisted-refusal-envelope.test.ts`.
- **Other rest pins:** `analytics-hop-object-reference.test.ts` now
answers on the engine-aggregate path what the declared join and the
nested form answer. `analytics-masked-field-gate.test.ts` now gets rows
for the joined filter on both compositions.
- **End to end, real security layer**
(`packages/rest/src/analytics-nested-relation-filter.test.ts`, new
#33335 block: `SecurityPlugin`, real `ObjectQL`, SQLite, plus an orphan
row). Each answer equals the engine's own nested-form answer:
- equality
- `$ne` (the orphan is not selected)
- `$ne` OR `{ owner: { $null: true } }` (the orphan is selected)
- inside `$or`
- a hidden parent gives `[]` for the member and its rows for the system
caller
- an empty parent match gives `[]`
- a multi-valued relation
- the dataset door
- an unreadable parent field gives `403 PERMISSION_DENIED`
- more than the cap gives `400 INVALID_FILTER`
- `$not` is refused `400 INVALID_FIELD`
## Acceptance notes
- **The two strategies differ on orphans.** With no row-level scope on
the parent, the native strategy's `LEFT JOIN` evaluates `$ne` over a
NULL parent as unknown, so an orphan is not selected there either. Under
`$ne` the two strategies agree, but for different reasons. Under `$not`
they could differ, which is why `$not` is refused here.
- **Large parent sets** are bounded by the engine's 1,000-id cap and
refused past it (`400 INVALID_FILTER`), never truncated.
- **Still native-only:** the dataset's own `filter` and a measure's own
`filter`. Those are author-time and were not in the card's ask.
- **Read-only observation, not filed (no reach measured):**
`AnalyticsServicePlugin`'s auto-bridged `executeRawSql` maps a
non-array, non-`{rows}` answer from `engine.execute` to `[]`. Only
`null` is typed as `RAW_SQL_UNSUPPORTED`. I did not reproduce this
against a real driver.
## Verification
Final head `689c18223e` (it includes the merge of `origin/main`). Each
block names the tree it read. No `service-analytics` file changed after
`3f40448624`; after that commit only rest tests changed.
<details><summary>service-analytics: test + typecheck (on 3f40448,
the merge of origin/main)</summary>
`bash scripts/pm/os-verify-lock.sh -c "pnpm --filter
@objectstack/service-analytics test && pnpm --filter
@objectstack/service-analytics typecheck && …"` → `Test Files 215 passed
(215)` · `Tests 5213 passed | 332 skipped (5545)` · `tsc --noEmit` clean
· `VERDICT command-exit 0`.
</details>
<details><summary>rest: full suite (a dependent that pins analytics
answers)</summary>
The first full run (`pnpm --filter @objectstack/rest exec vitest run
--maxWorkers=2`) reported `Test Files 1 failed | 415 passed (416)` ·
`Tests 1 failed | 8690 passed | 355 skipped`. The one red was
`analytics-hop-object-reference.test.ts` expecting the old ObjectQL
refusal. It now answers `[2]`, equal to the declared-join reference. I
repinned it. Re-run of the four touched rest files: `Test Files 4 passed
(4)` · `Tests 50 passed (50)` · `VERDICT command-exit 0`.
The other dependents of service-analytics (`runtime`, `cli`, `client`,
`verify`, `dogfood`) are declared to CI. A repo-wide grep finds no pin
of the old refusal text outside `service-analytics` and `rest`.
</details>
<details><summary>Ablations (the tested behaviour changed)</summary>
The `service-analytics` suites import `../strategies/*.js` from source,
so there is no `dist/` step. Each mutation was written with
`scripts/ablation-replace.mjs`, and each restore was proven by the
path's blob hash equalling HEAD's.
- **Respelling disabled** (`servedWhere`'s served-count guard changed to
`>= 0`): 14 reds across `crossobject-conjunct-refusal` and
`infer-cube-relation-traversal`. Restored blob = HEAD `78f2126f`.
- **`$not` refusal disabled** (`if (negated && false)` in
`cross-object-where.ts`): 13 reds across `crossobject-conjunct-refusal`,
`objectql-read-scope` and `unlisted-refusal-envelope`. Restored blob =
HEAD `6af89002`.
</details>
<details><summary>Gates (final head 689c182)</summary>
- `node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack` derived 95 commands, the same list on the
final head as before.
- **Final run on `689c18223e`:** all 95 commands exit 0. `--ran`
reconciliation: "95 derived, 95 run, 0 NOT-MEASURED, 0 UNRUN … a DERIVED
zero — all 95 recorded an exit code and none of them is 3".
- **Earlier run, before the last two commits:**
- `check:skill-examples` exit 3 (`client-react` unbuilt) until it was
built.
- `check:dts-closure` was red because it read `packages/spec/dist`
mid-rebuild by my own concurrent build. Green on the re-run.
- `check:test-source-alias` exit 1 on the edited
`analytics-dataset-unlisted-refusal-envelope.test.ts`. Fixed with a
module-top `import '@objectstack/objectql'`.
- `check:dual-build-cjs-loads` exit 3 (tree unbuilt). Green on the final
run: "123 published require entry point(s) across 72 package(s) load".
- `pnpm check:nul-bytes` exit 0.
- `eslint --no-inline-config` over the 12 changed `.ts` files: exit 0.
Repo-wide `pnpm lint` is CI's.
</details>
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
(`packages/spec` is untouched.)
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; `pnpm lint` passes. (eslint passes over the
changed files; the repo-wide run is CI's.)
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 6. `packages/spec` touched: its generated artifacts are
regenerated and committed. (Not touched.)
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English.
- [x] The *Before Submitting* list in CONTRIBUTING.md is done.
---
_Generated by [Claude
Code](https://claude.ai/code/session_016WV4cwotoQMyrtSr5EqqAF)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 489ef5f commit 498c967
15 files changed
Lines changed: 842 additions & 118 deletions
File tree
- .changeset
- content/docs/ui
- packages
- client/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 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
500 | 500 | | |
501 | 501 | | |
502 | 502 | | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
503 | 539 | | |
504 | 540 | | |
505 | 541 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
504 | 504 | | |
505 | 505 | | |
506 | 506 | | |
507 | | - | |
| 507 | + | |
508 | 508 | | |
509 | 509 | | |
510 | 510 | | |
| |||
790 | 790 | | |
791 | 791 | | |
792 | 792 | | |
793 | | - | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
794 | 796 | | |
795 | 797 | | |
796 | 798 | | |
| |||
855 | 857 | | |
856 | 858 | | |
857 | 859 | | |
858 | | - | |
| 860 | + | |
| 861 | + | |
859 | 862 | | |
860 | 863 | | |
861 | 864 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| |||
256 | 257 | | |
257 | 258 | | |
258 | 259 | | |
259 | | - | |
260 | | - | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
261 | 265 | | |
262 | 266 | | |
263 | 267 | | |
264 | | - | |
| 268 | + | |
265 | 269 | | |
266 | 270 | | |
267 | | - | |
| 271 | + | |
268 | 272 | | |
269 | 273 | | |
270 | 274 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
229 | 229 | | |
230 | 230 | | |
231 | 231 | | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
243 | 239 | | |
244 | 240 | | |
245 | 241 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
347 | 347 | | |
348 | 348 | | |
349 | 349 | | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
356 | 354 | | |
357 | 355 | | |
358 | 356 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
399 | 399 | | |
400 | 400 | | |
401 | 401 | | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
0 commit comments