Skip to content

Commit 19c5243

Browse files
committed
fix(schema-compiler): Fix queries with time dimensions without granularities don't hit pre-aggregations with allow_non_strict_date_range_match=true
1 parent dcd283a commit 19c5243

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

packages/cubejs-schema-compiler/src/adapter/BaseTimeDimension.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export class BaseTimeDimension extends BaseFilter {
3939
}
4040

4141
// TODO: find and fix all hidden references to granularity to rely on granularityObj instead?
42-
public get granularity(): string | undefined {
43-
return this.granularityObj?.granularity;
42+
public get granularity(): string | null | undefined {
43+
return this.granularityObj ? this.granularityObj.granularity : this.dateRangeGranularity();
4444
}
4545

4646
public selectColumns() {
@@ -217,7 +217,7 @@ export class BaseTimeDimension extends BaseFilter {
217217
return this.query.dateTimeCast(this.query.paramAllocator.allocateParam(this.dateRange ? this.dateToFormatted() : BUILD_RANGE_END_LOCAL));
218218
}
219219

220-
public dateRangeGranularity() {
220+
public dateRangeGranularity(): string | null {
221221
if (!this.dateRange) {
222222
return null;
223223
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ describe('PreAggregations', () => {
229229
dimensions: [sourceAndId, source],
230230
timeDimension: createdAt,
231231
granularity: 'hour',
232+
allowNonStrictDateRangeMatch: true
232233
},
233234
visitorsMultiplied: {
234235
measures: [count],
@@ -546,6 +547,33 @@ describe('PreAggregations', () => {
546547
});
547548
}));
548549

550+
it('simple pre-aggregation (with no granularity in query)', () => compiler.compile().then(() => {
551+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
552+
measures: [
553+
'visitors.count'
554+
],
555+
timeDimensions: [{
556+
dimension: 'visitors.createdAt',
557+
dateRange: ['2017-01-01 00:00:00.000', '2017-01-29 22:59:59.999']
558+
}],
559+
timezone: 'America/Los_Angeles',
560+
preAggregationsSchema: ''
561+
});
562+
563+
const queryAndParams = query.buildSqlAndParams();
564+
console.log(queryAndParams);
565+
expect(query.preAggregations?.preAggregationForQuery?.canUsePreAggregation).toEqual(true);
566+
expect(queryAndParams[0]).toMatch(/visitors_source_and_id_rollup/);
567+
568+
return dbRunner.evaluateQueryWithPreAggregations(query).then(res => {
569+
expect(res).toEqual(
570+
[{
571+
visitors__count: '5'
572+
}]
573+
);
574+
});
575+
}));
576+
549577
it('leaf measure pre-aggregation', () => compiler.compile().then(() => {
550578
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
551579
measures: [

0 commit comments

Comments
 (0)