Skip to content

Commit 890ec13

Browse files
committed
fix(service-analytics): refuse a derived measure currency that contradicts its operands' fixed code
Guard 1 of measure-currency-coherence skipped derived measures for the fixed-code comparison, so a derived sum of two EUR-fixed measures that declared `currency: 'USD'` compiled and its column printed euros labelled USD. A derived measure whose operands resolve to one `fixed` code now refuses a different declared `currency` with DATASET_INVALID / 400, naming the operands' code and the fix (delete the measure's `currency`). Restating the same code stays legal. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4d1Np3hU7RQnA13YZcL
1 parent 3f02da1 commit 890ec13

3 files changed

Lines changed: 77 additions & 2 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@objectstack/service-analytics': minor
3+
---
4+
5+
fix(service-analytics): a dataset refuses a derived measure `currency` that contradicts the one `fixed` code its operands resolve to
6+
7+
Clause-②: yes (narrowing)
8+
Metadata-protocol: narrowed
9+
10+
**BREAKING** (`@objectstack/service-analytics`): one more compile-time refusal, `DATASET_INVALID` / 400, for dataset metadata that compiled before and printed a wrong label. It reaches boot registration, `queryDataset` and the Studio draft preview, which compile through the one `compileDataset` call.
11+
12+
- A derived `sum` / `difference` whose monetary operands all resolve to one `fixed` code, directly or through a derived measure built on them, and which declares its own `currency` with another code. The column printed the operands' amounts under the declared code, such as `{ derived: { op: 'sum', of: ['eur_a', 'eur_b'] }, currency: 'USD' }` over two EUR-fixed measures printing euros labelled `USD`. The message names the dataset, the measure, its operands and their code, and the fix: delete the measure's `currency`, and the column takes the operands' code.
13+
- A derived measure `currency` that restates the operands' `fixed` code is still accepted.
14+
15+
Not judged, unchanged: a derived measure whose operands resolve to no one `fixed` code (a `fixed` amount with a tenant-currency one, a `ratio` or `product`), and the record-mode and mixed-code refusals of both guards.
16+
17+
No in-tree dataset declares this shape. A deployed tenant's stored dataset with it now refuses at registration and at query, and its message names the edit.
18+
19+
<!-- adr-0087: not-required (no-migration-prescription) the refusal rejects a contradiction in a dataset's own measures at compile and names the fix in its message; there is no stored shape to convert and no key is removed -->

‎packages/services/service-analytics/src/__tests__/record-currency-analytics.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,48 @@ describe('[ADR-0144 M0-3] analytics over a record-currency amount', () => {
370370
);
371371
});
372372

