Skip to content

Commit 98b05f5

Browse files
committed
test: fold the plain-group refusal into the having and guard fixtures; changeset and dogfood pin
Claude-Session: https://claude.ai/code/session_016WV4cyTehJsfYGVMzcXLJD Co-authored-by: Claude <noreply@anthropic.com>
1 parent 3a02407 commit 98b05f5

6 files changed

Lines changed: 111 additions & 4 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
'@objectstack/objectql': patch
3+
'@objectstack/service-analytics': patch
4+
---
5+
6+
fix(objectql, service-analytics)!: a plain group over a formula field is refused alike on every analytics face, instead of a 500, a driver backstop, one null group or a rendered statement
7+
8+
Clause-②: yes (narrowing) — `engine.aggregate` now refuses `INVALID_FIELD` / 400 a `groupBy` entry of any form (a name, `{ field }`, or a bucket) naming a virtual (`formula`) field, whatever its `returnType`, before any driver is asked: at `groupBy.N` for a name, `groupBy.N.field` for the object form, and `groupBy.N.dateGranularity` for a bucket, as before. `/analytics/query` on the native composition, `/analytics/sql` (`AnalyticsService.generateSql`), the draft preview of `queryDataset` and the delegated fallback face give the same refusal, field for field. Nothing is widened: no export of either package root, public member, answer member, error code or status is added.
9+
Metadata-protocol: none
10+
11+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export or type of either package root changes: a query that grouped by a column the table does not store is refused before it reaches the driver, with an error code and status already on the wire. There is nothing for a migration to rewrite and no tombstone. -->
12+
13+
**BREAKING** accept-set narrowing on `engine.aggregate` (`@objectstack/objectql`) and on every analytics face (`@objectstack/service-analytics`), for a plain group over a `formula` field only.
14+
15+
- **Before.** Measured on `main` `447226b2` with a real `SqlDriver`, a dataset or cube dimension over a `formula` (`returnType` `date` or `text`). `/analytics/query` on the native composition answered `DATABASE_ERROR` / 500 (the native strategy ran `GROUP BY calc` itself); on the ObjectQL composition it answered the SQL driver's missing-column backstop, `INVALID_FIELD` / 400 with no `path` and a "run schema sync" prescription that cannot help. The `/analytics/sql` dry run rendered `SELECT calc AS "calc" … GROUP BY calc` on both compositions, and the draft preview served one `null` group holding the total. `engine.aggregate` on a driver with no native `aggregate` (the in-memory aggregation) served one `null` group holding every row.
16+
- **Now.** The engine's aggregate door gains a storage axis (`assertGroupByTargetsAreStored`, ahead of the date-bucket door), judged by the spec's `isVirtualSearchField` (`SEARCH_VIRTUAL_TYPES`), the predicate the sort, filter, search and bucket axes already refuse a formula by. The service's copy (`dataset-date-bucket-door.ts`, `assertDatasetGroupTargetsStored`) gives the same refusal on the faces that hand the engine no keys: the dry run, the preview and the delegated fallback service. The native-SQL strategy declines a query whose `dimensions` entry resolves to a virtual field, so that query reaches the engine. A dimension through a relationship (`account.calc`) over a related object's formula is judged on the keys the cross-object path hands the related object, on the query and the dry run alike, whether or not a base row exists.
17+
- **Unchanged.** A plain group over a stored field, and a date or datetime bucket, are served on every face, byte for byte, and stay on the native strategy where they were. A measure-only query stays native. The #29167 refusal of a bucket over a formula keeps its words and position.
18+
- **Who sees it.** A caller grouping by a formula field: a located refusal with a remedy, instead of a 500, a backstop with the wrong advice, an empty-looking group or SQL for a statement that never runs.
19+
20+
**The remedy.** Denormalise the value onto the object (a stored field, written when the source changes) and group by that.

