diff --git a/.changeset/27111-compare-to-empty-buckets.md b/.changeset/27111-compare-to-empty-buckets.md new file mode 100644 index 00000000000..47cf941afcf --- /dev/null +++ b/.changeset/27111-compare-to-empty-buckets.md @@ -0,0 +1,16 @@ +--- +'@objectstack/service-analytics': patch +--- + +fix(service-analytics): a `compareTo` dataset answer also returns the time buckets that are empty in both windows (#27111) + +Clause-②: no — no input is refused or newly accepted and no export or type changes; a `compareTo` dataset answer gains the rows for the time buckets no record fell in, in either window, which it used to leave out +Metadata-protocol: none + +The empty time-bucket fill for a date-dimension series did not run when the selection carried a `compareTo`, so a period-over-period trend still dropped a month that held no record in either window, and a chart drew the months on either side of it as neighbours. `queryDataset` (and `POST /api/v1/analytics/dataset/query`) now fills a `compareTo` answer by the same rule as a plain series: + +- **Which buckets.** The primary window decides: every bucket of the time axis's own `dateRange`, never the shifted one. A bucket that holds records in either window already comes back, with the same values as before; only a bucket empty in both windows is added. An axis with no `dateRange` of its own (the comparison anchored on another, window-only date) gets only the gaps between its first and last bucket filled. +- **Their values.** The same values an empty group reads, for the measure and for its `__compare` column alike: `0` for `count` and `sum`; `avg`, `min` and `max` stay unset (the column is absent from the row). A derived measure is computed over them as over any row. +- **Not filled.** As for a plain series: a selection with a `limit` or `offset`, an `order` on a measure or on a `__compare` column, a grid with two bucketed time dimensions, and a fill that would leave more than 2,000 rows answer exactly as before. + +This supersedes the "or a `compareTo`" in the **Not filled** line of the #26893 note in the same release: a `compareTo` answer is filled too, as above. diff --git a/packages/services/service-analytics/src/__tests__/dataset-compare-bucket-alignment.test.ts b/packages/services/service-analytics/src/__tests__/dataset-compare-bucket-alignment.test.ts index 73e22471ec3..a27635126ad 100644 --- a/packages/services/service-analytics/src/__tests__/dataset-compare-bucket-alignment.test.ts +++ b/packages/services/service-analytics/src/__tests__/dataset-compare-bucket-alignment.test.ts @@ -370,9 +370,13 @@ describe('#6007 — the shift-back at each bucket size the selection can ask for }, CTX, ); + // 1 and 4 March hold nothing in either window: since #27111 the + // time-bucket fill adds them at 0 on both sides. expect(result.rows).toEqual([ + { close_date: '2026-03-01', opp_count: 0, opp_count__compare: 0 }, { close_date: '2026-03-02', opp_count: 1, opp_count__compare: 5 }, { close_date: '2026-03-03', opp_count: 2, opp_count__compare: 6 }, + { close_date: '2026-03-04', opp_count: 0, opp_count__compare: 0 }, ]); }); diff --git a/packages/services/service-analytics/src/__tests__/dataset-time-bucket-zero-fill.test.ts b/packages/services/service-analytics/src/__tests__/dataset-time-bucket-zero-fill.test.ts index e3136cea1df..adfc59c5ea4 100644 --- a/packages/services/service-analytics/src/__tests__/dataset-time-bucket-zero-fill.test.ts +++ b/packages/services/service-analytics/src/__tests__/dataset-time-bucket-zero-fill.test.ts @@ -26,8 +26,12 @@ * - another grid dimension is filled per combination that holds a row; * - a non-UTC zone: the keys the fill writes are the keys the query writes; * - the controls — a full window, a top-N, an order on a measure, no time - * dimension, `compareTo` — come back as they always have; - * - the bound: a fill past `MAX_ZERO_FILLED_GRID_ROWS` does not run at all. + * dimension — come back as they always have; + * - the bound: a fill past `MAX_ZERO_FILLED_GRID_ROWS` does not run at all; + * - #27111, `compareTo`: the same fill, over the PRIMARY window, adds the + * buckets empty in both windows with `0` in both the measure and its + * `__compare`; a bucket either window holds is unchanged, and the guards + * (no axis `dateRange`, a `limit`, an order on a `__compare`) hold. */ import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'; @@ -354,29 +358,160 @@ describe('[#26893] controls — answers the fill must not change', () => { expect(res.rows.map((r) => `${r.owner}|${r.opp_count}`).sort()).toEqual(['ann|10', 'bob|5']); }); - it('compareTo keeps its shape: its empty buckets are not filled', async () => { + it(`a fill past ${MAX_ZERO_FILLED_GRID_ROWS} rows does not run at all — never a partial fill`, async () => { const res = await run({ dimensions: ['close_date'], - measures: ['total_amount'], + measures: ['opp_count'], + dateGranularity: 'day', + runtimeFilter: WON, + timeDimensions: [{ dimension: 'close_date', dateRange: ['2017-01-01', '2026-12-31'] }], + }); + expect(res.rows).toHaveLength(DEALS.filter((d) => d.stage === 'closed_won').length); + }); +}); + +describe('[#27111] compareTo: a bucket empty in both windows is filled, over the primary window', () => { + it('May–August vs the previous year: May and August come back, with 0 in the measure and in its __compare', async () => { + const res = await run({ + dimensions: ['close_date'], + measures: ['opp_count', 'total_amount', 'avg_amount'], runtimeFilter: WON, timeDimensions: [{ dimension: 'close_date', dateRange: ['2026-05-01', '2026-08-31'] }], compareTo: { kind: 'previousYear' }, }); - // May and August hold nothing in either window (2025-05..08 holds no - // deal), and stay absent: the fill does not run under `compareTo`. - expect(series(res.rows)).toEqual([['2026-06', 70], ['2026-07', 80]]); - expect(res.rows.map((r) => r.total_amount__compare)).toEqual([0, 0]); + // 2025-05..08 holds no deal, and 2026 holds June and July only. + expect(series(res.rows)).toEqual([['2026-05', 0], ['2026-06', 70], ['2026-07', 80], ['2026-08', 0]]); + expect(res.rows.map((r) => r.total_amount__compare)).toEqual([0, 0, 0, 0]); + for (const month of ['2026-05', '2026-08']) { + const row = res.rows.find((r) => r.close_date === month)!; + expect(row.opp_count).toBe(0); + expect(row.opp_count__compare).toBe(0); + // avg is an absent key on both sides, the way an empty group reads it. + expect('avg_amount' in row).toBe(false); + expect('avg_amount__compare' in row).toBe(false); + } }); +}); - it(`a fill past ${MAX_ZERO_FILLED_GRID_ROWS} rows does not run at all — never a partial fill`, async () => { +/** + * A compare-only fixture, its own stage: March–August 2026 against + * March–August 2025, one month in each of the four states. + * + * | month | 2026 (primary) | 2025 (compare) | + * |:--|:--|:--| + * | 03 | 1 deal | 1 deal | + * | 04 | 1 deal | — | + * | 05 | — | 1 deal | + * | 06 | — | — | + * | 07 | 1 deal | — | + * | 08 | — | — | + * + * `closed_at` dates the same deals for the window-only anchor case: 2026's + * deals close in 2026 and 2025's in 2025, so anchoring on `closed_at` while + * the grid buckets `close_date` gives a series with no axis window. + */ +const CMP = { stage: 'cmp_probe' }; +const CMP_DEALS = [ + { id: 'c1', close_date: '2026-03-10', closed_at: '2026-03-10T12:00:00.000Z', amount: 4 }, + { id: 'c2', close_date: '2026-04-10', closed_at: '2026-04-10T12:00:00.000Z', amount: 8 }, + { id: 'c3', close_date: '2026-07-10', closed_at: '2026-07-10T12:00:00.000Z', amount: 16 }, + { id: 'c4', close_date: '2025-03-10', closed_at: '2025-03-10T12:00:00.000Z', amount: 1 }, + { id: 'c5', close_date: '2025-05-10', closed_at: '2025-05-10T12:00:00.000Z', amount: 2 }, +]; +const CMP_WINDOW: [string, string] = ['2026-03-01', '2026-08-31']; +const cells = (rows: Record[]) => + rows.map((r) => `${r.close_date}|${r.opp_count}|${r.opp_count__compare}`); + +describe('[#27111] compareTo: only the both-empty buckets are added; every other answer is unchanged', () => { + beforeAll(async () => { + for (const deal of CMP_DEALS) await engine.insert(DEAL, { ...deal, ...CMP, owner: 'ann' } as any); + }); + + it('a bucket either window holds keeps the merge\'s values; June and August are added at 0 / 0', async () => { + const res = await run({ + dimensions: ['close_date'], + measures: ['opp_count', 'total_amount'], + runtimeFilter: CMP, + timeDimensions: [{ dimension: 'close_date', dateRange: CMP_WINDOW }], + compareTo: { kind: 'previousYear' }, + }); + expect(cells(res.rows)).toEqual([ + '2026-03|1|1', '2026-04|1|0', '2026-05|0|1', '2026-06|0|0', '2026-07|1|0', '2026-08|0|0', + ]); + expect(res.rows.map((r) => [r.total_amount, r.total_amount__compare].map(Number))).toEqual([ + [4, 1], [8, 0], [0, 2], [0, 0], [16, 0], [0, 0], + ]); + }); + + it('the primary columns are the plain series\' answer: one fill rule', async () => { + const selection: DatasetSelection = { + dimensions: ['close_date'], + measures: ['opp_count', 'total_amount'], + runtimeFilter: CMP, + timeDimensions: [{ dimension: 'close_date', dateRange: CMP_WINDOW }], + }; + const plain = await run(selection); + const compared = await run({ ...selection, compareTo: { kind: 'previousYear' } }); + const primary = (rows: Record[]) => + rows.map((r) => [r.close_date, r.opp_count, Number(r.total_amount)]); + expect(primary(compared.rows)).toEqual(primary(plain.rows)); + expect(plain.rows.map((r) => r.close_date)).toEqual([ + '2026-03', '2026-04', '2026-05', '2026-06', '2026-07', '2026-08', + ]); + }); + + it('previousPeriod aligns by ordinal and fills the same way', async () => { + // June–August 2026 against March–May 2026: 03↔06, 04↔07, 05↔08. const res = await run({ dimensions: ['close_date'], measures: ['opp_count'], - dateGranularity: 'day', - runtimeFilter: WON, - timeDimensions: [{ dimension: 'close_date', dateRange: ['2017-01-01', '2026-12-31'] }], + runtimeFilter: CMP, + timeDimensions: [{ dimension: 'close_date', dateRange: ['2026-06-01', '2026-08-31'] }], + compareTo: { kind: 'previousPeriod' }, }); - expect(res.rows).toHaveLength(DEALS.filter((d) => d.stage === 'closed_won').length); + expect(cells(res.rows)).toEqual(['2026-06|0|1', '2026-07|1|1', '2026-08|0|0']); + }); + + it('an axis with no dateRange of its own (a window-only anchor) fills interior gaps only, no edges', async () => { + const res = await run({ + dimensions: ['close_date'], + measures: ['opp_count'], + runtimeFilter: CMP, + timeDimensions: [{ dimension: 'closed_at', dateRange: ['2026-01-01', '2026-12-31'] }], + compareTo: { kind: 'previousYear', dimension: 'closed_at' }, + }); + // The anchor is not a grid column, so the comparison rows key by their + // own close_date (2025-03, 2025-05). Between the first and last bucket + // the grid holds, every month; nothing before 2025-03 or after 2026-07. + expect(cells(res.rows)).toEqual([ + '2025-03|0|1', '2025-04|0|0', '2025-05|0|1', '2025-06|0|0', '2025-07|0|0', '2025-08|0|0', + '2025-09|0|0', '2025-10|0|0', '2025-11|0|0', '2025-12|0|0', '2026-01|0|0', '2026-02|0|0', + '2026-03|1|0', '2026-04|1|0', '2026-05|0|0', '2026-06|0|0', '2026-07|1|0', + ]); + }); + + it('a limit with compareTo is not filled', async () => { + const res = await run({ + dimensions: ['close_date'], + measures: ['opp_count'], + runtimeFilter: CMP, + timeDimensions: [{ dimension: 'close_date', dateRange: CMP_WINDOW }], + compareTo: { kind: 'previousYear' }, + limit: 10, + }); + expect(cells(res.rows)).toEqual(['2026-03|1|1', '2026-04|1|0', '2026-05|0|1', '2026-07|1|0']); + }); + + it('an order on a __compare column is not filled', async () => { + const res = await run({ + dimensions: ['close_date'], + measures: ['opp_count'], + runtimeFilter: CMP, + timeDimensions: [{ dimension: 'close_date', dateRange: CMP_WINDOW }], + compareTo: { kind: 'previousYear' }, + order: { opp_count__compare: 'desc' }, + }); + expect(res.rows.map((r) => r.close_date).sort()).toEqual(['2026-03', '2026-04', '2026-05', '2026-07']); }); }); diff --git a/packages/services/service-analytics/src/dataset-executor.ts b/packages/services/service-analytics/src/dataset-executor.ts index 55592517a85..201a3ec38ef 100644 --- a/packages/services/service-analytics/src/dataset-executor.ts +++ b/packages/services/service-analytics/src/dataset-executor.ts @@ -1550,9 +1550,19 @@ export class DatasetExecutor { * invented bucket would compete for its slots; * - an `order` on anything but a dimension — a measure, a `__compare` * column or a derived measure ranks rows, and a column of invented - * zeros reorders the ranking; - * - a `compareTo` — its answer keeps the shape it has (its own empty - * buckets are a separate question). + * zeros reorders the ranking. + * + * A `compareTo` answer is filled by this same plan (#27111), and the + * PRIMARY window decides the buckets: the plan reads the axis's own + * `dateRange`, never the shifted one. It runs after the comparison merge — + * when the axis is the anchor, {@link alignedCompareBucketKey} has already + * restated the comparison rows in primary bucket keys — so every bucket + * either window holds is a row already, left as the merge made it, and what + * the fill adds is only a bucket empty in BOTH windows. {@link fillEmptyGroups} + * then gives that row's `__compare` cells the empty values its + * primary cells get. An axis with no `dateRange` of its own (the anchor is + * another, window-only dimension) gets its interior gaps filled, as a plain + * series does. Every other guard holds for `compareTo` as for a plain series. * * The window is the axis's own `timeDimensions[].dateRange`, read in the * zone {@link buildQuery} resolves the query in. A window stated only in @@ -1567,7 +1577,7 @@ export class DatasetExecutor { order: Record | undefined, context?: ExecutionContext, ): TimeBucketFillPlan | undefined { - if (selection.compareTo || selection.limit != null || selection.offset != null) return undefined; + if (selection.limit != null || selection.offset != null) return undefined; if (Object.keys(order ?? {}).some((key) => !dimensions.includes(key))) return undefined; const bucketed = dimensions.flatMap((d) => { const granularity = this.granularityOf(compiled, selection, d);