Skip to content

Commit 47c4d78

Browse files
authored
feat(schema-compiler): exact match preagg with custom granularity without the need for allow_non_strict_date_range_match option (#8712)
* feat(schema-compiler): exact match preagg with custom granularity without the need for allow_non_strict_date_range_match option * Fix failing tests after tuning sortTimeDimensionsWithRollupGranularity
1 parent 23c9cda commit 47c4d78

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ export class BaseTimeDimension extends BaseFilter {
261261
return this.granularityObj?.resolvedGranularity();
262262
}
263263

264+
public isPredefinedGranularity(): boolean {
265+
return this.granularityObj?.isPredefined() || false;
266+
}
267+
264268
public wildcardRange() {
265269
return [FROM_PARTITION_RANGE, TO_PARTITION_RANGE];
266270
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export class Granularity {
5151
}
5252
}
5353

54+
public isPredefined(): boolean {
55+
return this.predefinedGranularity;
56+
}
57+
5458
public originFormatted(): string {
5559
return this.origin.format('YYYY-MM-DDTHH:mm:ss.SSS');
5660
}

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,11 @@ export class PreAggregations {
431431
static sortTimeDimensionsWithRollupGranularity(timeDimensions) {
432432
return timeDimensions && R.sortBy(
433433
R.prop(0),
434-
timeDimensions.map(d => [d.expressionPath(), d.rollupGranularity()])
434+
timeDimensions.map(d => (d.isPredefinedGranularity() ?
435+
[d.expressionPath(), d.rollupGranularity(), null] :
436+
// For custom granularities we need to add its name to the list (for exact matches)
437+
[d.expressionPath(), d.rollupGranularity(), d.granularity]
438+
))
435439
) || [];
436440
}
437441

@@ -548,7 +552,8 @@ export class PreAggregations {
548552
backAlias(references.sortedTimeDimensions || sortTimeDimensions(references.timeDimensions));
549553
const qryTimeDimensions = references.allowNonStrictDateRangeMatch
550554
? transformedQuery.timeDimensions
551-
: transformedQuery.sortedTimeDimensions;
555+
: transformedQuery.sortedTimeDimensions.map(t => t.slice(0, 2));
556+
// slice above is used to exclude possible custom granularity returned from sortTimeDimensionsWithRollupGranularity()
552557

553558
const backAliasMeasures = backAlias(references.measures);
554559
const backAliasSortedDimensions = backAlias(references.sortedDimensions || references.dimensions);
@@ -615,9 +620,16 @@ export class PreAggregations {
615620
* @returns {Array<Array<string>>}
616621
*/
617622
const expandTimeDimension = (timeDimension) => {
618-
const [dimension, granularity] = timeDimension;
619-
return expandGranularity(granularity)
623+
const [dimension, granularity, customGranularity] = timeDimension;
624+
const res = expandGranularity(granularity)
620625
.map((newGranularity) => [dimension, newGranularity]);
626+
627+
if (customGranularity) {
628+
// For custom granularities we add it upfront to the list (for exact matches)
629+
res.unshift([dimension, customGranularity]);
630+
}
631+
632+
return res;
621633
};
622634

623635
/**
@@ -782,7 +794,7 @@ export class PreAggregations {
782794
}
783795

784796
/**
785-
* Returns an array of potencially applicable for the query preaggs in the
797+
* Returns an array of potentially applicable for the query preaggs in the
786798
* same order they appear in the schema file.
787799
* @returns {Array<Object>}
788800
*/

0 commit comments

Comments
 (0)