Skip to content

Commit

Permalink
fix(schema-compiler): correct origin timestamp initialization in Gran…
Browse files Browse the repository at this point in the history
…ularity instance (#8710)

* remove doubled query.newTimeDimension() call in transformQueryToCanUseForm

* fix(schema-compiler): correct origin timestamp initialization in Granularity instance

* tests for custom preagg matches
  • Loading branch information
KSDaemon committed Sep 17, 2024
1 parent e5dec22 commit 11e941d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
10 changes: 5 additions & 5 deletions packages/cubejs-schema-compiler/src/adapter/Granularity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -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())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export class PreAggregations {
dateRange: td.dateRange,
granularity: td.granularity,
});
}).map(d => query.newTimeDimension(d));
});

let sortedAllCubeNames;
let sortedUsedCubePrimaryKeys;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -20,6 +37,7 @@ describe('Pre Aggregation by filter match tests', () => {
measures: Array<String>,
preAggTimeGranularity: string,
queryAggTimeGranularity: string,
queryTimeZone: string = 'America/Los_Angeles',
) {
const aaa: any = {
type: 'rollup',
Expand All @@ -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 = {
Expand Down Expand Up @@ -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(
Expand All @@ -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'
));
Expand Down

0 comments on commit 11e941d

Please sign in to comment.