feat(analytics): serve in-envelope cross-object grouping by FK-expand (#3654) - #3691
Merged
Conversation
…#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>
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
Contributor
📓 Docs Drift CheckThis PR changes 1 package(s): 6 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 展开)
单跳跨对象维度 + 可重组度量:
account)分组跑 base 聚合(引擎能做),加 base 对象 scope + 透传 ExecutionContext(feat(analytics,spec): executeAggregate 桥携带 ExecutionContext —— ADR-0021 D-C 第二层带子 (#3602) #3651 第二带子);region);受限行归
(restricted)桶(用户拍板语义):读不到被引用记录的行,度量仍计入、总计守恒,隐藏属性名绝不出现 —— 无泄露(ADR-0021 D-C / #3602 类)。/analytics/sql渲等价LEFT JOIN,预览与执行拒同一集。刻意有界 —— 信封外继续 LOUD 拒绝(绝不静默错答)
跨对象在度量/过滤器(需真 join)、多跳(
a.b.c)、avg/count_distinct 配跨对象维。NativeSQLStrategy(正常 SQL 路径)不变。测试
AnalyticsService+ 两次 aggregate stub,断言 base 与 FK 解析都加 scope、restricted 桶成形、/analytics/sql渲 LEFT JOIN。回退策略 → 三个全红。execution-context-bridgeparity 测试:信封内现渲 JOIN,parity 改用信封外度量。tsc无新增错误(剩 1 存量在未触文件)。🤖 Generated with Claude Code