Skip to content

Commit

Permalink
fix: Wrong interval functions for BigQuery (#367) Thanks to @lvauvillier
Browse files Browse the repository at this point in the history
!

* fix: Wrong interval functions for BigQuery

* fix: extend BaseQuery with timestamp add/subtract methods to properly handle BigQuery interval computation
  • Loading branch information
lvauvillier authored Feb 13, 2020
1 parent cfb65a2 commit 0e09d4d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/cubejs-schema-compiler/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,14 @@ class BaseQuery {
return `${date} + interval '${interval}'`;
}

addTimestampInterval(timestamp, interval) {
return this.addInterval(timestamp, interval);
}

subtractTimestampInterval(timestamp, interval) {
return this.subtractInterval(timestamp, interval);
}

cumulativeMeasures() {
return this.measures.filter(m => m.isCumulative());
}
Expand Down Expand Up @@ -1530,7 +1538,7 @@ class BaseQuery {
query.timeDimensions[0].path()[0]
][
query.timeDimensions[0].path()[1]
].filter((from, to) => `${query.nowTimestampSql()} < ${updateWindow ? this.addInterval(this.timeStampCast(to), updateWindow) : this.timeStampCast(to)}`),
].filter((from, to) => `${query.nowTimestampSql()} < ${updateWindow ? this.addTimestampInterval(this.timeStampCast(to), updateWindow) : this.timeStampCast(to)}`),
label: originalRefreshKey
}])
);
Expand Down
8 changes: 8 additions & 0 deletions packages/cubejs-schema-compiler/adapter/BigqueryQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ class BigqueryQuery extends BaseQuery {
return `DATETIME_ADD(${date}, INTERVAL ${interval})`;
}

subtractTimestampInterval(date, interval) {
return `TIMESTAMP_SUB(${date}, INTERVAL ${interval})`;
}

addTimestampInterval(date, interval) {
return `TIMESTAMP_ADD(${date}, INTERVAL ${interval})`;
}

nowTimestampSql() {
return `CURRENT_TIMESTAMP()`;
}
Expand Down

0 comments on commit 0e09d4d

Please sign in to comment.