From cfef903167f13928fa8f21cf39ecd86bd6f9d738 Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Mon, 16 Sep 2024 11:51:55 +0300 Subject: [PATCH 1/3] =?UTF-8?q?remove=20doubled=20query.newTimeDimension()?= =?UTF-8?q?=20call=20in=C2=A0transformQueryToCanUseForm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cubejs-schema-compiler/src/adapter/PreAggregations.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cubejs-schema-compiler/src/adapter/PreAggregations.js b/packages/cubejs-schema-compiler/src/adapter/PreAggregations.js index bb8905aab3203..9f5af0ba7d87f 100644 --- a/packages/cubejs-schema-compiler/src/adapter/PreAggregations.js +++ b/packages/cubejs-schema-compiler/src/adapter/PreAggregations.js @@ -314,7 +314,7 @@ export class PreAggregations { dateRange: td.dateRange, granularity: td.granularity, }); - }).map(d => query.newTimeDimension(d)); + }); let sortedAllCubeNames; let sortedUsedCubePrimaryKeys; From 985a6fcfd927385194b567170be0b5ac67c44b71 Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Mon, 16 Sep 2024 13:34:23 +0300 Subject: [PATCH 2/3] =?UTF-8?q?fix(schema-compiler):=20correct=20origin=20?= =?UTF-8?q?timestamp=20initialization=20in=C2=A0Granularity=20instance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cubejs-schema-compiler/src/adapter/Granularity.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/cubejs-schema-compiler/src/adapter/Granularity.ts b/packages/cubejs-schema-compiler/src/adapter/Granularity.ts index bde96b5ade8bb..a8f32943d038a 100644 --- a/packages/cubejs-schema-compiler/src/adapter/Granularity.ts +++ b/packages/cubejs-schema-compiler/src/adapter/Granularity.ts @@ -25,7 +25,7 @@ export class Granularity { ) { this.granularity = timeDimension.granularity; this.predefinedGranularity = isPredefinedGranularity(this.granularity); - this.origin = moment().startOf('year'); // Defaults to current year start + this.origin = moment.tz('UTC').startOf('year'); // Defaults to current year start if (this.predefinedGranularity) { this.granularityInterval = `1 ${this.granularity}`; @@ -60,17 +60,17 @@ export class Granularity { return this.granularity; } - if (this.origin) { + if (this.granularityOffset) { return this.query.minGranularity( this.granularityFromInterval(), - this.query.granularityFor(this.origin.utc()) + this.granularityFromOffset() ); } - if (this.granularityOffset) { + if (this.origin) { return this.query.minGranularity( this.granularityFromInterval(), - this.granularityFromOffset() + this.query.granularityFor(this.origin.utc()) ); } From 96c043caa50d26dc7101f1265fd0b5457b407d39 Mon Sep 17 00:00:00 2001 From: Konstantin Burkalev Date: Mon, 16 Sep 2024 13:34:42 +0300 Subject: [PATCH 3/3] =?UTF-8?q?tests=20for=C2=A0custom=20preagg=20matches?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/unit/pre-agg-time-dim-match.test.ts | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/cubejs-schema-compiler/test/unit/pre-agg-time-dim-match.test.ts b/packages/cubejs-schema-compiler/test/unit/pre-agg-time-dim-match.test.ts index 334a2fe3195a3..fe346c685368d 100644 --- a/packages/cubejs-schema-compiler/test/unit/pre-agg-time-dim-match.test.ts +++ b/packages/cubejs-schema-compiler/test/unit/pre-agg-time-dim-match.test.ts @@ -9,7 +9,24 @@ describe('Pre Aggregation by filter match tests', () => { cube.dimensions.created = { sql: 'created', - type: 'time' + type: 'time', + granularities: { + one_week: { + interval: '1 week', + }, + one_week_by_sunday: { + interval: '1 week', + offset: '-1 day' + }, + two_weeks_by_1st_feb_00am: { + interval: '2 weeks', + origin: '2024-02-01 00:00:00' + }, + two_weeks_by_1st_feb_10am: { + interval: '2 weeks', + origin: '2024-02-01 10:00:00' + } + } }; return prepareCube('cube', cube); @@ -20,6 +37,7 @@ describe('Pre Aggregation by filter match tests', () => { measures: Array, preAggTimeGranularity: string, queryAggTimeGranularity: string, + queryTimeZone: string = 'America/Los_Angeles', ) { const aaa: any = { type: 'rollup', @@ -28,6 +46,8 @@ describe('Pre Aggregation by filter match tests', () => { timeDimension: 'cube.created', granularity: preAggTimeGranularity, partitionGranularity: 'year', + // Enabling only for custom granularities + allowNonStrictDateRangeMatch: !/^(minute|hour|day|week|month|quarter|year)$/.test(preAggTimeGranularity) }; const cube: any = { @@ -55,7 +75,7 @@ describe('Pre Aggregation by filter match tests', () => { granularity: queryAggTimeGranularity, dateRange: { from: '2017-01-01', to: '2017-03-31' } }], - timezone: 'America/Los_Angeles', + timezone: queryTimeZone, }); const usePreAggregation = PreAggregations.canUsePreAggregationForTransformedQueryFn( @@ -71,6 +91,26 @@ describe('Pre Aggregation by filter match tests', () => { true, ['count'], 'day', 'day' )); + it('1 count measure, one_week_by_sunday, one_week_by_sunday', () => testPreAggregationMatch( + true, ['count'], 'one_week_by_sunday', 'one_week_by_sunday' + )); + + it('1 count measure, two_weeks_by_1st_feb_00am, two_weeks_by_1st_feb_00am', () => testPreAggregationMatch( + true, ['count'], 'two_weeks_by_1st_feb_00am', 'two_weeks_by_1st_feb_00am' + )); + + it('1 count measure, day, one_week_by_sunday', () => testPreAggregationMatch( + true, ['count'], 'day', 'one_week_by_sunday' + )); + + it('1 count measure, day, two_weeks_by_1st_feb_00am', () => testPreAggregationMatch( + true, ['count'], 'day', 'two_weeks_by_1st_feb_00am' + )); + + it('1 count measure, day, two_weeks_by_1st_feb_10am', () => testPreAggregationMatch( + false, ['count'], 'day', 'two_weeks_by_1st_feb_10am' + )); + it('1 count measure, week, day', () => testPreAggregationMatch( false, ['count'], 'week', 'day' ));