Skip to content

Commit

Permalink
fix(schema-compiler): Fix queries with time dimensions without granul…
Browse files Browse the repository at this point in the history
…arities don't hit pre-aggregations with allow_non_strict_date_range_match=true
  • Loading branch information
KSDaemon committed Jan 15, 2025
1 parent dcd283a commit 19c5243
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class BaseTimeDimension extends BaseFilter {
}

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

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

public dateRangeGranularity() {
public dateRangeGranularity(): string | null {
if (!this.dateRange) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ describe('PreAggregations', () => {
dimensions: [sourceAndId, source],
timeDimension: createdAt,
granularity: 'hour',
allowNonStrictDateRangeMatch: true
},
visitorsMultiplied: {
measures: [count],
Expand Down Expand Up @@ -546,6 +547,33 @@ describe('PreAggregations', () => {
});
}));

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

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

return dbRunner.evaluateQueryWithPreAggregations(query).then(res => {
expect(res).toEqual(
[{
visitors__count: '5'
}]
);
});
}));

it('leaf measure pre-aggregation', () => compiler.compile().then(() => {
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: [
Expand Down

0 comments on commit 19c5243

Please sign in to comment.