Skip to content

Commit 056a1d8

Browse files
authored
fix(schema-compiler): Handle member expressions in keyDimensions (#9083)
Use `definition` as key for member expressions. Before this, if dimension is member expression, `d.dimension` would be object, and key would be '[object Object]' string, so separate dimensions would have same key, and all but one will be eliminated
1 parent c5aa6cc commit 056a1d8

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,14 @@ export class BaseQuery {
18551855
keyDimensions(primaryKeyDimensions) {
18561856
// The same dimension with different granularities maybe requested, so it's not enough to filter only by dimension
18571857
return R.uniqBy(
1858-
(d) => `${d.dimension}${d.granularity ?? ''}`, this.dimensionsForSelect()
1858+
(d) => {
1859+
if (d.isMemberExpression) {
1860+
return d.dimension.definition;
1861+
}
1862+
1863+
return `${d.dimension}${d.granularity ?? ''}`;
1864+
},
1865+
this.dimensionsForSelect()
18591866
.concat(primaryKeyDimensions)
18601867
);
18611868
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2974,6 +2974,63 @@ describe('SQL Generation', () => {
29742974
}]
29752975
));
29762976

2977+
// Subquery aggregation for multiplied measure (and any `keysSelect` for that matter)
2978+
// should pick up all dimensions, even through member expressions
2979+
it('multiplied sum with dimension member expressions', async () => runQueryTest(
2980+
{
2981+
measures: [
2982+
'visitors_visitors_checkins_view.revenue',
2983+
'visitors_visitors_checkins_view.visitor_checkins_count',
2984+
],
2985+
dimensions: [
2986+
{
2987+
// eslint-disable-next-line no-new-func
2988+
expression: new Function(
2989+
'visitors_visitors_checkins_view',
2990+
// eslint-disable-next-line no-template-curly-in-string
2991+
'return `LOWER(${visitors_visitors_checkins_view.source})`'
2992+
),
2993+
expressionName: 'lower_source',
2994+
// eslint-disable-next-line no-template-curly-in-string
2995+
definition: 'LOWER(${visitors_visitors_checkins_view.source})',
2996+
cubeName: 'visitors_visitors_checkins_view',
2997+
},
2998+
{
2999+
// eslint-disable-next-line no-new-func
3000+
expression: new Function(
3001+
'visitors_visitors_checkins_view',
3002+
// eslint-disable-next-line no-template-curly-in-string
3003+
'return `UPPER(${visitors_visitors_checkins_view.source})`'
3004+
),
3005+
expressionName: 'upper_source',
3006+
// eslint-disable-next-line no-template-curly-in-string
3007+
definition: 'UPPER(${visitors_visitors_checkins_view.source})',
3008+
cubeName: 'visitors_visitors_checkins_view',
3009+
},
3010+
],
3011+
},
3012+
[
3013+
{
3014+
lower_source: null,
3015+
upper_source: null,
3016+
visitors_visitors_checkins_view__revenue: '1400',
3017+
visitors_visitors_checkins_view__visitor_checkins_count: '0',
3018+
},
3019+
{
3020+
lower_source: 'google',
3021+
upper_source: 'GOOGLE',
3022+
visitors_visitors_checkins_view__revenue: '300',
3023+
visitors_visitors_checkins_view__visitor_checkins_count: '1',
3024+
},
3025+
{
3026+
lower_source: 'some',
3027+
upper_source: 'SOME',
3028+
visitors_visitors_checkins_view__revenue: '300',
3029+
visitors_visitors_checkins_view__visitor_checkins_count: '5',
3030+
},
3031+
]
3032+
));
3033+
29773034
// TODO not implemented
29783035
// it('multi stage bucketing', async () => runQueryTest(
29793036
// {

0 commit comments

Comments
 (0)