‎packages/objectql/src/engine-aggregate-reference-verdict-positions.test.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
* the engine's groupBy door and its `min` / `max` door refuse it first. The
3333
* aggregated column's type carries no `multiple` flag, and the rows below
3434
* hold that it needs none.
35+
* - [#29304] Nor does a `formula` column: it is virtual (no stored value to
36+
* group), and the engine's groupBy storage door
37+
* (`assertGroupByTargetsAreStored`) refuses a group over it first. A
38+
* formula pair at `having` is refused there, before the verdict is asked.
3539
*
3640
* The `where` column is measured in `packages/rest`
3741
* (`aggregation-filter-where-doors.test.ts`), where a real `SqlDriver` answers
@@ -228,6 +232,10 @@ function expectedAt(position: Position, target: string, referent: string): Expec
228232
// The `multiple` seam (pointer 5944816815): no aggregated column holds a list.
229233
return { kind: 'refused first by', door: 'the groupBy door (a multi-value or JSON field is not grouped by)', code: 'INVALID_FIELD' };
230234
}
235+
if (position === 'having' && (FIELDS[target].type === 'formula' || FIELDS[referent].type === 'formula')) {
236+
// [#29304] No aggregated column holds a formula: a group over one is refused.
237+
return { kind: 'refused first by', door: 'the groupBy storage door (a formula is not grouped by)', code: 'INVALID_FIELD' };
238+
}
231239
if (position === 'aggregations[i].filter' && FIELDS[target].type === 'formula') {
232240
// `where` meets the same door first: a formula is computed on read, never filtered on.
233241
return { kind: 'refused first by', door: 'the materializable-filter door (a formula key)', code: 'INVALID_FIELD' };
@@ -420,16 +428,17 @@ describe('[#21299] the card\'s four measured shapes are refused before any read,
420428
});
421429

422430
it('…and at having, an addDays pair from a formula projection, which was answered: refused, naming the formula', async () => {
431+
// [#29304] The formula projection itself is refused now, at its groupBy
432+
// position, before `having` is read: no aggregated column holds a formula.
423433
const { engine, calls } = await makeEngine(OBJ);
424434
const out = await outcomeOf(() => engine.aggregate(OBJECT, {
425435
groupBy: ['due_f', { field: 'due_on', dateGranularity: 'day', alias: 'due_day' }],
426436
aggregations: [{ function: 'count', alias: 'n' }],
427437
having: { due_f: { $lte: { $field: 'due_day', addDays: 1 } } },
428438
} as unknown as EngineAggregateOptions), () => []);
429-
expect(out).toMatchObject({ refused: true, code: 'INVALID_FILTER', status: 400 });
439+
expect(out).toMatchObject({ refused: true, code: 'INVALID_FIELD', status: 400 });
430440
expect(calls).toEqual({ aggregate: 0, find: 0 });
431-
expect(out.message).toContain('the target field "due_f" (type "formula")');
432-
expect(out.message).toContain('{ "$field": "due_day" } with addDays');
441+
expect(out.message).toContain("groupBy.0 groups by 'due_f', but 'due_f' is a formula field");
433442
});
434443

