|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// Regression (ADR-0053 Phase 2, #1982): on a SQL driver — where BOTH |
| 4 | +// `nativeSql` and `objectqlAggregate` capabilities are advertised (the real |
| 5 | +// SQLite/Postgres/MySQL deployment) — a date-bucketed query must route to the |
| 6 | +// ObjectQLStrategy so `engine.aggregate` buckets by granularity and honours a |
| 7 | +// non-UTC reference timezone (in-memory). NativeSQLStrategy groups by the raw |
| 8 | +// column (`GROUP BY <col>`, no `date_trunc`) and ignores `timezone`; because it |
| 9 | +// has higher precedence (priority 10 < 20) it silently won every cube/dataset |
| 10 | +// query, producing one bucket per row and tz-invariant results. The earlier |
| 11 | +// granularity test passed only because it forced `nativeSql: false`, masking |
| 12 | +// the real misrouting. This test pins the production capability shape. |
| 13 | + |
| 14 | +import { describe, it, expect } from 'vitest'; |
| 15 | +import { DatasetSchema } from '@objectstack/spec/ui'; |
| 16 | +import { AnalyticsService } from '../analytics-service.js'; |
| 17 | + |
| 18 | +const dataset = DatasetSchema.parse({ |
| 19 | + name: 'pipeline', |
| 20 | + label: 'Pipeline', |
| 21 | + object: 'opportunity', |
| 22 | + dimensions: [ |
| 23 | + { name: 'stage', field: 'stage', type: 'string' }, |
| 24 | + { name: 'close_date', field: 'close_date', type: 'date' }, |
| 25 | + ], |
| 26 | + measures: [{ name: 'opp_count', aggregate: 'count' }], |
| 27 | +}); |
| 28 | + |
| 29 | +// Mirror a real SQL deployment: native SQL AND objectql aggregate both available. |
| 30 | +const PROD_CAPS = { nativeSql: true, objectqlAggregate: true, inMemory: true }; |
| 31 | + |
| 32 | +function buildService() { |
| 33 | + const rawSqlCalls: Array<{ sql: string }> = []; |
| 34 | + const aggregateCalls: Array<{ groupBy?: unknown[]; timezone?: string }> = []; |
| 35 | + const svc = new AnalyticsService({ |
| 36 | + queryCapabilities: () => PROD_CAPS, |
| 37 | + executeRawSql: async (_object, sql) => { |
| 38 | + rawSqlCalls.push({ sql }); |
| 39 | + return [{ 'close_date': '2026-06-17T01:44:15.667Z', opp_count: 1 }]; |
| 40 | + }, |
| 41 | + executeAggregate: async (_object, options) => { |
| 42 | + aggregateCalls.push({ groupBy: options.groupBy as unknown[], timezone: options.timezone }); |
| 43 | + return [{ close_date: '2026-06', opp_count: 6 }]; |
| 44 | + }, |
| 45 | + }); |
| 46 | + return { svc, rawSqlCalls, aggregateCalls }; |
| 47 | +} |
| 48 | + |
| 49 | +describe('NativeSQLStrategy declines date-granularity queries (regression #1982)', () => { |
| 50 | + it('routes a granularity timeDimension to engine.aggregate, NOT raw SQL', async () => { |
| 51 | + const { svc, rawSqlCalls, aggregateCalls } = buildService(); |
| 52 | + |
| 53 | + await svc.queryDataset!(dataset, { |
| 54 | + dimensions: ['close_date'], |
| 55 | + measures: ['opp_count'], |
| 56 | + timeDimensions: [{ dimension: 'close_date', granularity: 'month' }], |
| 57 | + timezone: 'America/New_York', |
| 58 | + }); |
| 59 | + |
| 60 | + // ObjectQLStrategy handled it (native SQL would have lost the bucket + tz). |
| 61 | + expect(rawSqlCalls).toHaveLength(0); |
| 62 | + expect(aggregateCalls).toHaveLength(1); |
| 63 | + // Bucketed groupBy + reference tz reach the engine. |
| 64 | + expect(aggregateCalls[0].groupBy).toEqual([{ field: 'close_date', dateGranularity: 'month' }]); |
| 65 | + expect(aggregateCalls[0].timezone).toBe('America/New_York'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('still uses native SQL for a plain (non-bucketed) query', async () => { |
| 69 | + const { svc, rawSqlCalls, aggregateCalls } = buildService(); |
| 70 | + |
| 71 | + await svc.queryDataset!(dataset, { |
| 72 | + dimensions: ['stage'], |
| 73 | + measures: ['opp_count'], |
| 74 | + }); |
| 75 | + |
| 76 | + // No granularity → native SQL is correct and preferred; we did not over-decline. |
| 77 | + expect(rawSqlCalls).toHaveLength(1); |
| 78 | + expect(aggregateCalls).toHaveLength(0); |
| 79 | + }); |
| 80 | +}); |
0 commit comments