Skip to content

Commit 5e84c10

Browse files
committed
feat(schema-compiler): Add support for time dimensions with granularities in multi-stage measures add_group_by
1 parent 1104bde commit 5e84c10

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,22 @@ export class BaseQuery {
14531453
const memberDef = member.definition();
14541454
// TODO can addGroupBy replaced by something else?
14551455
if (memberDef.addGroupByReferences) {
1456-
queryContext = { ...queryContext, dimensions: R.uniq(queryContext.dimensions.concat(memberDef.addGroupByReferences)) };
1456+
const dims = memberDef.addGroupByReferences.reduce((acc, cur) => {
1457+
const pathArr = cur.split('.');
1458+
// addGroupBy may include time dimension with granularity
1459+
// But we don't need it as time dimension
1460+
if (pathArr.length > 2) {
1461+
pathArr.splice(2, 0, 'granularities');
1462+
acc.push(pathArr.join('.'));
1463+
} else {
1464+
acc.push(cur);
1465+
}
1466+
return acc;
1467+
}, []);
1468+
queryContext = {
1469+
...queryContext,
1470+
dimensions: R.uniq(queryContext.dimensions.concat(dims)),
1471+
};
14571472
}
14581473
if (memberDef.timeShiftReferences?.length) {
14591474
let { commonTimeShift } = queryContext;

packages/cubejs-schema-compiler/src/compiler/CubeSymbols.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ export class CubeSymbols {
683683
return cubeEvaluator.pathFromArray(fullPath(cubeEvaluator.joinHints(), [referencedCube, name]));
684684
}, {
685685
// eslint-disable-next-line no-shadow
686-
sqlResolveFn: (symbol, currentCube, n) => cubeEvaluator.pathFromArray(fullPath(cubeEvaluator.joinHints(), [currentCube, n])),
686+
sqlResolveFn: (symbol, currentCube, refProperty, propertyName) => cubeEvaluator.pathFromArray(fullPath(cubeEvaluator.joinHints(), [currentCube, refProperty, ...(propertyName ? [propertyName] : [])])),
687687
// eslint-disable-next-line no-shadow
688688
cubeAliasFn: (currentCube) => cubeEvaluator.pathFromArray(fullPath(cubeEvaluator.joinHints(), [currentCube])),
689689
collectJoinHints: options.collectJoinHints,

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ describe('SQL Generation', () => {
231231
type: 'sum',
232232
add_group_by: [visitors.created_at],
233233
},
234+
revenue_sum_group_by_granularity: {
235+
multi_stage: true,
236+
sql: \`\${revenue}\`,
237+
type: 'number',
238+
add_group_by: [visitors.created_at.month],
239+
},
234240
revenue_rank: {
235241
multi_stage: true,
236242
type: \`rank\`,
@@ -3458,6 +3464,33 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
34583464
}]
34593465
));
34603466

3467+
it('multi stage revenue_sum_group_by_granularity and group by td with granularity', async () => runQueryTest(
3468+
{
3469+
measures: ['visitors.revenue_sum_group_by_granularity'],
3470+
dimensions: ['visitors.source'],
3471+
order: [{
3472+
id: 'visitors.source'
3473+
}],
3474+
timezone: 'UTC',
3475+
},
3476+
[{
3477+
visitors__revenue_sum_group_by_granularity: '300',
3478+
visitors__source: 'google',
3479+
},
3480+
{
3481+
visitors__revenue_sum_group_by_granularity: '300',
3482+
visitors__source: 'some',
3483+
},
3484+
{
3485+
visitors__revenue_sum_group_by_granularity: '900',
3486+
visitors__source: null,
3487+
},
3488+
{
3489+
visitors__revenue_sum_group_by_granularity: '500',
3490+
visitors__source: null,
3491+
}]
3492+
));
3493+
34613494
it('multi stage complex graph with time dimension no granularity', async () => runQueryTest(
34623495
{
34633496
measures: ['visitors.adjusted_rank_sum', 'visitors.visitor_revenue'],

0 commit comments

Comments
 (0)