Skip to content

Commit 00fe682

Browse files
authored
fix(schema-compiler): Fix allowNonStrictDateRangeMatch pre-agg match for dateRange queries without granularities (#9258)
1 parent 7ea098b commit 00fe682

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export class BaseTimeDimension extends BaseFilter {
273273
}
274274

275275
public resolvedGranularity() {
276-
return this.granularityObj ? this.granularityObj.resolvedGranularity() : this.dateRangeGranularity();
276+
return this.granularityObj ? this.granularityObj.resolvedGranularity() : null;
277277
}
278278

279279
public wildcardRange() {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,9 @@ export class PreAggregations {
604604
*/
605605
const expandTimeDimension = (timeDimension) => {
606606
const [dimension, resolvedGranularity] = timeDimension;
607+
if (!resolvedGranularity) {
608+
return [[dimension, '*']]; // Any granularity should fit
609+
}
607610
return expandGranularity(dimension, resolvedGranularity)
608611
.map((newGranularity) => [dimension, newGranularity]);
609612
};
@@ -638,7 +641,13 @@ export class PreAggregations {
638641

639642
const timeDimensionsMatch = (timeDimensionsList, doBackAlias) => R.allPass(
640643
timeDimensionsList.map(
641-
tds => R.anyPass(tds.map(td => R.contains(td)))
644+
tds => R.anyPass(tds.map(td => {
645+
if (td[1] === '*') {
646+
return R.any(tdtc => tdtc[0] === td[0]); // need to match the dimension at least
647+
} else {
648+
return R.contains(td);
649+
}
650+
}))
642651
)
643652
)(
644653
doBackAlias ?

packages/cubejs-schema-compiler/test/integration/postgres/pre-agg-allow-non-strict.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ const getQueries = (compiler, joinGraph, cubeEvaluator) => ([
195195
}],
196196
timezone: 'America/Los_Angeles',
197197
}),
198+
// no granularity
199+
new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
200+
measures: ['cube.totalQuantity', 'cube.totalProfit'],
201+
dimensions: ['cube.city'],
202+
timeDimensions: [{
203+
dimension: 'cube.orderDate',
204+
dateRange: ['2020-01-01 00:00:00.000', '2020-03-30 22:50:50.999'],
205+
}],
206+
timezone: 'America/Los_Angeles',
207+
}),
198208
]);
199209

200210
describe(
@@ -259,6 +269,15 @@ describe(
259269
expect(query.indexOf('cube__daily_data')).toEqual(-1);
260270
expect(query.indexOf('cube__hourly_data')).toEqual(-1);
261271
});
272+
273+
it('query with no granularity match MonthlyData', async () => {
274+
await compiler.compile();
275+
const [,,,,,, request] = getQueries(compiler, joinGraph, cubeEvaluator);
276+
const [query] = request.buildSqlAndParams();
277+
expect(query.indexOf('cube__monthly_data')).toBeGreaterThanOrEqual(0);
278+
expect(query.indexOf('cube__daily_data')).toEqual(-1);
279+
expect(query.indexOf('cube__hourly_data')).toEqual(-1);
280+
});
262281
});
263282

264283
describe('The `DailyData` pre-aggregation with the `allowNonStrictDateRangeMatch` enabled', () => {
@@ -320,6 +339,15 @@ describe(
320339
expect(query.indexOf('cube__daily_data')).toBeGreaterThanOrEqual(0);
321340
expect(query.indexOf('cube__hourly_data')).toEqual(-1);
322341
});
342+
343+
it('query with no granularity match MonthlyData', async () => {
344+
await compiler.compile();
345+
const [,,,,,, request] = getQueries(compiler, joinGraph, cubeEvaluator);
346+
const [query] = request.buildSqlAndParams();
347+
expect(query.indexOf('cube__monthly_data')).toEqual(-1);
348+
expect(query.indexOf('cube__daily_data')).toBeGreaterThanOrEqual(0);
349+
expect(query.indexOf('cube__hourly_data')).toEqual(-1);
350+
});
323351
});
324352

325353
describe('The `HourlyData` pre-aggregation with the `allowNonStrictDateRangeMatch` enabled', () => {
@@ -381,6 +409,15 @@ describe(
381409
expect(query.indexOf('cube__daily_data')).toEqual(-1);
382410
expect(query.indexOf('cube__hourly_data')).toBeGreaterThanOrEqual(0);
383411
});
412+
413+
it('query with no granularity match HourlyData', async () => {
414+
await compiler.compile();
415+
const [,,,,,, request] = getQueries(compiler, joinGraph, cubeEvaluator);
416+
const [query] = request.buildSqlAndParams();
417+
expect(query.indexOf('cube__monthly_data')).toEqual(-1);
418+
expect(query.indexOf('cube__daily_data')).toEqual(-1);
419+
expect(query.indexOf('cube__hourly_data')).toBeGreaterThanOrEqual(0);
420+
});
384421
});
385422

386423
describe('`MonthlyData` and `DailyData` pre-aggregations with the `allowNonStrictDateRangeMatch` enabled', () => {

0 commit comments

Comments
 (0)