435444
it('the same-class control on the same object answers as before', async () => {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// END-TO-END gate for #29304: a plain group over a formula field is refused
4+
// `INVALID_FIELD` / 400 at its position on `POST /analytics/query` and on the
5+
// `POST /analytics/sql` dry run alike — proven on the stock showcase, through
6+
// the real stack (HTTP → REST → analytics plugin → the native strategy's
7+
// decline → the engine's aggregate door).
8+
//
9+
// `showcase_project.budget_remaining` is a `formula` (`returnType: 'currency'`):
10+
// computed on read, no column. Measured on `main` `447226b2` with a real
11+
// `SqlDriver`, a dimension over a formula answered `DATABASE_ERROR` / 500 on
12+
// the native composition (the strategy ran `GROUP BY` over a column no table
13+
// stores), and the dry run rendered that statement.
14+
//
15+
// Controls: a stored dimension (`status`) is served on both doors.
16+
17+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
18+
import { type VerifyStack } from '@objectstack/verify';
19+
import { bootShowcase } from './showcase-boot.js';
20+
21+
const CUBE = 'showcase_project';
22+
23+
describe('dogfood: a plain group over a formula field is refused alike on the query and the dry run (#29304)', () => {
24+
let stack: VerifyStack;
25+
let token: string;
26+
27+
beforeAll(async () => {
28+
stack = await bootShowcase();
29+
token = await stack.signIn();
30+
}, 180_000);
31+
32+
afterAll(async () => {
33+
await stack?.stop?.();
34+
});
35+
36+
async function ask(door: '/analytics/query' | '/analytics/sql', dimension: string) {
37+
const res = await stack.apiAs(token, 'POST', door, { cube: CUBE, measures: ['count'], dimensions: [dimension] });
38+
return { status: res.status, body: (await res.json()) as Record<string, any> };
39+
}
40+
41+
for (const door of ['/analytics/query', '/analytics/sql'] as const) {
42+
it(`${door}: a dimension over the formula → 400 INVALID_FIELD at groupBy.0, with the store-the-value remedy`, async () => {
43+
const res = await ask(door, 'budget_remaining');
44+
expect(res.status, JSON.stringify(res.body)).toBe(400);
45+
expect(res.body.code).toBe('INVALID_FIELD');
46+
const message = String(res.body.message ?? res.body.error);
47+
expect(message).toContain(
48+
`aggregate('${CUBE}'): groupBy.0 groups by 'budget_remaining', but 'budget_remaining' is a formula field`,
49+
);
50+
expect(message).toContain(`Denormalise the value onto '${CUBE}' (a stored field, written when the source changes) and group by that.`);
51+
});
52+
53+
it(`CONTROL ${door}: a stored dimension is served`, async () => {
54+
const res = await ask(door, 'status');
55+
expect(res.status, JSON.stringify(res.body)).toBe(200);
56+
});
57+
}
58+
59+
it('the query and the dry run give one refusal, word for word', async () => {
60+
const query = await ask('/analytics/query', 'budget_remaining');
61+
const sql = await ask('/analytics/sql', 'budget_remaining');
62+
expect(sql.body).toEqual(query.body);
63+
});
64+
});

‎packages/services/service-analytics/src/__tests__/dimension-structured-json-door.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,13 @@ describe('what the door does not judge', () => {
320320
() => null,
321321
(e: Refusal) => ({ code: e.code, status: e.status, member: e.member }),
322322
);
323+
if (type === 'formula') {
324+
// [#29304] Not this door's: the native strategy declines a grouped
325+
// virtual field to the engine path, which this native-only harness
326+
// does not configure — no refusal of this door's (no `member`).
327+
expect(verdict, type).toEqual({ code: undefined, status: undefined, member: undefined });
328+
continue;
329+
}
323330
expect(verdict, type).toEqual(
324331
STRUCTURED_JSON_TYPES.has(type) || isMultiValueField({ type })
325332
? { code: 'INVALID_FIELD', status: 400, member: `f_${type}` }

‎packages/services/service-analytics/src/__tests__/multi-value-json-stored-door.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,13 @@ describe('GUARD the judged population is the spec predicates\', over every Field
371371
() => null,
372372
(e: Refusal) => ({ code: e.code, status: e.status, member: e.member }),
373373
);
374+
if (type === 'formula') {
375+
// [#29304] Not this door's: the native strategy declines a grouped
376+
// virtual field to the engine path, which this native-only harness
377+
// does not configure — no refusal of this door's (no `member`).
378+
expect(verdict, field).toEqual({ code: undefined, status: undefined, member: undefined });
379+
continue;
380+
}
374381
const judged = STRUCTURED_JSON_TYPES.has(type) || isMultiValueField({ type, multiple });
375382
expect(verdict, field).toEqual(judged ? { code: 'INVALID_FIELD', status: 400, member: field } : null);
376383
}

‎packages/services/service-analytics/src/__tests__/preview-date-bucket-type.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ describe('[#28852] a date bucket over a column that is not a date, on every face
251251
it('[#29304] still serves a plain dimension over a stored column alike on every face', async () => {
252252
for (const face of FACES) {
253253
const result = await ask(face, { dimensions: ['region'], measures: ['total'] });
254-
const rows = (result.rows as Array<Record<string, unknown>>).map((r) => ({ ...r, total: Number(r.total) }));
254+
const rows = (result.rows as Array<Record<string, unknown>>).map((r) => ({ region: r.region, total: Number(r.total) }));
255255
expect(rows.sort((a, b) => String(a.region).localeCompare(String(b.region))), face)
256256
.toEqual([{ region: 'amer', total: 100 }, { region: 'emea', total: 50 }]);
257257
}

0 commit comments

Comments
 (0)