Skip to content

Commit 11e941d

Browse files
authored
fix(schema-compiler): correct origin timestamp initialization in Granularity instance (#8710)
* remove doubled query.newTimeDimension() call in transformQueryToCanUseForm * fix(schema-compiler): correct origin timestamp initialization in Granularity instance * tests for custom preagg matches
1 parent e5dec22 commit 11e941d

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

packages/cubejs-schema-compiler/src/adapter/Granularity.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class Granularity {
2525
) {
2626
this.granularity = timeDimension.granularity;
2727
this.predefinedGranularity = isPredefinedGranularity(this.granularity);
28-
this.origin = moment().startOf('year'); // Defaults to current year start
28+
this.origin = moment.tz('UTC').startOf('year'); // Defaults to current year start
2929

3030
if (this.predefinedGranularity) {
3131
this.granularityInterval = `1 ${this.granularity}`;
@@ -60,17 +60,17 @@ export class Granularity {
6060
return this.granularity;
6161
}
6262

63-
if (this.origin) {
63+
if (this.granularityOffset) {
6464
return this.query.minGranularity(
6565
this.granularityFromInterval(),
66-
this.query.granularityFor(this.origin.utc())
66+
this.granularityFromOffset()
6767
);
6868
}
6969

70-
if (this.granularityOffset) {
70+
if (this.origin) {
7171
return this.query.minGranularity(
7272
this.granularityFromInterval(),
73-
this.granularityFromOffset()
73+
this.query.granularityFor(this.origin.utc())
7474
);
7575
}
7676

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ export class PreAggregations {
314314
dateRange: td.dateRange,
315315
granularity: td.granularity,
316316
});
317-
}).map(d => query.newTimeDimension(d));
317+
});
318318

319319
let sortedAllCubeNames;
320320
let sortedUsedCubePrimaryKeys;

packages/cubejs-schema-compiler/test/unit/pre-agg-time-dim-match.test.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,24 @@ describe('Pre Aggregation by filter match tests', () => {
99

1010
cube.dimensions.created = {
1111
sql: 'created',
12-
type: 'time'
12+
type: 'time',
13+
granularities: {
14+
one_week: {
15+
interval: '1 week',
16+
},
17+
one_week_by_sunday: {
18+
interval: '1 week',
19+
offset: '-1 day'
20+
},
21+
two_weeks_by_1st_feb_00am: {
22+
interval: '2 weeks',
23+
origin: '2024-02-01 00:00:00'
24+
},
25+
two_weeks_by_1st_feb_10am: {
26+
interval: '2 weeks',
27+
origin: '2024-02-01 10:00:00'
28+
}
29+
}
1330
};
1431

1532
return prepareCube('cube', cube);
@@ -20,6 +37,7 @@ describe('Pre Aggregation by filter match tests', () => {
2037
measures: Array<String>,
2138
preAggTimeGranularity: string,
2239
queryAggTimeGranularity: string,
40+
queryTimeZone: string = 'America/Los_Angeles',
2341
) {
2442
const aaa: any = {
2543
type: 'rollup',
@@ -28,6 +46,8 @@ describe('Pre Aggregation by filter match tests', () => {
2846
timeDimension: 'cube.created',
2947
granularity: preAggTimeGranularity,
3048
partitionGranularity: 'year',
49+
// Enabling only for custom granularities
50+
allowNonStrictDateRangeMatch: !/^(minute|hour|day|week|month|quarter|year)$/.test(preAggTimeGranularity)
3151
};
3252

3353
const cube: any = {
@@ -55,7 +75,7 @@ describe('Pre Aggregation by filter match tests', () => {
5575
granularity: queryAggTimeGranularity,
5676
dateRange: { from: '2017-01-01', to: '2017-03-31' }
5777
}],
58-
timezone: 'America/Los_Angeles',
78+
timezone: queryTimeZone,
5979
});
6080

6181
const usePreAggregation = PreAggregations.canUsePreAggregationForTransformedQueryFn(
@@ -71,6 +91,26 @@ describe('Pre Aggregation by filter match tests', () => {
7191
true, ['count'], 'day', 'day'
7292
));
7393

94+
it('1 count measure, one_week_by_sunday, one_week_by_sunday', () => testPreAggregationMatch(
95+
true, ['count'], 'one_week_by_sunday', 'one_week_by_sunday'
96+
));
97+
98+
it('1 count measure, two_weeks_by_1st_feb_00am, two_weeks_by_1st_feb_00am', () => testPreAggregationMatch(
99+
true, ['count'], 'two_weeks_by_1st_feb_00am', 'two_weeks_by_1st_feb_00am'
100+
));
101+
102+
it('1 count measure, day, one_week_by_sunday', () => testPreAggregationMatch(
103+
true, ['count'], 'day', 'one_week_by_sunday'
104+
));
105+
106+
it('1 count measure, day, two_weeks_by_1st_feb_00am', () => testPreAggregationMatch(
107+
true, ['count'], 'day', 'two_weeks_by_1st_feb_00am'
108+
));
109+
110+
it('1 count measure, day, two_weeks_by_1st_feb_10am', () => testPreAggregationMatch(
111+
false, ['count'], 'day', 'two_weeks_by_1st_feb_10am'
112+
));
113+
74114
it('1 count measure, week, day', () => testPreAggregationMatch(
75115
false, ['count'], 'week', 'day'
76116
));

0 commit comments

Comments
 (0)