Repository navigation
Commit c59ee99
feat(objectql,service-analytics): refuse a raw aggregate over a record-mode quantity unless grouped by its unit field (#36060)
repo:objectstack#7 · os-tesla · session_01L9qibvMi1U7R2kBd3JbTPu
Fixes #35491
A total over a quantity whose unit is stored per row (`unitConfig: {
unitMode: 'record', unitField }`) is now refused unless it groups by
that unit field. Before, 10 KGM plus 10 LBR answered 20 in the engine
aggregate, a dataset or cube measure, and a `summary` roll-up. This is
ADR-0158 U7 R1, built as a one-for-one mirror of the ADR-0144 D6
record-currency doors.
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:按单位逐行记录的数量(如一行 10 千克、一行 10
磅),在报表、数据集和汇总字段里不按单位分组直接求和、求平均、取最大最小时,会得到明确的拒绝提示,而不是一个错误的总数;按单位分组后照常出数。
- 为什么做:属于“计量单位”第一期,直接相加不同单位的数量会得出看似合理却错误的数字,这里与多币种的同类保护保持一致。
- 出错会怎样:目前还没有任何应用或示例使用这种按行记录单位的数量,影响面为零;如有问题,撤回这次合并即可恢复原样。
- 要你做什么:无。
## What changes
- **ObjectQL aggregate door**
(`packages/objectql/src/record-unit-aggregate-door.ts`, wired in
`ObjectQL.aggregate` right after the record-currency door): `sum` /
`avg` / `min` / `max` over a `record`-mode quantity answers
`INVALID_FIELD` / 400 before any driver is asked, unless `groupBy`
includes the quantity's `unitField`. The message names the quantity, its
unit field and the grouping to add; the thrown error carries `field`,
`fields`, `object`, `param: 'aggregations'` and `unitField` (in process
only: the `/data` REST body keeps `field` and `object` and drops
`unitField`, measured below).
- **`summary` registration** (`recordUnitSummaryRefusal`, in
`SchemaRegistry.registerObject` beside `recordCurrencySummaryRefusal`):
a roll-up of `sum` / `avg` / `min` / `max` over such a child quantity
answers `INVALID_METADATA` / 422 (`RecordUnitSummaryRefusedError`),
judged at whichever registration completes the pair. Per ADR-0158 L3
every such roll-up is refused in R1 (no `master_detail` following), so
the roll-up recompute gets no bypass channel: the currency door needs
`ROLLUP_AGGREGATE_OPTION` only for its admitted roll-up, and units admit
none.
- **Analytics cube measure door**
(`packages/services/service-analytics/src/cube-record-unit-door.ts`,
asked right after the record-currency door at all four places that ask
it: the inferred, augmented and declared `ensureCube` paths and the
draft-preview branch of `queryDataset`): `INVALID_FIELD` / 400, the
thrown error carrying `member`, `param: 'measures'`, `field`, `object`
and `unitField` (in process only: the analytics REST body is `code` and
`message`), unless a dimension's column is the unit field on the same
object. The plugin's `sourceFieldMeta` relays `unitField` only for a
`number` under `unitMode: 'record'`. The dimension resolver both doors
share moved into one private method (`groupedColumnOf`), with no change
to what it answers.
- **The widening.** `AnalyticsServiceConfig.sourceFieldMeta` may answer
one more optional key, `unitField`: a `number` field's
`unitConfig.unitField`, under `unitMode: 'record'` only.
`AnalyticsServicePlugin` relays it for a `number` under `unitMode:
'record'` and for nothing else. A host that does not answer it gets no
record-unit judgment from the cube door. The precedent is a35fc66,
`homeAmountField` on the same answer. The errors' `unitField` member is
not part of the widening: no REST envelope carries it.
- Two ADR anchors (new files only) and a patch changeset for
`@objectstack/objectql` and `@objectstack/service-analytics`.
**The raw-aggregate set, as built** (ADR-0158 Open item 12, the
aggregate half; carried to the ADR text by the weekly ADR revisions card
#36086, whose row lands after this PR merges): `sum`, `avg`, `min` and
`max` are raw. `count` reads no quantity and `count_distinct` adds
nothing, so neither is judged. Grouping by the quantity's own
`unitField` admits the aggregate. A `fixed`-mode quantity and a `number`
field with no `unitConfig` pass unchanged. No new error code:
`INVALID_FIELD` and `INVALID_METADATA`, as ADR-0144 assigned for
currency.
## Why
ADR-0158 U7 R1: "a build-time refusal of a raw aggregate over a
`record`-mode quantity unless it groups by that quantity's unit field
(mirrors ADR-0144 D6)". L3: "In R1, a `summary` over `record`-mode child
quantities is therefore always refused, as every such currency roll-up
was before ADR-0144's D6 amendment".
## Tests
Read first (by risk):
1. `packages/objectql/src/engine-delete-and-aggregate.ts`: the one new
call, after the record-currency door.
2. `packages/objectql/src/registry-object-registration.ts`: the summary
refusal beside the currency one.
3. `packages/services/service-analytics/src/analytics-service.ts`: the
four call sites and the extracted `groupedColumnOf`.
Two-minute check: `pnpm --filter @objectstack/objectql exec vitest run
src/record-unit-aggregate-door.test.ts` shows 8 passed; `pnpm --filter
@objectstack/service-analytics exec vitest run
src/__tests__/record-unit-analytics.test.ts` shows 23 passed.
- `packages/objectql/src/record-unit-aggregate-door.test.ts` (8):
ungrouped `sum`/`avg`/`min`/`max` refused with code, status, field, unit
field and the remedy inside the first 500 characters; grouped by another
field refused; grouped by the unit field answers `KGM [15, 7.5, 5, 10]`,
`LBR [10, 10, 10, 10]`; `count` 3, `count_distinct` 2, fixed-unit `sum`
11 and `max` 6, plain `sum` 6 and `avg` 2; the summary refused at both
registration orders and over itself, naming roll-up, quantity and unit
field; `count` over the quantity, `sum` over the fixed quantity and the
plain number register in both orders and roll up 2, 9 and 5.
-
`packages/services/service-analytics/src/__tests__/record-unit-analytics.test.ts`
(23, SqlDriver over better-sqlite3, both strategies): grouped values per
unit; a totals grouping keeping the unit; refusals (another dimension
only, each raw aggregate ungrouped, a grand total) with no read of the
object; `count`, `count_distinct`, fixed-unit and plain measures served
ungrouped with values; the cube face; the inferred cube refused and then
served grouped; the draft preview refused and served grouped.
<details><summary>Before and after, measured on a real engine (ObjectQL
over SqlDriver / better-sqlite3)</summary>
The four wiring files restored to `origin/main` for the before run; the
probe is a throwaway file, not committed.
| Probe | Before | After |
|---|---|---|
| `aggregate sum(qty)`, rows 10 KGM and 10 LBR | `20` | `INVALID_FIELD`
/ 400 |
| `avg` / `min` / `max` | `10` / `10` / `10` | `INVALID_FIELD` / 400
each |
| `count(qty)` | `2` | `2` |
| `sum(qty)` grouped by `unit_code` | KGM 10, LBR 10 | KGM 10, LBR 10 |
| `summary` sum over child rows 10 KGM, 10 LBR | registered, stored `20`
| `INVALID_METADATA` / 422 at registration |
| dataset `total` ungrouped | `20` | `INVALID_FIELD` / 400 |
| dataset `total` by unit | KGM 10, LBR 10 | KGM 10, LBR 10 |
| inferred cube `qty_sum` | `20` | `INVALID_FIELD` / 400 |
</details>
<details><summary>Ablation, one per door (committed first;
scripts/ablation-replace.mjs; restore proven by blob equality)</summary>
The suites resolve `@objectstack/objectql` and the service to `src` (the
service-analytics vitest alias), so no `dist` rebuild sits between
mutation and run.
- Aggregate door: delete the `assertRecordUnitAggregatesGrouped(...)`
call in `engine-delete-and-aggregate.ts`: 2 failed / 6 passed (the two
refusal pins); restored, blob `691b2595e09a` == HEAD.
- Summary door: delete `if (recordUnitSummary) return { refusal:
recordUnitSummary };`: 3 failed / 5 passed (both orders and the self
roll-up); restored, blob `fa4c14cb923c` == HEAD.
- Cube door: make the unit arm skip every measure (`if (unitField ===
undefined || unitField !== undefined) continue;`): 17 failed / 6 passed
(every refusal pin on both strategies, the cube face, the inferred cube
and the draft preview); restored, blob `dad0d024e94b` == HEAD.
</details>
## Acceptance notes
- **Dataset compile, mirrored as currency does it.** Currency's
`DATASET_INVALID` compile guards (`measure-currency-coherence.ts`) judge
a measure's `currency` key and derived measures across currencies; a
dataset that declares an ungrouped `sum` over a record-mode amount
compiles, and the refusal rides each query selection through the cube
door. Units mirror that: a dataset measure over a record-mode quantity
compiles, and every selection that does not group by the unit field is
refused `INVALID_FIELD`. A dataset measure has no `unit` key, so guard 1
has no unit twin. A derived measure combining a record-mode quantity
with another quantity is not a raw aggregate and is not judged here (no
ruling covers it).
- **Two-segment `unitField`.** The object door refuses a dotted
`unitField`, so no quantity follows a parent's unit and this door has no
path arm (the currency door's `MASTER_DETAIL.CODE` arm has no twin).
- **Repo-wide sweep.** No pin asserted a raw total over a `record`-mode
quantity (grep of `unitMode: 'record'` repo-wide: no aggregate,
`summary` or dataset measure over one outside this PR's own tests). No
`examples/**`, `packages/**/fixtures` or `packages/**/__fixtures__` file
declares `unitConfig`, so no example build is broken by the refusal.
- **Declaration basis.** `unitConfig` is not in any released CHANGELOG
(`packages/spec/CHANGELOG.md` and `packages/objectql/CHANGELOG.md` at
18.6.0 carry no `unitConfig`; its changeset `35489-unit-config.md` is
pending), so the refusal narrows nothing published: `Metadata-protocol:
none`. The one published-surface change is the optional
`sourceFieldMeta` key above, so `Clause-②: yes (widening)`.
- **`unitField` on the wire, measured.** A throwaway probe in
`packages/rest` (not committed) threw each door's real error into the
real REST writers. `/data` (`mapDataError`): 400, body keys `error`,
`code`, `field`, `object`; the INVALID_FIELD arm copies `field` and
`object` only (`packages/types/src/data-error-classification.ts` lines
1308 and 1309). `POST /analytics/dataset/query`: 400, body keys `code`,
`message`; the declared-4xx arm writes `code`, `message` and an optional
`userMessage` only (`packages/rest/src/analytics-routes.ts` line 926).
The same holds for the currency twin's `member`, `field` and `object` on
the analytics door.
- UI whole-set summary over a record-mode quantity: #36082
- Dataset compile guard (both families) and derived-measure twin: #36083
- ADR-0158 amendment with the raw-aggregate set (the card's last
Done-when clause): row on #36086, prerequisite this PR merged.
## Verification
Suites, typecheck and gate readings at head `f01bd3db4e`, on
`origin/main` `8145424b53`. Head `948f998319` adds a changeset-only
commit (the widening declaration), re-checked below. Changed lines: 1043
(+1032 / -11, 11 files), measured by `dispatch-gates`.
<details><summary>Suites and typecheck</summary>
- `pnpm --filter @objectstack/objectql test`: Test Files 625 passed
(625), Tests 14223 passed (14223); exit 0.
- `pnpm --filter @objectstack/service-analytics test`: Test Files 222
passed (222), Tests 5674 passed, 345 skipped (6019); exit 0.
- `pnpm --filter @objectstack/objectql run typecheck` (tsc plus
`check:test-typecheck`, OK, debt unchanged) and `pnpm --filter
@objectstack/service-analytics run typecheck`: lock VERDICT command-exit
0. Both new test files are in the compiled program (`--listFiles`).
- eslint over the eight changed source and test files: exit 0 (the
repo-wide `pnpm lint` is CI's).
</details>
<details><summary>Derived gate families</summary>
- `node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack`: 84 families, each run with its exit
captured before any pipe. 83 exit 0 on the first pass;
`check:dual-build-cjs-loads` exited 3 (PREREQUISITE NOT MET, no
workspace dist). After `turbo run build --filter='./packages/**'` (81/81
tasks), its re-run exits 0: 124 entry points across 72 packages load.
- `--ran` reconciliation over the re-run record: "84 derived, 84 run, 0
NOT-MEASURED, 0 UNRUN".
- At `948f998319`: `node scripts/check-changeset-no-major.mjs --base
origin/main --event` (this body as the payload): exit 0, "clause-② `yes
(widening)` and metadata-protocol `none` … a non-protocol change at
`patch`"; `check-empty-changeset` and `check-adr-0087-registration` exit
0.
- `pnpm check:adr-anchors`: exit 0 with the two new anchors.
</details>
CI carries the full farm, `test:repo` (`Test Core`) and the dogfood
battery (no `!` in the title and no `(narrowing)` arm, so no local full
dogfood run is owed).
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; `pnpm lint` passes (CI).
- [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` 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.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01L9qibvMi1U7R2kBd3JbTPu
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 886ba45 commit c59ee99
11 files changed
Lines changed: 1033 additions & 11 deletions
File tree
- .changeset
- packages
- objectql/src
- services/service-analytics/src
- __tests__
- scripts/adr-anchors
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| 67 | + | |
67 | 68 | | |
68 | 69 | | |
69 | 70 | | |
| |||
1096 | 1097 | | |
1097 | 1098 | | |
1098 | 1099 | | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
1099 | 1104 | | |
1100 | 1105 | | |
1101 | 1106 | | |
| |||
| 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 | + | |
| 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 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
0 commit comments