Skip to content

Commit bacc643

Browse files
authored
fix: Invalid identifier day for month lambdaRollup (cube-js#5338)
* fix: Invalid identifier day for month `lambdaRollup` * chore: Invalid identifier day for month `lambdaRollup`
1 parent a81ca35 commit bacc643

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export class PreAggregations {
144144
}
145145

146146
preAggregationDescriptionFor(cube, foundPreAggregation) {
147-
const { preAggregationName, preAggregation } = foundPreAggregation;
147+
const { preAggregationName, preAggregation, references } = foundPreAggregation;
148148

149149
const tableName = this.preAggregationTableName(cube, preAggregationName, preAggregation);
150150
const invalidateKeyQueries = this.query.preAggregationInvalidateKeyQueries(cube, preAggregation);
@@ -185,10 +185,11 @@ export class PreAggregations {
185185
uniqueKeyColumns,
186186
aggregationsColumns,
187187
dataSource: queryForSqlEvaluation.dataSource,
188-
granularity: preAggregation.granularity,
188+
// in fact we can reference preAggregation.granularity however accessing timeDimensions is more strict and consistent
189+
granularity: references.timeDimensions[0]?.granularity,
189190
partitionGranularity: preAggregation.partitionGranularity,
190191
preAggregationStartEndQueries:
191-
(preAggregation.partitionGranularity || preAggregation.granularity) &&
192+
(preAggregation.partitionGranularity || references.timeDimensions[0]?.granularity) &&
192193
this.refreshRangeQuery().preAggregationStartEndQueries(cube, preAggregation),
193194
matchedTimeDimensionDateRange:
194195
preAggregation.partitionGranularity && (
@@ -1072,7 +1073,7 @@ export class PreAggregations {
10721073
)(this.rollupMeasures(preAggregationForQuery));
10731074

10741075
// TODO granularity shouldn't be null?
1075-
const rollupGranularity = this.castGranularity(preAggregationForQuery.preAggregation.granularity) || 'day';
1076+
const rollupGranularity = preAggregationForQuery.references.timeDimensions[0]?.granularity || 'day';
10761077

10771078
return this.query.evaluateSymbolSqlWithContext(
10781079
// eslint-disable-next-line prefer-template

packages/cubejs-testing/birdbox-fixtures/lambda/schema/Orders.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ cube(`Orders`, {
22
sql: `SELECT * FROM public.orders`,
33

44
preAggregations: {
5+
ordersByCompletedAtMonthLambda: {
6+
type: `rollupLambda`,
7+
rollups: [ordersByCompletedAtMonth],
8+
unionWithSourceData: true,
9+
},
10+
511
ordersByCompletedAtLambda: {
612
type: `rollupLambda`,
713
rollups: [ordersByCompletedAt],
@@ -18,6 +24,22 @@ cube(`Orders`, {
1824
unionWithSourceData: true,
1925
},
2026

27+
ordersByCompletedAtMonth: {
28+
measures: [count],
29+
timeDimension: completedAt,
30+
granularity: `month`,
31+
partitionGranularity: `month`,
32+
buildRangeStart: {
33+
sql: `SELECT DATE('2020-02-7')`,
34+
},
35+
buildRangeEnd: {
36+
sql: `SELECT DATE('2020-05-7')`,
37+
},
38+
refreshKey: {
39+
every: '1 day'
40+
},
41+
},
42+
2143
ordersByCompletedAt: {
2244
measures: [count],
2345
dimensions: [status],

packages/cubejs-testing/test/smoke-lambda.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,49 @@ describe('lambda', () => {
214214
);
215215
});
216216

217+
test('query month', async () => {
218+
const query: Query = {
219+
measures: ['Orders.count'],
220+
timeDimensions: [
221+
{
222+
dimension: 'Orders.completedAt',
223+
granularity: 'month'
224+
}
225+
],
226+
order: {
227+
'Orders.completedAt': 'desc',
228+
},
229+
limit: 3
230+
};
231+
const response = await client.load(query);
232+
233+
// @ts-ignore
234+
expect(Object.keys(response.loadResponse.results[0].usedPreAggregations)).toEqual([
235+
'dev_pre_aggregations.orders_orders_by_completed_at_month'
236+
]);
237+
238+
// With lambda-view we observe all 'fresh' data, with no partition/buildRange limit.
239+
expect(response.rawData()).toEqual(
240+
[
241+
{
242+
'Orders.completedAt': '2021-12-01T00:00:00.000',
243+
'Orders.completedAt.month': '2021-12-01T00:00:00.000',
244+
'Orders.count': '2',
245+
},
246+
{
247+
'Orders.completedAt': '2021-01-01T00:00:00.000',
248+
'Orders.completedAt.month': '2021-01-01T00:00:00.000',
249+
'Orders.count': '127',
250+
},
251+
{
252+
'Orders.completedAt': '2020-12-01T00:00:00.000',
253+
'Orders.completedAt.month': '2020-12-01T00:00:00.000',
254+
'Orders.count': '808',
255+
},
256+
]
257+
);
258+
});
259+
217260
test('query with 2 dimensions', async () => {
218261
const response = await client.load({
219262
measures: ['Orders.count'],

0 commit comments

Comments
 (0)