Repository navigation
Commit 8882857
fix(service-analytics): a live dataset query serves a derived measure built on another derived measure, and refuses a derived cycle with its path (#28169)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} -->
_Requested by **Elon** · [project
thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DQcUJMSg3N7pTvoXMtHAZbF)_
domain:server#4 · os-elon-musk · session_016WV4cyJ2FnmRRSXEUtT6G1
Fixes #28127
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 改了什么:数据集查询里,派生度量的 `of` 引用另一个派生度量(派生套派生)时,执行器现在沿 `of`
一路展开到聚合度量,按依赖先后计算;环路(`loop_a → loop_b → loop_a`)在读任何数据前以
`DATASET_INVALID` / 400 拒绝,消息写出环路路径。
- 为什么改:实测 main 上实时查询(native 与 ObjectQL 两策略)把派生操作数当成基础字段,拒为
`INVALID_FIELD`;草稿预览不是“能答”,而是把它当行计数,答出看似合理的错数(应为 150,答 1),环路也答出数字。
- 风险与回滚:只改 `DatasetExecutor.executeSelection`
的度量展开;预览与实时共用同一执行器,所以一处修复两边一致。普通派生度量值与列不变(对照钉子)。回滚即 revert 本 PR。
- 先看:`packages/services/service-analytics/src/dataset-executor.ts` 的
`expandSelectedMeasures`。
- 两分钟验证:`pnpm --filter @objectstack/service-analytics exec vitest run
src/__tests__/derived-of-derived-measure.test.ts`,预期 16 passed。
- 需要你做的:无,席位落地。环路在预览上由“答出错数”改为拒绝,是否需契约复审由席位定。
## What changes
- **`packages/services/service-analytics/src/dataset-executor.ts`.** New
exported `expandSelectedMeasures(measures, derived)`: the selected
non-derived measures, then a depth-first walk of each selected derived
measure's `of`, which adds aggregated operands to the base set and
returns the derived measures dependencies first (each once). A derived
measure that reaches itself is refused `DATASET_INVALID` / 400
(`datasetInvalidError`, an existing code) with the cycle's path, before
any query. `executeSelection` calls it in place of the one-level
expansion; the derived pass, the `totals` re-entry and the single-query
test read its result unchanged. For a derived measure whose operands are
all aggregated, the base set and derived order are what they were.
-
**`packages/services/service-analytics/src/__tests__/derived-of-derived-measure.test.ts`**
(new). On the native strategy, the ObjectQL strategy and the draft
preview, over a real SQLite engine: two levels served with values; three
levels served, with a `totals` grand total; the cycle and a measure
built on it refused `DATASET_INVALID` / 400 with `loop_a → loop_b →
loop_a` and zero reads; CONTROL, a plain derived measure keeps its value
and its columns. Plus one pure cell on `expandSelectedMeasures`
(dependency order, a shared operand once, base order).
- **`.changeset/28127-derived-of-derived-measure.md`.**
`@objectstack/service-analytics` patch.
## Why
### Before (measured at `origin/main` `062a2323`)
Dataset over `amount` with `total` (sum), `deals` (count), `total_twice`
= derived sum of `[total, total]`, `twice_per_deal` = derived ratio of
`[total_twice, deals]`; rows amer 100 + 50, emea 30. Selecting
`twice_per_deal` by region:
| Face | Before | After |
|:---|:---|:---|
| native | refused `INVALID_FIELD` / 400: "Measure 'total_twice' on cube
'os28127_ds' aggregates field 'total_twice', which object 'os28127_deal'
does not have" | amer 150, emea 60 |
| ObjectQL | same refusal | amer 150, emea 60 |
| draft preview | served amer **1**, emea **1** (`total_twice` answered
as a row count: 2 and 1) | amer 150, emea 60 |
A cycle (`loop_a` = sum of `[loop_b, total]`, `loop_b` = sum of
`[loop_a, total]`; `DatasetSchema` parses it, refusing only the one-step
self-reference), selecting `loop_a`: native and ObjectQL refused
`INVALID_FIELD` naming `loop_b` as a missing field; the draft preview
served amer 152, emea 31. After: `DATASET_INVALID` / 400 on all three,
"derived measure "loop_a" depends on itself: loop_a → loop_b → loop_a",
before any read.
### Why this path
The card read the draft preview as expanding derived-of-derived. It does
not: the preview runs the same `DatasetExecutor` over seed rows
(`analytics-service.ts`, the ADR-0037 P3 branch), and its in-memory
evaluator answers a measure the cube does not declare as `count(*)`. So
there was no preview expansion to share; the one fix in the shared
executor serves both faces, and the preview's wrong number goes with it.
### Clause-②
`no`: `DatasetSchema` accepts a derived measure over a derived measure,
and `content/docs/data-modeling/currency.mdx` describes one ("A derived
`sum` or `difference` over one `fixed` code carries that code, so a
measure built on it is judged the same way"); the live refusal
contradicted that text. The cycle refusal reaches a selection no face
answered correctly: the live faces already refused it (the code moves
from `INVALID_FIELD` to `DATASET_INVALID`, both existing), and the draft
preview served a meaningless number, which it now refuses.
`Metadata-protocol: none`: nothing an author can write changes.
## Verification
- New pins red on `origin/main` (fix reverted, test kept): 13 of 16
failed; green there: the three CONTROL cells. Green after: 16 of 16.
- Ablations on the committed head (`scripts/ablation-replace.mjs`, each
restored to the HEAD blob with `git diff HEAD` empty): derived operand
added to the base set instead of walked → 13 failed; cycle check
disabled → the 6 cycle cells fail (stack overflow instead of the
refusal); derived order reversed → the 6 value cells and the pure cell
fail; aggregated operands dropped → the 3 CONTROL cells, the 6 value
cells and the pure cell fail.
- `@objectstack/service-analytics` full suite: 197 files, 4775 passed /
282 skipped. `tsc --noEmit`: exit 0.
- `dispatch-gates --commands` derived 59 families on the committed head
`925343a7`, run after a whole-tree `turbo run build` and fetching the
teardown self-test's pinned commit: all 59 exit 0. `--ran`: 59 derived,
59 run, 0 NOT-MEASURED. `pnpm lint`: exit 0.
## Acceptance notes
- **Out of scope, not changed:** the draft preview's in-memory evaluator
(`evaluateAnalyticsQueryOverRows`) answers a measure the cube does not
declare as `count(*)` where the live faces refuse it `INVALID_FIELD`.
Measured only through the derived operand this PR stops sending; whether
another caller reaches it was not measured.
- A derived measure's intermediate derived operands appear as columns of
the result, as its aggregated operands already did.
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
(Not touched.)
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; `pnpm lint` passes.
- [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: 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 (the 维护者速读 block is the
repo's own Chinese reader section).
- [x] The *Before Submitting* list in CONTRIBUTING.md is done.
---
_Generated by [Claude
Code](https://claude.ai/code/session_016WV4cyJ2FnmRRSXEUtT6G1)_
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_016WV4cyJ2FnmRRSXEUtT6G1
Co-authored-by: Claude <noreply@anthropic.com>1 parent f9a1363 commit 8882857
3 files changed
Lines changed: 284 additions & 13 deletions
File tree
- .changeset
- packages/services/service-analytics/src
- __tests__
| 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 | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 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 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
194 | 251 | | |
195 | 252 | | |
196 | 253 | | |
| |||
1215 | 1272 | | |
1216 | 1273 | | |
1217 | 1274 | | |
1218 | | - | |
1219 | | - | |
1220 | | - | |
1221 | | - | |
1222 | | - | |
1223 | | - | |
1224 | | - | |
1225 | | - | |
1226 | | - | |
1227 | | - | |
1228 | | - | |
1229 | | - | |
1230 | | - | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
1231 | 1278 | | |
1232 | 1279 | | |
1233 | 1280 | | |
| |||
0 commit comments