Skip to content

Commit

Permalink
Add custom granularities support to time dimension and base query
Browse files Browse the repository at this point in the history
  • Loading branch information
KSDaemon committed Aug 7, 2024
1 parent b26e3b9 commit 88de386
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
20 changes: 17 additions & 3 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -2115,6 +2115,10 @@ export class BaseQuery {
return this.evaluateSymbolSql(dimension.path()[0], dimension.path()[1], dimension.dimensionDefinition());
}

dimensionGranularitySql(dimension) {
return this.evaluateSymbolSql(dimension.path()[0], dimension.path()[1], dimension.dimensionDefinition(), null, ['granularities', dimension.granularity]);
}

segmentSql(segment) {
return this.evaluateSymbolSql(segment.path()[0], segment.path()[1], segment.segmentDefinition());
}
Expand Down Expand Up @@ -2171,12 +2175,12 @@ export class BaseQuery {
return this.evaluateSymbolContext || {};
}

evaluateSymbolSql(cubeName, name, symbol, memberExpressionType) {
evaluateSymbolSql(cubeName, name, symbol, memberExpressionType, childPropPathArray) {
const isMemberExpr = !!memberExpressionType;
if (!memberExpressionType) {
this.pushMemberNameForCollectionIfNecessary(cubeName, name);
}
const memberPathArray = [cubeName, name];
const memberPathArray = [cubeName, name].concat(childPropPathArray ?? []);
const memberPath = this.cubeEvaluator.pathFromArray(memberPathArray);
let type = memberExpressionType;
if (!type && this.cubeEvaluator.isMeasure(memberPathArray)) {
Expand Down Expand Up @@ -2281,7 +2285,17 @@ export class BaseQuery {
this.autoPrefixAndEvaluateSql(cubeName, symbol.longitude.sql, isMemberExpr)
]);
} else {
let res = this.autoPrefixAndEvaluateSql(cubeName, symbol.sql, isMemberExpr);
let res;

// TODO Temporarily placed it here, but maybe this should be moved level up and processed in
// more generalized way to support nested properties not only on dimension level
if (childPropPathArray && childPropPathArray.length > 0) {
const childProperty = this.cubeEvaluator.byPath('dimensions', memberPathArray);
res = this.autoPrefixAndEvaluateSql(cubeName, childProperty.sql, isMemberExpr);
} else {
res = this.autoPrefixAndEvaluateSql(cubeName, symbol.sql, isMemberExpr);
}

if (symbol.shiftInterval) {
res = `(${this.addTimestampInterval(res, symbol.shiftInterval)})`;
}
Expand Down
35 changes: 28 additions & 7 deletions packages/cubejs-schema-compiler/src/adapter/BaseTimeDimension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export class BaseTimeDimension extends BaseFilter {

public readonly isPredefined: boolean;

public readonly baseGranularity: string;
public readonly baseGranularity: string | undefined;

public readonly granularitySql: Function | undefined;

public readonly boundaryDateRange: any;

Expand All @@ -31,9 +33,14 @@ export class BaseTimeDimension extends BaseFilter {
this.dateRange = timeDimension.dateRange;
this.granularity = timeDimension.granularity;
this.isPredefined = isPredefinedGranularity(this.granularity);
this.baseGranularity = this.query.cubeEvaluator
.byPath('dimensions', timeDimension.dimension)
.granularities?.[this.granularity]?.baseGranularity;
if (!this.isPredefined) {
const customGranularity = this.query.cubeEvaluator
.byPath('dimensions', timeDimension.dimension)
.granularities?.[this.granularity];

this.baseGranularity = customGranularity?.baseGranularity;
this.granularitySql = customGranularity?.sql;
}
this.boundaryDateRange = timeDimension.boundaryDateRange;
this.shiftInterval = timeDimension.shiftInterval;
}
Expand Down Expand Up @@ -95,12 +102,22 @@ export class BaseTimeDimension extends BaseFilter {
if (context.rollupGranularity === this.granularity) {
return super.dimensionSql();
}
return this.query.timeGroupedColumn(granularity, this.query.dimensionSql(this));
if (this.isPredefined || !this.granularity) {
return this.query.timeGroupedColumn(granularity, this.query.dimensionSql(this));
} else {
return this.query.dimensionGranularitySql(this);
}
}

if (context.ungrouped) {
return this.convertedToTz();
}
return this.query.timeGroupedColumn(granularity, this.convertedToTz());

if (this.isPredefined) {
return this.query.timeGroupedColumn(granularity, this.convertedToTz());
} else {
return this.granularityConvertedToTz();
}
}

public dimensionDefinition(): DimensionDefinition | SegmentDefinition {
Expand All @@ -115,7 +132,11 @@ export class BaseTimeDimension extends BaseFilter {
}

public convertedToTz() {
return this.query.convertTz(this.query.dimensionSql(this));
return this.query.convertTz(`${this.query.dimensionSql(this)}`);
}

public granularityConvertedToTz() {
return this.query.convertTz(`(${this.query.dimensionGranularitySql(this)})`);
}

public filterToWhere() {
Expand Down

0 comments on commit 88de386

Please sign in to comment.