Skip to content

Commit 4159c70

Browse files
fix(service-analytics)!: the dataset draft preview refuses a dimension whose field the object does not have, as the live strategies do, instead of grouping every row under one null key (#28259)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} --> _Requested by **Elon** · [project thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DLMAJ9kZMBicdMLygftCdLd)_ domain:server#3 · os-elon-musk · session_016WV4cyQNVxpxREGfKkTsqs Fixes #28201 Clause-②: no (narrowing) — a draft-preview dataset query (`queryDataset` with `previewDrafts: true` over a pending seed draft) now refuses a selected dimension that the native and ObjectQL strategies refuse, which it served as one `null` group before. No export, answer member, status or error code is added: the refusal is the live faces' existing `INVALID_FIELD` / 400. Metadata-protocol: none ## 维护者速读 - 改了什么:数据集草稿预览遇到对象上不存在的维度(如拼错的 `bogus`),过去把所有行归到一个 `null` 组并给出总数;现在与实时两种策略一样按 400 `INVALID_FIELD` 拒,消息逐字相同。同时把预览各道闸门的顺序对齐实时,两处错误同时出现时三面报出同一个错误。 - 为什么改:草稿里的错别字显示一个空白分组加总数,发布后才被拒;这是 #28172(度量那一半,PR #28210 已合入)的维度那一半。 - 风险与回滚:只影响草稿预览;已声明的维度和用对象真实字段名的未声明维度(如 `amount`)照旧分组,实时策略不变。回滚即 revert。 - 先看:`packages/services/service-analytics/src/analytics-service.ts` 里 `answerDataset` 的 `previewService.query`(新增 `assertDimensionFields` 与闸门顺序)。 - 两分钟验证:`pnpm --filter @objectstack/service-analytics exec vitest run src/__tests__/preview-undeclared-dimension-parity.test.ts`,预期 7 passed。 - 需要你做的:无,席位落地。 ## What changes **The draft preview's gate list (`analytics-service.ts`, `answerDataset` → `previewService.query`).** - It now asks `assertDimensionFields` (#5520), the gate `ensureCube` asks on the live path, on the cube with the minted measures and with the compiled cube's declared dimensions as the vocabulary. A dimension whose field the object does not have (`bogus`) is refused `INVALID_FIELD` / 400 with the same message, `param: 'dimensions'`, `field` and `dimension` named, `Valid dimensions:` listing the declared ones. An undeclared dimension naming a real field stays legal, as the gate's own contract says, so `amount` still groups on every face. - Its gates now run in the live order, which #28210's contract review noted as a non-blocking difference: `ensureCube`'s order (measure fields #4437, dimension fields #5520, aggregate × field-type #21044, record currency ADR-0144 D6), then `query()`'s (caller-measure column reference #21156, stored body #21120, internal fields, field-level read #20917). The order change is required for parity, not only tidiness: with `region_sum` beside `bogus` the live faces answer the dimension 400 first, and with `amount + 1` beside `region_sum` they answer the field-type 400 before the column-reference 403. Before this PR the preview answered the field-type 400 and the 403 respectively. Both cases are pinned. `preview-evaluator.ts` is unchanged: an undeclared dimension over a real field is legal, and the evaluator has no field list to judge it by; the service gate above does. **Not in this PR (measured here, carded by the seat):** a `runtimeFilter` on a field the object does not have is refused `INVALID_FIELD` / 400 (`param: 'where'`) live and served as `[]` on the preview (#28257, which also notes the unmeasured #20807 JSON-dimension door); a dotted dimension `<dataset>.bogus` is refused live with two different refusals (native `DATASET_INVALID`, ObjectQL `INVALID_FIELD`) and served on the preview, which needs a ruling first (#28258). ## Why Measured on `main` `86cf7cef`, real SQLite engine, a dataset declaring dimension `region` and measures `total` (sum of `amount`), `deals` (count): | selection | native | ObjectQL | draft preview (before) | draft preview (after) | |:---|:---|:---|:---|:---| | dims `['bogus']` | `INVALID_FIELD` / 400 | `INVALID_FIELD` / 400 | `[{bogus: null, total: 180}]` | `INVALID_FIELD` / 400, same message | | dims `['bogus', 'region']` | `INVALID_FIELD` / 400 | `INVALID_FIELD` / 400 | two groups with `bogus: null` | `INVALID_FIELD` / 400, same message | | dims `['amount']` (real field) | three groups | three groups | three groups | three groups | | dims `['region']` (declared) | two groups | two groups | two groups | two groups | | `region_sum` + dims `['bogus']` | dimension 400 | dimension 400 | field-type 400 | dimension 400 | | `['amount + 1', 'region_sum']` | field-type 400 | field-type 400 | column-reference 403 | field-type 400 | ## Verification - `packages/services/service-analytics/src/__tests__/preview-undeclared-dimension-parity.test.ts` (new, 7 cases, each over native / ObjectQL / preview through `queryDataset` on real SQLite): `bogus` refused `INVALID_FIELD` / 400 with `param`, `field`, `dimension` and a message identical on all three faces; `bogus` beside a declared dimension, same; CONTROLS: `region` and `amount` serve the same groups on all three; three two-mistake cases answer the same refusal first on all three (`nope` + `bogus` → the measure; `region_sum` + `bogus` → the dimension; `amount + 1` + `region_sum` → the field-type door). - Ablations (source changed, both parity files rerun, 17 cases): `analytics-service.ts` reset to `main` ⇒ 4 red (both `bogus` cases and the two order cases that moved); `assertDimensionFields` removed ⇒ 3 red (both `bogus` cases, `region_sum` + `bogus`); the field-type door moved ahead of the dimension gate ⇒ 1 red (`region_sum` + `bogus`); the caller-measure gate moved back ahead of the field-type door ⇒ 1 red (`amount + 1` + `region_sum`). The #28172 measure pins stay green in every arm but the reset. - `pnpm --filter @objectstack/service-analytics exec vitest run` at head: 199 files, 4795 passed, 282 skipped, 0 failed. `pnpm --filter @objectstack/service-analytics typecheck` and eslint on the touched files clean. - Every `dispatch-gates --commands` family, `pnpm lint` and the dogfood battery run at this head: the readings are posted on the card with the os-dev report. Skills-docs: none. `git grep -n -i -E "previewDrafts|draft (data )?preview" -- skills content/docs` (references excluded) finds `content/docs/data-modeling/currency.mdx:146` (still true) and release notes; nothing in `skills/` or `content/docs/` describes how the preview answers an undeclared dimension. ## 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. (No formatter ran; the lint reading is posted on the card with the gate battery.) - [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 lane's maintainer summary). --- _Generated by [Claude Code](https://claude.ai/code/session_016WV4cyQNVxpxREGfKkTsqs)_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_016WV4cyQNVxpxREGfKkTsqs Co-authored-by: Claude <noreply@anthropic.com>
1 parent 0027c4a commit 4159c70

3 files changed

Lines changed: 214 additions & 9 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
'@objectstack/service-analytics': patch
3+
---
4+
5+
fix(service-analytics)!: the dataset draft preview refuses a dimension whose field the object does not have, as the live strategies do, instead of grouping every row under one null key
6+
7+
Clause-②: no (narrowing) — a draft-preview dataset query (`queryDataset` with `previewDrafts: true` over a pending seed draft) now refuses a selected dimension that the native and ObjectQL strategies refuse, which it served as one `null` group before. Nothing is widened: no export, answer member, error code or status is added.
8+
Metadata-protocol: none
9+
10+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export or type changes: the draft preview now answers an undeclared dimension the way the published dataset already answers it. The refusal is the live faces' existing one, so there is nothing for a migration to rewrite and no tombstone. -->
11+
12+
**BREAKING** accept-set narrowing on the draft preview of `queryDataset` (`@objectstack/service-analytics`).
13+
14+
- **Before.** The preview grouped by a dimension the dataset does not declare without asking whether the object has its field. A misspelled `bogus` answered one group, `{ bogus: null }`, carrying the whole total, where both live strategies refuse it `INVALID_FIELD` / 400 ("Dimension 'bogus' on cube … groups by field 'bogus', which object … does not have. Valid dimensions: …").
15+
- **Now.** The preview asks the live strategies' dimension gate before it reads a seed row, so `bogus` is refused `INVALID_FIELD` / 400 with the same message, alone or beside a declared dimension.
16+
- **Gate order.** The preview now asks its gates in the live order: the measure-field gate, the dimension-field gate, the aggregate × field-type door and the record-currency door (`ensureCube`'s order), then the caller-measure column-reference gate, the stored-body, internal-field and field-level read gates (`query()`'s order). So a query with two mistakes is answered about the same one first on every face. Before, the preview asked the column-reference gate (403) ahead of the field-type (400) and currency doors, so `['amount + 1', 'region_sum']` answered 403 on the preview and 400 live.
17+
- **Unchanged.** A declared dimension, and an undeclared one naming a field the object has (`amount`), group as before on every face. The live strategies are unchanged.
18+
- **Who sees it.** A Studio author whose draft widget groups by a dimension name the object has no field for: the typo now shows the refusal it meets once published, instead of a single blank group with the grand total.
19+
20+
**The remedy.** Group by a dimension the dataset declares, or by one of the object's own field names.
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#28201] A dimension the dataset does not declare, through `queryDataset` on
5+
* the native strategy, the ObjectQL strategy and the draft preview.
6+
*
7+
* ## What these pins hold
8+
*
9+
* - A dimension whose field the object does not have (`bogus`) is refused
10+
* `INVALID_FIELD` / 400 on every face, with the same message naming the
11+
* member and the valid dimensions, alone or beside a declared dimension.
12+
* Before, the preview grouped every row under one `null` key and served the
13+
* total.
14+
* - CONTROL: a declared dimension and an undeclared one naming a real field
15+
* (`amount`) serve the same groups on every face.
16+
* - The preview asks its gates in the live order, so a query with two mistakes
17+
* is answered about the same one first on every face: a measure field the
18+
* object does not have before a bogus dimension, a bogus dimension before
19+
* the field-type door, and the field-type door before the column-reference
20+
* gate.
21+
*/
22+
23+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
24+
import { ObjectQL } from '@objectstack/objectql';
25+
import { SqlDriver } from '@objectstack/driver-sql';
26+
import { DatasetSchema } from '@objectstack/spec/ui';
27+
import type { AnalyticsService } from '../analytics-service.js';
28+
import { AnalyticsServicePlugin } from '../plugin.js';
29+
import { installDefaultOrganization } from './default-organization.testkit.js';
30+
31+
const OBJECT = 'os28201_deal';
32+
33+
const ROWS = [
34+
{ id: 'd1', region: 'amer', amount: 100 },
35+
{ id: 'd2', region: 'amer', amount: 50 },
36+
{ id: 'd3', region: 'emea', amount: 30 },
37+
] as const;
38+
39+
const DATASET = DatasetSchema.parse({
40+
name: 'os28201_deals',
41+
label: 'Deals',
42+
object: OBJECT,
43+
dimensions: [{ name: 'region', field: 'region', type: 'string' }],
44+
measures: [
45+
{ name: 'total', aggregate: 'sum', field: 'amount' },
46+
{ name: 'deals', aggregate: 'count' },
47+
],
48+
});
49+
50+
type Face = 'native' | 'objectql' | 'preview';
51+
const FACES: readonly Face[] = ['native', 'objectql', 'preview'];
52+
53+
const quiet = { debug() {}, info() {}, warn() {}, error() {}, trace() {}, fatal() {}, child() { return quiet; } };
54+
55+
type Refusal = Error & { code?: string; status?: number; param?: string; field?: string; dimension?: string };
56+
57+
const refusalOf = (p: Promise<unknown>): Promise<Refusal> =>
58+
p.then<never, Refusal>(
59+
() => {
60+
throw new Error('expected a refusal, but the query resolved');
61+
},
62+
(e) => e as Refusal,
63+
);
64+
65+
/** A `'protocol'` service answering one pending seed draft, which routes `previewDrafts` onto the preview path. */
66+
const pendingSeedProtocol = {
67+
getMetaItems: async () => [{ name: 'os28201_seed', object: OBJECT }],
68+
getMetaItem: async () => ({ item: { records: ROWS } }),
69+
};
70+
71+
describe('[#28201] an undeclared dimension on the native, ObjectQL and draft-preview faces', () => {
72+
let engine: ObjectQL;
73+
const services: Partial<Record<'native' | 'objectql', AnalyticsService>> = {};
74+
75+
const ask = (face: Face, selection: { dimensions: readonly string[]; measures: readonly string[] }) =>
76+
services[face === 'preview' ? 'objectql' : face]!.queryDataset(
77+
DATASET,
78+
{ dimensions: [...selection.dimensions], measures: [...selection.measures] } as never,
79+
undefined,
80+
face === 'preview' ? { previewDrafts: true } : undefined,
81+
);
82+
83+
beforeAll(async () => {
84+
const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as never);
85+
engine = new ObjectQL({ logger: quiet } as never);
86+
engine.registerDriver(driver as never, true);
87+
await engine.init();
88+
engine.registry.registerObject({
89+
name: OBJECT,
90+
label: 'Deal',
91+
fields: {
92+
region: { name: 'region', type: 'text' },
93+
amount: { name: 'amount', type: 'number' },
94+
},
95+
} as never);
96+
await engine.syncSchemas();
97+
await installDefaultOrganization(engine as never);
98+
for (const row of ROWS) await engine.insert(OBJECT, { ...row } as never);
99+
100+
for (const [face, caps] of [
101+
['native', undefined],
102+
['objectql', () => ({ nativeSql: false, objectqlAggregate: true, inMemory: false })],
103+
] as const) {
104+
const registered: Record<string, unknown> = {};
105+
const provided: Record<string, unknown> = { data: engine, protocol: pendingSeedProtocol };
106+
await new AnalyticsServicePlugin({ ...(caps ? { queryCapabilities: caps } : {}) } as never).init({
107+
getService: (name: string) => provided[name] ?? registered[name],
108+
registerService: (name: string, svc: unknown) => { registered[name] = svc; },
109+
replaceService: (name: string, svc: unknown) => { registered[name] = svc; },
110+
hook: () => {},
111+
logger: quiet,
112+
} as never);
113+
services[face] = registered.analytics as AnalyticsService;
114+
}
115+
});
116+
117+
afterAll(async () => {
118+
try { await engine?.destroy(); } catch { /* noop */ }
119+
});
120+
121+
it("refuses 'bogus' INVALID_FIELD / 400 with one message on every face", async () => {
122+
const refusals = await Promise.all(FACES.map((face) => refusalOf(ask(face, { dimensions: ['bogus'], measures: ['total'] }))));
123+
for (const err of refusals) {
124+
expect(err.code, err.message).toBe('INVALID_FIELD');
125+
expect(err.status).toBe(400);
126+
expect(err.param).toBe('dimensions');
127+
expect(err.field).toBe('bogus');
128+
expect(err.dimension).toBe('bogus');
129+
expect(err.message).toContain("Dimension 'bogus' on cube 'os28201_deals' groups by field 'bogus'");
130+
expect(err.message).toContain('Valid dimensions: region.');
131+
}
132+
const [native, objectql, preview] = refusals;
133+
expect(preview.message).toBe(native.message);
134+
expect(objectql.message).toBe(native.message);
135+
});
136+
137+
it("refuses 'bogus' beside a declared dimension with one message on every face", async () => {
138+
const refusals = await Promise.all(FACES.map((face) => refusalOf(ask(face, { dimensions: ['region', 'bogus'], measures: ['deals'] }))));
139+
for (const err of refusals) {
140+
expect(err.code, err.message).toBe('INVALID_FIELD');
141+
expect(err.status).toBe(400);
142+
expect(err.field).toBe('bogus');
143+
}
144+
expect(refusals[2].message).toBe(refusals[0].message);
145+
});
146+
147+
for (const [dimension, expected] of [
148+
['region', [{ key: 'amer', total: 150 }, { key: 'emea', total: 30 }]],
149+
['amount', [{ key: 30, total: 30 }, { key: 50, total: 50 }, { key: 100, total: 100 }]],
150+
] as const) {
151+
it(`CONTROL: '${dimension}' serves the same groups on every face`, async () => {
152+
for (const face of FACES) {
153+
const result = await ask(face, { dimensions: [dimension], measures: ['total'] });
154+
const groups = (result.rows as Array<Record<string, unknown>>)
155+
.map((r) => ({ key: r[dimension], total: Number(r.total) }))
156+
.sort((a, b) => String(a.key).localeCompare(String(b.key), undefined, { numeric: true }));
157+
expect(groups, face).toEqual(expected);
158+
}
159+
});
160+
}
161+
162+
for (const [label, selection, param] of [
163+
["'nope' beside 'bogus': the measure's missing field", { dimensions: ['bogus'], measures: ['nope'] }, 'measures'],
164+
["'region_sum' beside 'bogus': the dimension's missing field", { dimensions: ['bogus'], measures: ['region_sum'] }, 'dimensions'],
165+
["'amount + 1' beside 'region_sum': the field-type door", { dimensions: ['region'], measures: ['amount + 1', 'region_sum'] }, 'measures'],
166+
] as const) {
167+
it(`two mistakes, ${label} first, on every face`, async () => {
168+
const refusals = await Promise.all(FACES.map((face) => refusalOf(ask(face, selection))));
169+
for (const err of refusals) {
170+
expect(err.code, err.message).toBe('INVALID_FIELD');
171+
expect(err.status).toBe(400);
172+
expect(err.param).toBe(param);
173+
}
174+
expect(refusals[2].message).toBe(refusals[0].message);
175+
});
176+
}
177+
});

