Skip to content

Commit

Permalink
feat(schema-compiler): exact match preagg with custom granularity wit…
Browse files Browse the repository at this point in the history
…hout 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
  • Loading branch information
KSDaemon authored Sep 18, 2024
1 parent 23c9cda commit 47c4d78
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ export class BaseTimeDimension extends BaseFilter {
return this.granularityObj?.resolvedGranularity();
}

public isPredefinedGranularity(): boolean {
return this.granularityObj?.isPredefined() || false;
}

public wildcardRange() {
return [FROM_PARTITION_RANGE, TO_PARTITION_RANGE];
}
Expand Down
4 changes: 4 additions & 0 deletions packages/cubejs-schema-compiler/src/adapter/Granularity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export class Granularity {
}
}

public isPredefined(): boolean {
return this.predefinedGranularity;
}

public originFormatted(): string {
return this.origin.format('YYYY-MM-DDTHH:mm:ss.SSS');
}
Expand Down
22 changes: 17 additions & 5 deletions packages/cubejs-schema-compiler/src/adapter/PreAggregations.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,11 @@ export class PreAggregations {
static sortTimeDimensionsWithRollupGranularity(timeDimensions) {
return timeDimensions && R.sortBy(
R.prop(0),
timeDimensions.map(d => [d.expressionPath(), d.rollupGranularity()])
timeDimensions.map(d => (d.isPredefinedGranularity() ?
[d.expressionPath(), d.rollupGranularity(), null] :
// For custom granularities we need to add its name to the list (for exact matches)
[d.expressionPath(), d.rollupGranularity(), d.granularity]
))
) || [];
}

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

const backAliasMeasures = backAlias(references.measures);
const backAliasSortedDimensions = backAlias(references.sortedDimensions || references.dimensions);
Expand Down Expand Up @@ -615,9 +620,16 @@ export class PreAggregations {
* @returns {Array<Array<string>>}
*/
const expandTimeDimension = (timeDimension) => {
const [dimension, granularity] = timeDimension;
return expandGranularity(granularity)
const [dimension, granularity, customGranularity] = timeDimension;
const res = expandGranularity(granularity)
.map((newGranularity) => [dimension, newGranularity]);

if (customGranularity) {
// For custom granularities we add it upfront to the list (for exact matches)
res.unshift([dimension, customGranularity]);
}

return res;
};

/**
Expand Down Expand Up @@ -782,7 +794,7 @@ export class PreAggregations {
}

/**
* Returns an array of potencially applicable for the query preaggs in the
* Returns an array of potentially applicable for the query preaggs in the
* same order they appear in the schema file.
* @returns {Array<Object>}
*/
Expand Down

0 comments on commit 47c4d78

Please sign in to comment.