Skip to content

feat(analytics): serve in-envelope cross-object grouping by FK-expand (#3654) - #3691

Merged
os-zhuang merged 1 commit into
mainfrom
feat/objectql-crossobj-3654-v2
Jul 27, 2026
Merged

feat(analytics): serve in-envelope cross-object grouping by FK-expand (#3654)#3691
os-zhuang merged 1 commit into
mainfrom
feat/objectql-crossobj-3654-v2

Conversation

@os-zhuang

Copy link
Copy Markdown
Contributor

Closes #3654(能力项)。取代 #3672 —— 那个基于旧 main、与 #3651/#3652 冲突;本 PR 在当前 main 上干净重叠加,单 commit。

背景

engine.aggregate() 不会 join,所以 ObjectQL 回退路径(日期分桶 / in-memory / federated)此前拒绝跨对象分组如 revenue by account.region(#3664 止血)。本 PR 直接服务常见情形。

残留 2(generateSql 渲 scope)已由 #3652 在 main 上做掉;第二带子(executeAggregate 携带 context)已由 #3651 做掉。本 PR 只加能力项,并在这两者之上重叠加。

做法(FK 展开)

单跳跨对象维度 + 可重组度量:

  1. 按 lookup FK 列(account)分组跑 base 聚合(引擎能做),加 base 对象 scope + 透传 ExecutionContext(feat(analytics,spec): executeAggregate 桥携带 ExecutionContext —— ADR-0021 D-C 第二层带子 (#3602) #3651 第二带子);
  2. 被引用对象自己的 RLS scope 把 FK id 解析成属性(region);
  3. 内存按属性重并桶,重组度量(sum/count 相加,min/max 取极值)。

受限行归 (restricted)(用户拍板语义):读不到被引用记录的行,度量仍计入、总计守恒,隐藏属性名绝不出现 —— 无泄露(ADR-0021 D-C / #3602 类)。/analytics/sql 渲等价 LEFT JOIN,预览与执行拒同一集。

刻意有界 —— 信封外继续 LOUD 拒绝(绝不静默错答)

跨对象在度量/过滤器(需真 join)、多跳(a.b.c)、avg/count_distinct 配跨对象维。NativeSQLStrategy(正常 SQL 路径)不变。

测试

  • 纯 rebucket(6):重组 sum/count/min/max、restricted 总计守恒、null≠restricted、多维、双跨对象维。
  • 展开集成(3):真实 AnalyticsService + 两次 aggregate stub,断言 base 与 FK 解析都加 scope、restricted 桶成形、/analytics/sql 渲 LEFT JOIN。回退策略 → 三个全红
  • 信封外拒绝(3):跨对象度量 / 过滤 / avg 配跨对象维。
  • 更新 main 的 execution-context-bridge parity 测试:信封内现渲 JOIN,parity 改用信封外度量。
  • service-analytics 242 passed;tsc 无新增错误(剩 1 存量在未触文件)。

🤖 Generated with Claude Code

…#3654)

The ObjectQL fallback path (date-granularity bucketing, in-memory driver,
federated) could not join, so cross-object grouping like
`revenue by account.region` was rejected outright (#3664 stopgap). It now
serves the common case by FK-expand:

1. group the base aggregate on the lookup FK column (`account`) — which
   the engine CAN do — scoped to the base object (and, per #3651's second
   belt, threading the ExecutionContext to the engine too);
2. resolve each FK id to the related attribute (`region`) with a read of
   the referenced object scoped to THAT object's own RLS;
3. re-bucket by the resolved attribute in memory, recombining measures
   (sum/count add, min/max take the extremum).

A base row whose referenced record the caller cannot read buckets under
an explicit `(restricted)` group: the measure still counts (grand totals
preserved) but the hidden attribute never appears — no leak (ADR-0021
D-C / #3602). `/analytics/sql` renders the equivalent LEFT JOIN, so
preview and execution accept/reject the same set.

Bounded — still rejected LOUD (never silently wrong): cross-object in a
MEASURE or FILTER, multi-hop dims, and non-recombinable measures
(avg/count_distinct) with a cross-object dim. NativeSQLStrategy is
unchanged.

The pure re-bucketing step is isolated in cross-object-rebucket.ts and
exhaustively unit-tested (recombination, restricted-total conservation,
null-vs-restricted, multi-dim). Integration tests drive the real
AnalyticsService with a two-call aggregate stub, asserting both the base
and FK-resolution are scoped and the restricted bucket forms; reverting
the strategy turns all three red.

Re-applied cleanly on top of #3651/#3652 (executeAggregate carries
context; generateSql renders scope) after those landed on main.

Co-Authored-By: Claude <noreply@anthropic.com>
@vercel

vercel Bot commented Jul 27, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
objectstack Ignored Ignored Jul 27, 2026 2:33pm

Request Review

@github-actions

Copy link
Copy Markdown
Contributor

📓 Docs Drift Check

This PR changes 1 package(s): packages/services.

6 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:

  • content/docs/automation/webhooks.mdx (via packages/services)
  • content/docs/kernel/runtime-services/audit-service.mdx (via packages/services)
  • content/docs/kernel/runtime-services/index.mdx (via packages/services)
  • content/docs/kernel/runtime-services/settings-service.mdx (via packages/services)
  • content/docs/plugins/packages.mdx (via packages/services)
  • content/docs/protocol/kernel/i18n-standard.mdx (via packages/services)

Advisory only. To re-verify, run the docs-accuracy-audit workflow scoped to these files:
node scripts/docs-audit/affected-docs.mjs origin/main → pass the list as args.docs.

@os-zhuang
os-zhuang merged commit fc5f126 into main Jul 27, 2026
16 checks passed
@os-zhuang
os-zhuang deleted the feat/objectql-crossobj-3654-v2 branch July 27, 2026 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

analytics/objectql: 跨对象(点号 lookup)维度聚合在 ObjectQL 路径上静默塌成 (null) 桶 / native SQL 报错

1 participant