373+
for (const op of ['sum', 'difference'] as const) {
374+
it(`a derived ${op} declaring a currency other than its operands' one fixed code is refused, naming the fix`, () => {
375+
const measures = [
376+
...base,
377+
{ name: 'eur_restated', aggregate: 'sum', field: 'amt_eur', currency: 'EUR' },
378+
{ name: 'eur_as_usd', derived: { op, of: ['eur', 'eur_restated'] }, currency: 'USD' },
379+
];
380+
expectRefused(measures, 'eur_as_usd');
381+
expect(compileRefusal(measures)?.message).toContain(
382+
`[dataset-compiler] dataset "os27187_guard" derived measure "eur_as_usd" declares currency "USD" over `
383+
+ 'measures "eur", "eur_restated", whose amounts are fixed to EUR.',
384+
);
385+
expect(compileRefusal(measures)?.message)
386+
.toContain("delete the measure's `currency` (the column takes the operands' EUR)");
387+
});
388+
}
389+
390+
it('a derived measure declaring a currency over a fixed-code derived sum is judged too', () => {
391+
expectRefused(
392+
[...base, { name: 'eur_twice', derived: { op: 'sum', of: ['eur', 'eur'] } }, { name: 'eur_as_jpy', derived: { op: 'sum', of: ['eur_twice', 'eur'] }, currency: 'JPY' }],
393+
'eur_as_jpy',
394+
);
395+
});
396+
397+
it("CONTROL: a derived measure restating its operands' one fixed code is accepted", () => {
398+
expect(
399+
compileRefusal([...base, { name: 'eur_twice', derived: { op: 'sum', of: ['eur', 'eur'] }, currency: 'EUR' }]),
400+
).toBeUndefined();
401+
});
402+
403+
it('CONTROL: a derived measure currency is not judged where its operands resolve to no one fixed code', () => {
404+
expect(
405+
compileRefusal([
406+
...base,
407+
// a fixed amount with the tenant currency: one currency, but not a `fixed` one
408+
{ name: 'usd_and_tenant_as_jpy', derived: { op: 'sum', of: ['usd', 'tenant'] }, currency: 'JPY' },
409+
// a ratio resolves to no currency
410+
{ name: 'eur_per_invoice_as_usd', derived: { op: 'ratio', of: ['eur', 'invoices'] }, currency: 'USD' },
411+
]),
412+
).toBeUndefined();
413+
});
414+
373415
it('CONTROL: a fixed operand restated by its measure currency compares by that one code', () => {
374416
expect(
375417
compileRefusal([...base, { name: 'usd_restated', aggregate: 'sum', field: 'amt_usd', currency: 'USD' }, { name: 'both', derived: { op: 'sum', of: ['usd', 'usd_restated'] } }]),
@@ -400,6 +442,7 @@ describe('[ADR-0144 M0-3] analytics over a record-currency amount', () => {
400442
const fixedShapes = [
401443
['a measure currency contradicting a fixed field', [{ name: 'eur_as_jpy', aggregate: 'sum', field: 'amt_eur', currency: 'JPY' }], 'eur_as_jpy'],
402444
['a derived measure across two different fixed codes', [...base, { name: 'mixed', derived: { op: 'difference', of: ['usd', 'eur'] } }], 'mixed'],
445+
["a derived measure currency contradicting its operands' fixed code", [...base, { name: 'eur_as_usd', derived: { op: 'sum', of: ['eur', 'eur'] }, currency: 'USD' }], 'eur_as_usd'],
403446
] as const;
404447
for (const [label, measures, named] of fixedShapes) {
405448
for (const [door, options] of [['queryDataset', undefined], ['the draft preview', { previewDrafts: true }]] as const) {

‎packages/services/service-analytics/src/measure-currency-coherence.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
* field): its column carries no per-row pointer, so the one code would
1818
* label every row.
1919
* - Over a `fixed` field, a code other than the field's. The same code is
20-
* redundant, not wrong, and is accepted.
20+
* redundant, not wrong, and is accepted. That holds for a derived measure
21+
* whose resolved currency is one `fixed` code (a sum of `fixed` amounts in
22+
* one code): `computeDerived` keeps the operands' amounts, so the measure's
23+
* code would label them as another currency.
2124
* 2. **A derived measure across currencies.** `computeDerived` adds,
2225
* subtracts, multiplies and divides bare numbers, so such a value is not a
2326
* number in any currency (probe A, `m_mixed_sum`: `200` with no currency).
@@ -147,7 +150,8 @@ export function assertMeasureCurrenciesCoherent(
147150
});
148151

149152
// Guard 1 — a measure `currency` over a record-mode amount (aggregated or
150-
// derived), or contradicting a `fixed` field's code.
153+
// derived), or contradicting a `fixed` code (a field's, or the one its
154+
// derived operands resolve to).
151155
for (const m of dataset.measures) {
152156
if (m.duration || !m.currency) continue;
153157
const field = m.derived ? undefined : fieldOf(m);
@@ -176,6 +180,15 @@ export function assertMeasureCurrenciesCoherent(
176180
+ "currencies: delete the measure's `currency`.",
177181
);
178182
}
183+
if (resolved?.kind === 'code' && resolved.fixed && resolved.code !== m.currency) {
184+
const operands = [...new Set(monetaryOperands(m).map((o) => `"${o.name}"`))].join(', ');
185+
throw datasetInvalidError(
186+
`[dataset-compiler] dataset "${dataset.name}" derived measure "${m.name}" declares currency `
187+
+ `"${m.currency}" over measures ${operands}, whose amounts are fixed to ${resolved.code}. The amounts are `
188+
+ `${resolved.code}, so the column would print them as ${m.currency}: delete the measure's \`currency\` `
189+
+ `(the column takes the operands' ${resolved.code}).`,
190+
);
191+
}
179192
}
180193

181194
// Guard 2 — a derived measure combining a record-mode amount with another

0 commit comments

Comments
 (0)