Skip to content

Commit 6700b43

Browse files
authored
feat(schema-compiler): Add support for time dimensions with granularities in multi-stage measures add_group_by (#9657)
1 parent 97b6bb4 commit 6700b43

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
@@ -1462,7 +1462,22 @@ export class BaseQuery {
14621462
const memberDef = member.definition();
14631463
// TODO can addGroupBy replaced by something else?
14641464
if (memberDef.addGroupByReferences) {
1465-
queryContext = { ...queryContext, dimensions: R.uniq(queryContext.dimensions.concat(memberDef.addGroupByReferences)) };
1465+
const dims = memberDef.addGroupByReferences.reduce((acc, cur) => {
1466+
const pathArr = cur.split('.');
1467+
// addGroupBy may include time dimension with granularity
1468+
// But we don't need it as time dimension
1469+
if (pathArr.length > 2) {
1470+
pathArr.splice(2, 0, 'granularities');
1471+
acc.push(pathArr.join('.'));
1472+
} else {
1473+
acc.push(cur);
1474+
}
1475+
return acc;
1476+
}, []);
1477+
queryContext = {
1478+
...queryContext,
1479+
dimensions: R.uniq(queryContext.dimensions.concat(dims)),
1480+
};
14661481
}
14671482
if (memberDef.timeShiftReferences?.length) {
14681483
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\`,
@@ -3461,6 +3467,33 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
34613467
}]
34623468
));
34633469

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

0 commit comments

Comments
 (0)