‎packages/services/service-analytics/src/analytics-service.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2763,13 +2763,25 @@ export class AnalyticsService implements IAnalyticsService {
27632763
const cube: Cube = Object.keys(minted).length > 0
27642764
? { ...compiled.cube, measures: { ...compiled.cube.measures, ...minted } }
27652765
: compiled.cube;
2766+
// The gates run in `ensureCube`'s order, then `query()`'s, so a
2767+
// query with two mistakes is answered about the same one first on
2768+
// every face. [#28201] …and an undeclared dimension is judged as
2769+
// `ensureCube` judges it: one whose field the object does not have
2770+
// (`bogus`) is refused `INVALID_FIELD` / 400 with the same message,
2771+
// where the evaluator grouped every row under one `null` key. One
2772+
// that names a real field (`amount`) groups by it, here as there.
27662773
this.assertMeasureFields(q, cube, Object.keys(compiled.cube.measures));
2767-
this.assertCallerMeasuresResolvable(q, compiled.cube, compiled.cube, compiled.durations, context);
2768-
// [#21044] …and the aggregate × field-type table, which the
2769-
// published path also asks in `ensureCube`: a minted `region_sum`
2770-
// over a text field is refused there, and here it would sum no
2771-
// number at all and read `0`.
2774+
this.assertDimensionFields(q, cube, Object.keys(compiled.cube.dimensions));
2775+
// [#21044] …and the aggregate × field-type table: a minted
2776+
// `region_sum` over a text field is refused there, and here it
2777+
// would sum no number at all and read `0`.
27722778
this.assertMeasureFieldTypes(q, cube);
2779+
// [ADR-0144 D6] …and the record-currency door: a draft preview
2780+
// must not serve the sum across currencies that the published
2781+
// dataset refuses.
2782+
this.assertRecordCurrencyGrouped(q, cube);
2783+
// [#21156] `query()` asks the caller-member gate after `ensureCube`.
2784+
this.assertCallerMeasuresResolvable(q, compiled.cube, compiled.cube, compiled.durations, context);
27732785
// [#21120] The body-column refusal rides this preview path too —
27742786
// a drafted seed row carries the same stored body the published
27752787
// ones do.
@@ -2778,10 +2790,6 @@ export class AnalyticsService implements IAnalyticsService {
27782790
// carries the same `internal` fields the published ones do.
27792791
this.assertNoInternalFieldsQueried(q, cube, previewScope);
27802792
await this.assertFieldsReadable(q, cube, previewScope, context);
2781-
// [ADR-0144 D6] …and the record-currency door, which the published
2782-
// path meets in `ensureCube`: a draft preview must not serve the
2783-
// sum across currencies that the published dataset refuses.
2784-
this.assertRecordCurrencyGrouped(q, cube);
27852793
return evaluateAnalyticsQueryOverRows(
27862794
q,
27872795
cube,

0 commit comments

Comments
 (0)