Skip to content

Commit

Permalink
test for custom granularity match
Browse files Browse the repository at this point in the history
  • Loading branch information
KSDaemon committed Jan 29, 2025
1 parent 14ee9a2 commit d06be73
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ describe('PreAggregations', () => {
},
createdAt: {
type: 'time',
sql: 'created_at'
sql: 'created_at',
granularities: {
hourTenMinOffset: {
interval: '1 hour',
offset: '10 minutes'
}
}
},
signedUpAt: {
type: 'time',
Expand Down Expand Up @@ -224,6 +230,11 @@ describe('PreAggregations', () => {
granularity: 'hour',
partitionGranularity: 'month'
},
countCustomGranularity: {
measures: [count],
timeDimension: createdAt,
granularity: 'hourTenMinOffset'
},
sourceAndIdRollup: {
measures: [count],
dimensions: [sourceAndId, source],
Expand Down Expand Up @@ -590,6 +601,49 @@ describe('PreAggregations', () => {
});
}));

it('simple pre-aggregation with custom granularity (exact match)', () => compiler.compile().then(() => {
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: [
'visitors.count'
],
timeDimensions: [{
dimension: 'visitors.createdAt',
dateRange: ['2017-01-01 00:10:00.000', '2017-01-29 22:09:59.999'],
granularity: 'hourTenMinOffset',
}],
timezone: 'America/Los_Angeles',
preAggregationsSchema: ''
});

const queryAndParams = query.buildSqlAndParams();
console.log(queryAndParams);
expect(query.preAggregations?.preAggregationForQuery?.canUsePreAggregation).toEqual(true);
expect(queryAndParams[0]).toMatch(/visitors_count_custom_granularity/);

return dbRunner.evaluateQueryWithPreAggregations(query).then(res => {
expect(res).toEqual(
[
{
visitors__count: '1',
visitors__created_at_hourTenMinOffset: '2017-01-02T15:10:00.000Z',
},
{
visitors__count: '1',
visitors__created_at_hourTenMinOffset: '2017-01-04T15:10:00.000Z',
},
{
visitors__count: '1',
visitors__created_at_hourTenMinOffset: '2017-01-05T15:10:00.000Z',
},
{
visitors__count: '2',
visitors__created_at_hourTenMinOffset: '2017-01-06T15:10:00.000Z',
},
]
);
});
}));

it('leaf measure pre-aggregation', () => compiler.compile().then(() => {
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: [
Expand Down
8 changes: 4 additions & 4 deletions packages/cubejs-schema-compiler/test/unit/base-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ describe('SQL Generation', () => {
'orders.status'
],
filters: [],
timezone: 'Europe/Kyiv'
timezone: 'UTC'
},
{
measures: [
Expand All @@ -518,7 +518,7 @@ describe('SQL Generation', () => {
'orders.status'
],
filters: [],
timezone: 'Europe/Kyiv'
timezone: 'UTC'
},
{
measures: [
Expand All @@ -538,7 +538,7 @@ describe('SQL Generation', () => {
'orders.status'
],
filters: [],
timezone: 'Europe/Kyiv'
timezone: 'UTC'
},
{
measures: [
Expand All @@ -558,7 +558,7 @@ describe('SQL Generation', () => {
'orders.status'
],
filters: [],
timezone: 'Europe/Kyiv'
timezone: 'UTC'
},
// requesting via view
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe('Pre Aggregation by filter match tests', () => {
},
one_week_by_sunday: {
interval: '1 week',
offset: '-1 day'
// offset: '-1 day' // offsets might lead to flaky tests through years
origin: '2025-01-05 00:00:00'
},
two_weeks_by_1st_feb_00am: {
interval: '2 weeks',
Expand All @@ -38,6 +39,8 @@ describe('Pre Aggregation by filter match tests', () => {
preAggTimeGranularity: string,
queryAggTimeGranularity: string,
queryTimeZone: string = 'America/Los_Angeles',
dateRange: [ string, string ] = ['2017-01-01', '2017-03-31'],
allowNonStrictDateRangeMatch: boolean = false
) {
const aaa: any = {
type: 'rollup',
Expand All @@ -46,8 +49,7 @@ 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)
allowNonStrictDateRangeMatch
};

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

// aaa.sortedDimensions = aaa.dimensions;
// aaa.sortedDimensions.sort();
aaa.sortedTimeDimensions = [[aaa.timeDimension, aaa.granularity]];
aaa.sortedTimeDimensions = [[aaa.timeDimension, aaa.granularity, 'day']];

return compiler.compile().then(() => {
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: measures.map(m => `cube.${m}`),
timeDimensions: [{
dimension: 'cube.created',
granularity: queryAggTimeGranularity,
dateRange: { from: '2017-01-01', to: '2017-03-31' }
dateRange,
}],
timezone: queryTimeZone,
});
Expand All @@ -92,15 +94,43 @@ describe('Pre Aggregation by filter match tests', () => {
));

it('1 count measure, one_week_by_sunday, one_week_by_sunday', () => testPreAggregationMatch(
true, ['count'], 'one_week_by_sunday', 'one_week_by_sunday'
true,
['count'],
'one_week_by_sunday',
'one_week_by_sunday',
'UTC',
['2024-02-11', '2024-03-02']
));

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, one_week_by_sunday, one_week_by_sunday (dst)', () => testPreAggregationMatch(
true,
['count'],
'one_week_by_sunday',
'one_week_by_sunday',
'UTC',
['2024-02-25', '2024-03-30'], // DST Switch happens here, but still must work!
));

it('1 count measure, two_weeks_by_1st_feb_00am, two_weeks_by_1st_feb_00am (match)', () => testPreAggregationMatch(
true,
['count'],
'two_weeks_by_1st_feb_00am',
'two_weeks_by_1st_feb_00am',
'UTC',
['2024-01-18', '2024-02-28']
));

it('1 count measure, two_weeks_by_1st_feb_00am, two_weeks_by_1st_feb_00am (miss)', () => testPreAggregationMatch(
false,
['count'],
'two_weeks_by_1st_feb_00am',
'two_weeks_by_1st_feb_00am',
'UTC',
['2024-01-18', '2024-02-07'], // Interval not aligned
));

it('1 count measure, day, one_week_by_sunday', () => testPreAggregationMatch(
true, ['count'], 'day', 'one_week_by_sunday'
true, ['count'], 'day', 'one_week_by_sunday', 'UTC'
));

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

0 comments on commit d06be73

Please sign in to comment.