Skip to content

Commit b1e1444

Browse files
KSDaemonmarianore-muttdata
authored andcommitted
fix(schema-compiler): Fix rolling window queries with expressions from SQL API (cube-js#9603)
1 parent 8011ecf commit b1e1444

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

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

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -945,45 +945,50 @@ export class BaseQuery {
945945
}
946946

947947
runningTotalDateJoinCondition() {
948-
return this.timeDimensions.map(
949-
d => [
950-
d,
951-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
952-
(dateFrom, dateTo, dateField, dimensionDateFrom, dimensionDateTo) => `${dateField} >= ${dimensionDateFrom} AND ${dateField} <= ${dateTo}`
953-
]
954-
);
948+
return this.timeDimensions
949+
.map(
950+
d => [
951+
d,
952+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
953+
(dateFrom, dateTo, dateField, dimensionDateFrom, dimensionDateTo) => `${dateField} >= ${dimensionDateFrom} AND ${dateField} <= ${dateTo}`
954+
]
955+
);
955956
}
956957

957958
rollingWindowToDateJoinCondition(granularity) {
958-
return this.timeDimensions.map(
959-
d => [
960-
d,
961-
(dateFrom, dateTo, dateField, dimensionDateFrom, dimensionDateTo, isFromStartToEnd) => `${dateField} >= ${this.timeGroupedColumn(granularity, dateFrom)} AND ${dateField} <= ${dateTo}`
962-
]
963-
);
959+
return this.timeDimensions
960+
.filter(td => td.granularity)
961+
.map(
962+
d => [
963+
d,
964+
(dateFrom, dateTo, dateField, dimensionDateFrom, dimensionDateTo, isFromStartToEnd) => `${dateField} >= ${this.timeGroupedColumn(granularity, dateFrom)} AND ${dateField} <= ${dateTo}`
965+
]
966+
);
964967
}
965968

966969
rollingWindowDateJoinCondition(trailingInterval, leadingInterval, offset) {
967970
offset = offset || 'end';
968-
return this.timeDimensions.map(
969-
d => [d, (dateFrom, dateTo, dateField, dimensionDateFrom, dimensionDateTo, isFromStartToEnd) => {
971+
return this.timeDimensions
972+
.filter(td => td.granularity)
973+
.map(
974+
d => [d, (dateFrom, dateTo, dateField, dimensionDateFrom, dimensionDateTo, isFromStartToEnd) => {
970975
// dateFrom based window
971-
const conditions = [];
972-
if (trailingInterval !== 'unbounded') {
973-
const startDate = isFromStartToEnd || offset === 'start' ? dateFrom : dateTo;
974-
const trailingStart = trailingInterval ? this.subtractInterval(startDate, trailingInterval) : startDate;
975-
const sign = offset === 'start' ? '>=' : '>';
976-
conditions.push(`${dateField} ${sign} ${trailingStart}`);
977-
}
978-
if (leadingInterval !== 'unbounded') {
979-
const endDate = isFromStartToEnd || offset === 'end' ? dateTo : dateFrom;
980-
const leadingEnd = leadingInterval ? this.addInterval(endDate, leadingInterval) : endDate;
981-
const sign = offset === 'end' ? '<=' : '<';
982-
conditions.push(`${dateField} ${sign} ${leadingEnd}`);
983-
}
984-
return conditions.length ? conditions.join(' AND ') : '1 = 1';
985-
}]
986-
);
976+
const conditions = [];
977+
if (trailingInterval !== 'unbounded') {
978+
const startDate = isFromStartToEnd || offset === 'start' ? dateFrom : dateTo;
979+
const trailingStart = trailingInterval ? this.subtractInterval(startDate, trailingInterval) : startDate;
980+
const sign = offset === 'start' ? '>=' : '>';
981+
conditions.push(`${dateField} ${sign} ${trailingStart}`);
982+
}
983+
if (leadingInterval !== 'unbounded') {
984+
const endDate = isFromStartToEnd || offset === 'end' ? dateTo : dateFrom;
985+
const leadingEnd = leadingInterval ? this.addInterval(endDate, leadingInterval) : endDate;
986+
const sign = offset === 'end' ? '<=' : '<';
987+
conditions.push(`${dateField} ${sign} ${leadingEnd}`);
988+
}
989+
return conditions.length ? conditions.join(' AND ') : '1 = 1';
990+
}]
991+
);
987992
}
988993

989994
/**

packages/cubejs-testing/test/__snapshots__/birdbox-postgresql-pre-aggregations.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`postgresql-cubestore HTTP Transport Empty partitions: Empty partitions
55
exports[`postgresql-cubestore HTTP Transport Rolling Mixed With Dimension No Granularity: Rolling Mixed With Dimension No Granularity 1`] = `
66
Array [
77
Object {
8-
"visitors.checkinsRollingTotal": null,
8+
"visitors.checkinsRollingTotal": "5",
99
"visitors.source": "some",
1010
},
1111
]

packages/cubejs-testing/test/__snapshots__/cli-postgresql-pre-aggregations.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`postgresql HTTP Transport Empty partitions: Empty partitions 1`] = `Arr
55
exports[`postgresql HTTP Transport Rolling Mixed With Dimension No Granularity: Rolling Mixed With Dimension No Granularity 1`] = `
66
Array [
77
Object {
8-
"visitors.checkinsRollingTotal": null,
8+
"visitors.checkinsRollingTotal": "5",
99
"visitors.source": "some",
1010
},
1111
]

0 commit comments

Comments
 (0)