Skip to content

Commit be9bed8

Browse files
committed
fix timeshifts for views
1 parent 83c5821 commit be9bed8

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,8 @@ export class BaseQuery {
13311331
timeDimensions: this.options.timeDimensions || [],
13321332
multiStageTimeDimensions: (this.options.timeDimensions || []).filter(td => !!td.granularity),
13331333
// TODO accessing filters directly from options might miss some processing logic
1334-
filters: this.options.filters || []
1334+
filters: this.options.filters || [],
1335+
segments: this.options.segments || [],
13351336
},
13361337
allMemberChildren,
13371338
withQueries
@@ -1450,10 +1451,14 @@ export class BaseQuery {
14501451
const allBackAliasMembers = this.allBackAliasTimeDimensions();
14511452
let { commonTimeShift } = queryContext;
14521453
const timeShifts = queryContext.timeShifts || {};
1454+
const memberOfCube = !this.cubeEvaluator.cubeFromPath(memberPath).isView;
14531455

14541456
if (memberDef.timeShiftReferences.length === 1 && !memberDef.timeShiftReferences[0].timeDimension) {
14551457
const timeShift = memberDef.timeShiftReferences[0];
1456-
commonTimeShift = timeShift.type === 'next' ? this.negateInterval(timeShift.interval) : timeShift.interval;
1458+
// We avoid view's timeshift evaluation as there will be another round of underlying cube's member evaluation
1459+
if (memberOfCube) {
1460+
commonTimeShift = timeShift.type === 'next' ? this.negateInterval(timeShift.interval) : timeShift.interval;
1461+
}
14571462

14581463
mapFn = (td) => {
14591464
// We need to ignore aliased td, because it will match and insert shiftInterval on first
@@ -1468,9 +1473,12 @@ export class BaseQuery {
14681473
};
14691474
};
14701475
} else {
1471-
memberDef.timeShiftReferences.forEach((r) => {
1472-
timeShifts[r.timeDimension] = r.type === 'next' ? this.negateInterval(r.interval) : r.interval;
1473-
});
1476+
// We avoid view's timeshift evaluation as there will be another round of underlying cube's member evaluation
1477+
if (memberOfCube) {
1478+
memberDef.timeShiftReferences.forEach((r) => {
1479+
timeShifts[r.timeDimension] = r.type === 'next' ? this.negateInterval(r.interval) : r.interval;
1480+
});
1481+
}
14741482

14751483
mapFn = (td) => {
14761484
const timeShift = memberDef.timeShiftReferences.find(r => r.timeDimension === td.dimension || td.dimension === allBackAliasMembers[r.timeDimension]);
@@ -1538,11 +1546,15 @@ export class BaseQuery {
15381546
...queryContext,
15391547
// TODO make it same way as keepFilters
15401548
timeDimensions: queryContext.timeDimensions.map(td => ({ ...td, dateRange: undefined })),
1549+
// TODO keep segments related to this multistage (if applicable)
1550+
segments: [],
15411551
filters: this.keepFilters(queryContext.filters, filterMember => filterMember === memberPath),
15421552
};
15431553
} else {
15441554
queryContext = {
15451555
...queryContext,
1556+
// TODO remove not related segments
1557+
// segments: queryContext.segments,
15461558
filters: this.keepFilters(queryContext.filters, filterMember => !this.memberInstanceByPath(filterMember).isMultiStage()),
15471559
};
15481560
}
@@ -1602,6 +1614,7 @@ export class BaseQuery {
16021614
multiStageDimensions: withQuery.multiStageDimensions,
16031615
multiStageTimeDimensions: withQuery.multiStageTimeDimensions,
16041616
filters: withQuery.filters,
1617+
segments: withQuery.segments,
16051618
from: fromSql && {
16061619
sql: fromSql,
16071620
alias: `${withQuery.alias}_join`,
@@ -2908,8 +2921,10 @@ export class BaseQuery {
29082921
return td.dimensionSql();
29092922
} else {
29102923
let res = this.autoPrefixAndEvaluateSql(cubeName, symbol.sql, isMemberExpr);
2924+
const mp = this.cubeEvaluator.pathFromArray([cubeName, name]);
29112925

2912-
if (symbol.type === 'time') {
2926+
// Skip view's member evaluation as there will be underlying cube's same member evaluation
2927+
if (symbol.type === 'time' && !this.cubeEvaluator.cubeFromPath(mp).isView) {
29132928
if (this.safeEvaluateSymbolContext().commonTimeShift) {
29142929
res = `(${this.addTimestampInterval(res, this.safeEvaluateSymbolContext().commonTimeShift)})`;
29152930
} else if (this.safeEvaluateSymbolContext().timeShifts?.[this.cubeEvaluator.pathFromArray([cubeName, name])]) {

0 commit comments

Comments
 (0)