Skip to content

Commit 348838c

Browse files
committed
fix(service-analytics)!: the /analytics/sql dry run refuses a selection asking one column twice, as /analytics/query does
- objectql-strategy.ts generateSql: judges the keys execute() hands the engine (aggregateGroupKeys, or the cross-object path's keys when the plan routes there) with the #28798 dataset door before it renders a bucket, so the dry run answers the query's INVALID_QUERY / 400 field for field. It rendered a statement for two granularities of one dimension (one bucket, the other dropped), a column raw beside its own bucket and two spellings bucketed twice, and a 501 saying the query is served at a non-UTC zone. - The echo's one-granularity granByDim map is gone: it reads granularitiesByDimension, as the key builders do. - crossObjectGroupKeys: executeCrossObject's key build moved to a module-level function (not exported) so the echo judges the same keys. - Tests on real SQLite: each refused shape on the dry run vs the query, field for field; served echoes pinned byte for byte. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3b920f6 commit 348838c

3 files changed

Lines changed: 268 additions & 37 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
'@objectstack/service-analytics': patch
3+
---
4+
5+
fix(service-analytics)!: the `/analytics/sql` dry run refuses a selection asking one column twice, as `/analytics/query` does, instead of rendering a statement the query refuses
6+
7+
Clause-②: no (narrowing) — `/analytics/sql` (`AnalyticsService.generateSql`) now refuses `INVALID_QUERY` / 400 a selection that names one column twice in its groups (two granularities of one time dimension, the column raw beside its own bucket, or two spellings of it bucketed twice), with the refusal `/analytics/query` gives the same selection, field for field; it rendered a statement for each. Nothing is widened: no export, public member, 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 of the package root changes: a dry run that rendered a statement the query refuses is refused with the query's refusal, already on the wire. There is nothing for a migration to rewrite and no tombstone. -->
11+
12+
**BREAKING** accept-set narrowing on the `/analytics/sql` dry run (`@objectstack/service-analytics`). `/analytics/query` and `queryDataset` are unchanged.
13+
14+
- **Before.** Measured on real SQLite on `main` `7e60dc23` (native declines every bucketed selection, so the ObjectQL strategy's echo answers). `/analytics/query` refused each selection below, and the dry run did this instead:
15+
- `timeDimensions` `closed_at` by month and by year: rendered one `year` bucket, the month entry dropped.
16+
- `closed_at` raw beside `CUBE.closed_at` by month, or a second dimension over `closed_at` beside its bucket: rendered `GROUP BY closed_at, strftime('%Y-%m', …)`.
17+
- `closed_at` by month beside `CUBE.closed_at` by year: rendered both buckets.
18+
- Beside a cross-object `account.name`: rendered the join and one bucket.
19+
- At a non-UTC `timezone`: answered `NOT_IMPLEMENTED` / 501, saying the query is served.
20+
- **Now.** The echo judges the keys `execute()` hands the engine with the same dataset door (`dataset-group-column-door.ts`, #28798) before it renders a bucket, so it answers the query's `INVALID_QUERY` / 400 with the same message, `object`, `param`, `path` and `fields`. On a selection that routes to the cross-object path, those are that path's keys. The echo's one-granularity-per-dimension map is gone: it reads the granularities the query's key builders read.
21+
- **Unchanged.** Every echo the query serves renders byte for byte (pinned): one bucket of a column, buckets of two columns, one granularity asked twice, raw columns, a cross-object dimension. The 501 for a bucket the engine groups in memory is unchanged when the selection names no column twice. No new refusal is added; the query's own now reaches the dry run.
22+
- **Who sees it.** A caller of `/analytics/sql` sending one of those selections now gets the query's refusal, instead of SQL for a query that does not run.
23+
24+
**The remedy.** Ask each column once: drop the raw dimension or the bucket's granularity, or ask one granularity. The refusal names the members.
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#28841] The `/analytics/sql` dry run refuses what `/analytics/query`
5+
* refuses for one column asked twice under one name, on real SQLite.
6+
*
7+
* ## The shape this closes
8+
*
9+
* A selection asking one column for two values (two granularities of it, the
10+
* column raw beside its own bucket, or two spellings of it bucketed twice) is
11+
* refused by the query: the ObjectQL strategy's dataset door
12+
* (`dataset-group-column-door.ts`, #28798) judges the keys `execute()` hands
13+
* the engine, `INVALID_QUERY` / 400. Native declines every bucketed selection
14+
* to that strategy, so its `generateSql` is the dry run that answers. It built
15+
* its GROUP BY from its own map of one granularity per dimension and ran no
16+
* door, so on `main` `7e60dc23` it rendered a statement for each:
17+
*
18+
* | selection | the dry run rendered |
19+
* |:--|:--|
20+
* | `closed_at` month and year | one `strftime('%Y', …)` bucket (the month entry dropped) |
21+
* | `closed_at` raw beside `CUBE.closed_at` month | `GROUP BY closed_at, strftime('%Y-%m', …)` |
22+
* | `closed_day` (over `closed_at`) beside `closed_at` month | the same two columns |
23+
* | `closed_at` month beside `CUBE.closed_at` year | both buckets |
24+
* | `account.name` beside `closed_at` month and year | the join, one `year` bucket |
25+
* | `closed_at` month and year at `America/New_York` | `NOT_IMPLEMENTED` / 501 saying the query is served |
26+
*
27+
* It now judges the same keys with the same door before it renders anything
28+
* (the cross-object path's keys when the selection routes there), so it
29+
* answers the query's refusal field for field. Every echo the query serves
30+
* still renders byte for byte (pinned below).
31+
*/
32+
33+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
34+
import { ObjectQL } from '@objectstack/objectql';
35+
import { SqlDriver } from '@objectstack/driver-sql';
36+
import type { Cube } from '@objectstack/spec/data';
37+
import type { AnalyticsService } from '../analytics-service.js';
38+
import { AnalyticsServicePlugin } from '../plugin.js';
39+
import { installDefaultOrganization } from './default-organization.testkit.js';
40+
41+
const DEAL = 'os28841_deal';
42+
const ACCOUNT = 'os28841_account';
43+
const CUBE = 'os28841_cube';
44+
45+
const CUBES = [
46+
{
47+
name: CUBE,
48+
title: 'Column twice cube',
49+
sql: DEAL,
50+
public: true,
51+
measures: { amount_sum: { type: 'sum', sql: 'amount', label: 'Amount' } },
52+
dimensions: {
53+
region: { type: 'string', sql: 'region', label: 'Region' },
54+
closed_at: { type: 'time', sql: 'closed_at', label: 'Closed at' },
55+
closed_on: { type: 'time', sql: 'closed_on', label: 'Closed on' },
56+
closed_day: { type: 'time', sql: 'closed_at', label: 'Closed day' },
57+
},
58+
},
59+
] as unknown as Cube[];
60+
61+
const quiet = { debug() {}, info() {}, warn() {}, error() {}, child() { return quiet; } };
62+
63+
type Query = Record<string, unknown>;
64+
const td = (dimension: string, granularity: string) => ({ dimension, granularity });
65+
const onCube = (extra: Query): Query => ({ cube: CUBE, measures: ['amount_sum'], ...extra });
66+
/** The inferred cube over the deal object, which serves a lookup path as a cross-object dimension. */
67+
const onObject = (extra: Query): Query => ({ cube: DEAL, measures: ['amount_sum'], ...extra });
68+
69+
describe('[#28841] the /analytics/sql dry run refuses one column asked twice, as the query does', () => {
70+
let engine: ObjectQL;
71+
let analytics: AnalyticsService;
72+
73+
beforeAll(async () => {
74+
const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as never);
75+
engine = new ObjectQL({ logger: quiet } as never);
76+
engine.registerDriver(driver as never, true);
77+
await engine.init();
78+
engine.registry.registerObject({ name: ACCOUNT, label: 'Account', fields: { name: { name: 'name', type: 'text' } } } as never);
79+
engine.registry.registerObject({
80+
name: DEAL,
81+
label: 'Deal',
82+
fields: {
83+
region: { name: 'region', type: 'text' },
84+
amount: { name: 'amount', type: 'number' },
85+
closed_at: { name: 'closed_at', type: 'datetime' },
86+
closed_on: { name: 'closed_on', type: 'date' },
87+
account: { name: 'account', type: 'lookup', reference: ACCOUNT },
88+
},
89+
} as never);
90+
await engine.syncSchemas();
91+
await installDefaultOrganization(engine as never);
92+
await engine.insert(ACCOUNT, { id: 'a1', name: 'Acme' } as never);
93+
await engine.insert(DEAL, { id: 'd1', region: 'amer', amount: 10, closed_at: '2026-03-01T00:00:00.000Z', closed_on: '2026-03-02', account: 'a1' } as never);
94+
95+
// The default composition: no `queryCapabilities` override.
96+
const registered: Record<string, unknown> = {};
97+
await new AnalyticsServicePlugin({ cubes: CUBES, debugSql: true } as never).init({
98+
getService: (name: string) => (name === 'data' ? engine : registered[name]),
99+
registerService: (name: string, svc: unknown) => { registered[name] = svc; },
100+
replaceService: (name: string, svc: unknown) => { registered[name] = svc; },
101+
hook: () => {},
102+
logger: quiet,
103+
} as never);
104+
analytics = registered.analytics as AnalyticsService;
105+
});
106+
107+
afterAll(async () => {
108+
try { await engine?.destroy(); } catch { /* noop */ }
109+
});
110+
111+
/** The refusal `p` rejects with, as the keys a caller reads; a resolution fails the case. */
112+
const refusalOf = async (face: string, p: Promise<unknown>) => {
113+
const err = (await p.then(
114+
() => { throw new Error(`${face}: expected a refusal, but it resolved`); },
115+
(e: unknown) => e,
116+
)) as Error & Record<string, unknown>;
117+
return {
118+
message: err.message, code: err.code, status: err.status, httpStatus: err.httpStatus,
119+
object: err.object, param: err.param, path: err.path, fields: err.fields,
120+
};
121+
};
122+
123+
it.each<[string, Query, string]>([
124+
['two granularities of one dimension', onCube({ timeDimensions: [td('closed_at', 'month'), td('closed_at', 'year')] }), 'groupBy.1.field'],
125+
['a column raw beside its own bucket, spelled CUBE.closed_at', onCube({ dimensions: ['closed_at'], timeDimensions: [td(`${CUBE}.closed_at`, 'month')] }), 'groupBy.1.field'],
126+
['a second dimension over the column, raw beside its bucket', onCube({ dimensions: ['closed_day'], timeDimensions: [td('closed_at', 'month')] }), 'groupBy.1.field'],
127+
['two spellings of one column at two granularities', onCube({ timeDimensions: [td('closed_at', 'month'), td(`${CUBE}.closed_at`, 'year')] }), 'groupBy.1.field'],
128+
['the column raw beside its bucket on the inferred cube', onObject({ dimensions: ['closed_at'], timeDimensions: [td(`${DEAL}.closed_at`, 'month')] }), 'groupBy.1.field'],
129+
["two granularities beside a cross-object 'account.name' (its FK column first)", onObject({ dimensions: ['account.name'], timeDimensions: [td('closed_at', 'month'), td('closed_at', 'year')] }), 'groupBy.2.field'],
130+
["two granularities beside two dimensions through one relationship (one FK key, so the later position)", onObject({ dimensions: ['account.id', 'account.name'], timeDimensions: [td('closed_at', 'month'), td('closed_at', 'year')] }), 'groupBy.2.field'],
131+
['two granularities at a non-UTC zone, where the dry run said the query is served (501)', onCube({ timeDimensions: [td('closed_at', 'month'), td('closed_at', 'year')], timezone: 'America/New_York' }), 'groupBy.1.field'],
132+
])('refuses %s with the query\'s refusal, field for field', async (_label, query, path) => {
133+
const asQuery = await refusalOf('query', analytics.query({ ...query } as never));
134+
const asSql = await refusalOf('sql', analytics.generateSql({ ...query } as never));
135+
expect(asQuery).toMatchObject({ code: 'INVALID_QUERY', status: 400, httpStatus: 400, object: DEAL, param: 'groupBy', path, fields: ['closed_at'] });
136+
expect(asSql, 'the dry run vs the query').toEqual(asQuery);
137+
});
138+
139+
it.each<[string, Query, string]>([
140+
[
141+
'one bucket of one column',
142+
onCube({ timeDimensions: [td('closed_at', 'month')] }),
143+
"SELECT strftime('%Y-%m', `closed_at`) AS \"closed_at\", SUM(amount) AS \"amount_sum\" FROM \"os28841_deal\" "
144+
+ "GROUP BY strftime('%Y-%m', `closed_at`)",
145+
],
146+
[
147+
'buckets of two columns',
148+
onCube({ timeDimensions: [td('closed_at', 'month'), td('closed_on', 'year')] }),
149+
"SELECT strftime('%Y-%m', `closed_at`) AS \"closed_at\", strftime('%Y', `closed_on`) AS \"closed_on\", "
150+
+ "SUM(amount) AS \"amount_sum\" FROM \"os28841_deal\" GROUP BY strftime('%Y-%m', `closed_at`), strftime('%Y', `closed_on`)",
151+
],
152+
[
153+
'a dimension that is its own bucket, asked twice at one granularity, beside a raw column',
154+
onCube({ dimensions: ['region', 'closed_at'], timeDimensions: [td('closed_at', 'month'), td('closed_at', 'month')] }),
155+
"SELECT region AS \"region\", strftime('%Y-%m', `closed_at`) AS \"closed_at\", SUM(amount) AS \"amount_sum\" "
156+
+ "FROM \"os28841_deal\" GROUP BY region, strftime('%Y-%m', `closed_at`)",
157+
],
158+
])('still renders %s byte for byte, the statement the query echoes', async (_label, query, sql) => {
159+
const dryRun = await analytics.generateSql({ ...query } as never);
160+
expect(dryRun).toEqual({ sql, params: [] });
161+
expect((await analytics.query({ ...query } as never)).sql).toBe(sql);
162+
});
163+
164+
it("still renders a cross-object 'account.name' beside one bucket byte for byte", async () => {
165+
const dryRun = await analytics.generateSql(onObject({ dimensions: ['account.name'], timeDimensions: [td('closed_at', 'month')] }) as never);
166+
expect(dryRun).toEqual({
167+
sql: "SELECT \"os28841_account\".\"name\" AS \"account.name\", strftime('%Y-%m', `closed_at`) AS \"closed_at\", "
168+
+ "SUM(amount) AS \"amount_sum\" FROM \"os28841_deal\" LEFT JOIN \"os28841_account\" ON \"os28841_deal\".\"account\" = "
169+
+ "\"os28841_account\".\"id\" GROUP BY \"os28841_account\".\"name\", strftime('%Y-%m', `closed_at`)",
170+
params: [],
171+
});
172+
});
173+
174+
it('still answers one bucket at a non-UTC zone with the echo refusal, while the query serves it', async () => {
175+
const query = onCube({ timeDimensions: [td('closed_at', 'month')], timezone: 'America/New_York' });
176+
expect(await refusalOf('sql', analytics.generateSql({ ...query } as never))).toMatchObject({ code: 'NOT_IMPLEMENTED', status: 501 });
177+
expect((await analytics.query({ ...query } as never)).rows).toHaveLength(1);
178+
});
179+
});

0 commit comments

Comments
 (0)