Skip to content

Commit c041a8c

Browse files
committed
fix intervals math in bq
1 parent c54579f commit c041a8c

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

packages/cubejs-schema-compiler/src/adapter/BigqueryQuery.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,31 +182,31 @@ export class BigqueryQuery extends BaseQuery {
182182
}
183183

184184
public subtractInterval(date, interval) {
185-
return `DATETIME_SUB(${date}, INTERVAL ${this.formatInterval(interval)[0]})`;
186-
}
187-
188-
public addInterval(date, interval) {
189-
return `DATETIME_ADD(${date}, INTERVAL ${this.formatInterval(interval)[0]})`;
190-
}
191-
192-
public subtractTimestampInterval(date, interval) {
193185
const [intervalFormatted, timeUnit] = this.formatInterval(interval);
194-
if (['YEAR', 'MONTH', 'QUARTER'].includes(timeUnit)) {
186+
if (['YEAR', 'MONTH', 'QUARTER'].includes(timeUnit) || intervalFormatted.includes('WEEK')) {
195187
return this.timeStampCast(`DATETIME_SUB(DATETIME(${date}), INTERVAL ${intervalFormatted})`);
196188
}
197189

198190
return `TIMESTAMP_SUB(${date}, INTERVAL ${intervalFormatted})`;
199191
}
200192

201-
public addTimestampInterval(date, interval) {
193+
public addInterval(date, interval) {
202194
const [intervalFormatted, timeUnit] = this.formatInterval(interval);
203-
if (['YEAR', 'MONTH', 'QUARTER'].includes(timeUnit)) {
195+
if (['YEAR', 'MONTH', 'QUARTER'].includes(timeUnit) || intervalFormatted.includes('WEEK')) {
204196
return this.timeStampCast(`DATETIME_ADD(DATETIME(${date}), INTERVAL ${intervalFormatted})`);
205197
}
206198

207199
return `TIMESTAMP_ADD(${date}, INTERVAL ${intervalFormatted})`;
208200
}
209201

202+
public subtractTimestampInterval(timestamp, interval) {
203+
return this.subtractInterval(timestamp, interval);
204+
}
205+
206+
public addTimestampInterval(timestamp, interval) {
207+
return this.addInterval(timestamp, interval);
208+
}
209+
210210
public nowTimestampSql() {
211211
return 'CURRENT_TIMESTAMP()';
212212
}

0 commit comments

Comments
 (0)