Skip to content

Commit a668184

Browse files
committed
test for custom granularity match
1 parent ff40856 commit a668184

File tree

3 files changed

+98
-14
lines changed

3 files changed

+98
-14
lines changed

packages/cubejs-schema-compiler/test/integration/postgres/pre-aggregations.test.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,13 @@ describe('PreAggregations', () => {
9494
},
9595
createdAt: {
9696
type: 'time',
97-
sql: 'created_at'
97+
sql: 'created_at',
98+
granularities: {
99+
hourTenMinOffset: {
100+
interval: '1 hour',
101+
offset: '10 minutes'
102+
}
103+
}
98104
},
99105
signedUpAt: {
100106
type: 'time',
@@ -224,6 +230,11 @@ describe('PreAggregations', () => {
224230
granularity: 'hour',
225231
partitionGranularity: 'month'
226232
},
233+
countCustomGranularity: {
234+
measures: [count],
235+
timeDimension: createdAt,
236+
granularity: 'hourTenMinOffset'
237+
},
227238
sourceAndIdRollup: {
228239
measures: [count],
229240
dimensions: [sourceAndId, source],
@@ -590,6 +601,49 @@ describe('PreAggregations', () => {
590601
});
591602
}));
592603

604+
it('simple pre-aggregation with custom granularity (exact match)', () => compiler.compile().then(() => {
605+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
606+
measures: [
607+
'visitors.count'
608+
],
609+
timeDimensions: [{
610+
dimension: 'visitors.createdAt',
611+
dateRange: ['2017-01-01 00:10:00.000', '2017-01-29 22:09:59.999'],
612+
granularity: 'hourTenMinOffset',
613+
}],
614+
timezone: 'America/Los_Angeles',
615+
preAggregationsSchema: ''
616+
});
617+
618+
const queryAndParams = query.buildSqlAndParams();
619+
console.log(queryAndParams);
620+
expect(query.preAggregations?.preAggregationForQuery?.canUsePreAggregation).toEqual(true);
621+
expect(queryAndParams[0]).toMatch(/visitors_count_custom_granularity/);
622+
623+
return dbRunner.evaluateQueryWithPreAggregations(query).then(res => {
624+
expect(res).toEqual(
625+
[
626+
{
627+
visitors__count: '1',
628+
visitors__created_at_hourTenMinOffset: '2017-01-02T15:10:00.000Z',
629+
},
630+
{
631+
visitors__count: '1',
632+
visitors__created_at_hourTenMinOffset: '2017-01-04T15:10:00.000Z',
633+
},
634+
{
635+
visitors__count: '1',
636+
visitors__created_at_hourTenMinOffset: '2017-01-05T15:10:00.000Z',
637+
},
638+
{
639+
visitors__count: '2',
640+
visitors__created_at_hourTenMinOffset: '2017-01-06T15:10:00.000Z',
641+
},
642+
]
643+
);
644+
});
645+
}));
646+
593647
it('leaf measure pre-aggregation', () => compiler.compile().then(() => {
594648
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
595649
measures: [

packages/cubejs-schema-compiler/test/unit/base-query.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ describe('SQL Generation', () => {
498498
'orders.status'
499499
],
500500
filters: [],
501-
timezone: 'Europe/Kyiv'
501+
timezone: 'UTC'
502502
},
503503
{
504504
measures: [
@@ -518,7 +518,7 @@ describe('SQL Generation', () => {
518518
'orders.status'
519519
],
520520
filters: [],
521-
timezone: 'Europe/Kyiv'
521+
timezone: 'UTC'
522522
},
523523
{
524524
measures: [
@@ -538,7 +538,7 @@ describe('SQL Generation', () => {
538538
'orders.status'
539539
],
540540
filters: [],
541-
timezone: 'Europe/Kyiv'
541+
timezone: 'UTC'
542542
},
543543
{
544544
measures: [
@@ -558,7 +558,7 @@ describe('SQL Generation', () => {
558558
'orders.status'
559559
],
560560
filters: [],
561-
timezone: 'Europe/Kyiv'
561+
timezone: 'UTC'
562562
},
563563
// requesting via view
564564
{

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

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ describe('Pre Aggregation by filter match tests', () => {
1616
},
1717
one_week_by_sunday: {
1818
interval: '1 week',
19-
offset: '-1 day'
19+
// offset: '-1 day' // offsets might lead to flaky tests through years
20+
origin: '2025-01-05 00:00:00'
2021
},
2122
two_weeks_by_1st_feb_00am: {
2223
interval: '2 weeks',
@@ -38,6 +39,8 @@ describe('Pre Aggregation by filter match tests', () => {
3839
preAggTimeGranularity: string,
3940
queryAggTimeGranularity: string,
4041
queryTimeZone: string = 'America/Los_Angeles',
42+
dateRange: [ string, string ] = ['2017-01-01', '2017-03-31'],
43+
allowNonStrictDateRangeMatch: boolean = false
4144
) {
4245
const aaa: any = {
4346
type: 'rollup',
@@ -46,8 +49,7 @@ describe('Pre Aggregation by filter match tests', () => {
4649
timeDimension: 'cube.created',
4750
granularity: preAggTimeGranularity,
4851
partitionGranularity: 'year',
49-
// Enabling only for custom granularities
50-
allowNonStrictDateRangeMatch: !/^(minute|hour|day|week|month|quarter|year)$/.test(preAggTimeGranularity)
52+
allowNonStrictDateRangeMatch
5153
};
5254

5355
const cube: any = {
@@ -65,15 +67,15 @@ describe('Pre Aggregation by filter match tests', () => {
6567

6668
// aaa.sortedDimensions = aaa.dimensions;
6769
// aaa.sortedDimensions.sort();
68-
aaa.sortedTimeDimensions = [[aaa.timeDimension, aaa.granularity]];
70+
aaa.sortedTimeDimensions = [[aaa.timeDimension, aaa.granularity, 'day']];
6971

7072
return compiler.compile().then(() => {
7173
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
7274
measures: measures.map(m => `cube.${m}`),
7375
timeDimensions: [{
7476
dimension: 'cube.created',
7577
granularity: queryAggTimeGranularity,
76-
dateRange: { from: '2017-01-01', to: '2017-03-31' }
78+
dateRange,
7779
}],
7880
timezone: queryTimeZone,
7981
});
@@ -92,15 +94,43 @@ describe('Pre Aggregation by filter match tests', () => {
9294
));
9395

9496
it('1 count measure, one_week_by_sunday, one_week_by_sunday', () => testPreAggregationMatch(
95-
true, ['count'], 'one_week_by_sunday', 'one_week_by_sunday'
97+
true,
98+
['count'],
99+
'one_week_by_sunday',
100+
'one_week_by_sunday',
101+
'UTC',
102+
['2024-02-11', '2024-03-02']
96103
));
97104

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'
105+
it('1 count measure, one_week_by_sunday, one_week_by_sunday (dst)', () => testPreAggregationMatch(
106+
true,
107+
['count'],
108+
'one_week_by_sunday',
109+
'one_week_by_sunday',
110+
'UTC',
111+
['2024-02-25', '2024-03-30'], // DST Switch happens here, but still must work!
112+
));
113+
114+
it('1 count measure, two_weeks_by_1st_feb_00am, two_weeks_by_1st_feb_00am (match)', () => testPreAggregationMatch(
115+
true,
116+
['count'],
117+
'two_weeks_by_1st_feb_00am',
118+
'two_weeks_by_1st_feb_00am',
119+
'UTC',
120+
['2024-01-18', '2024-02-28']
121+
));
122+
123+
it('1 count measure, two_weeks_by_1st_feb_00am, two_weeks_by_1st_feb_00am (miss)', () => testPreAggregationMatch(
124+
false,
125+
['count'],
126+
'two_weeks_by_1st_feb_00am',
127+
'two_weeks_by_1st_feb_00am',
128+
'UTC',
129+
['2024-01-18', '2024-02-07'], // Interval not aligned
100130
));
101131

102132
it('1 count measure, day, one_week_by_sunday', () => testPreAggregationMatch(
103-
true, ['count'], 'day', 'one_week_by_sunday'
133+
true, ['count'], 'day', 'one_week_by_sunday', 'UTC'
104134
));
105135

106136
it('1 count measure, day, two_weeks_by_1st_feb_00am', () => testPreAggregationMatch(

0 commit comments

Comments
 (0)