Skip to content

Commit d3c28d4

Browse files
authored
fix(schema-compiler): Fix view queries for measure with single timeshift reference without time dimension (#9565)
1 parent ff595fb commit d3c28d4

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

packages/cubejs-schema-compiler/src/adapter/BaseQuery.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,10 +1428,15 @@ export class BaseQuery {
14281428
if (memberDef.timeShiftReferences?.length) {
14291429
let mapFn;
14301430

1431+
const allBackAliasMembers = this.allBackAliasTimeDimensions();
1432+
14311433
if (memberDef.timeShiftReferences.length === 1 && !memberDef.timeShiftReferences[0].timeDimension) {
14321434
const timeShift = memberDef.timeShiftReferences[0];
14331435
mapFn = (td) => {
1434-
if (td.shiftInterval) {
1436+
// We need to ignore aliased td, because it will match and insert shiftInterval on first
1437+
// occurrence, but later during recursion it will hit the original td but shiftInterval will be
1438+
// present and simple check for td.shiftInterval will always result in error.
1439+
if (td.shiftInterval && !td.dimension === allBackAliasMembers[timeShift.timeDimension]) {
14351440
throw new UserError(`Hierarchical time shift is not supported but was provided for '${td.dimension}'. Parent time shift is '${td.shiftInterval}' and current is '${timeShift.interval}'`);
14361441
}
14371442
return {
@@ -1440,7 +1445,6 @@ export class BaseQuery {
14401445
};
14411446
};
14421447
} else {
1443-
const allBackAliasMembers = this.allBackAliasTimeDimensions();
14441448
mapFn = (td) => {
14451449
const timeShift = memberDef.timeShiftReferences.find(r => r.timeDimension === td.dimension || td.dimension === allBackAliasMembers[r.timeDimension]);
14461450
if (timeShift) {

packages/cubejs-schema-compiler/test/integration/postgres/sql-generation.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,33 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
14441444
{ visitors_multi_stage__create_date_created_at_day: '2017-01-06T00:00:00.000Z', visitors_multi_stage__revenue: '900', visitors_multi_stage__revenue_day_ago_via_join: '300' }
14451445
]));
14461446

1447+
it('CAGR (no td in time_shift via view)', async () => runQueryTest({
1448+
measures: [
1449+
'visitors_multi_stage.revenue',
1450+
'visitors_multi_stage.revenue_day_ago_no_td',
1451+
],
1452+
timeDimensions: [{
1453+
dimension: 'visitors_multi_stage.create_date_created_at',
1454+
granularity: 'day',
1455+
dateRange: ['2016-12-01', '2017-01-31']
1456+
}],
1457+
order: [{
1458+
id: 'visitors_multi_stage.create_date_created_at'
1459+
}],
1460+
timezone: 'America/Los_Angeles'
1461+
}, [
1462+
{
1463+
visitors_multi_stage__create_date_created_at_day: '2017-01-05T00:00:00.000Z',
1464+
visitors_multi_stage__revenue: '300',
1465+
visitors_multi_stage__revenue_day_ago_no_td: '200',
1466+
},
1467+
{
1468+
visitors_multi_stage__create_date_created_at_day: '2017-01-06T00:00:00.000Z',
1469+
visitors_multi_stage__revenue: '900',
1470+
visitors_multi_stage__revenue_day_ago_no_td: '300',
1471+
}
1472+
]));
1473+
14471474
it('sql utils', async () => runQueryTest({
14481475
measures: [
14491476
'visitors.visitor_count'

0 commit comments

Comments
 (0)