Skip to content

Commit 6a6dfe9

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 6a6dfe9

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
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,7 +39,7 @@ 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 {
42+
public get granularity(): string | null | undefined {
4343
return this.granularityObj?.granularity;
4444
}
4545

@@ -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
}
@@ -262,7 +262,7 @@ export class BaseTimeDimension extends BaseFilter {
262262
}
263263

264264
public resolvedGranularity() {
265-
return this.granularityObj?.resolvedGranularity();
265+
return this.granularityObj ? this.granularityObj.resolvedGranularity() : this.dateRangeGranularity();
266266
}
267267

268268
public isPredefinedGranularity(): boolean {

packages/cubejs-schema-compiler/src/adapter/PreAggregations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ export class PreAggregations {
450450
static timeDimensionsAsIs(timeDimensions) {
451451
return timeDimensions && R.sortBy(
452452
R.prop(0),
453-
timeDimensions.map(d => [d.expressionPath(), d.granularity]),
453+
timeDimensions.map(d => [d.expressionPath(), d.resolvedGranularity()]),
454454
) || [];
455455
}
456456

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)