Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading