Skip to content

Commit 066af41

Browse files
committed
move bigquery to timestamp
1 parent abc8c6d commit 066af41

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@ export class BigqueryQuery extends BaseQuery {
4242
}
4343

4444
public convertTz(field) {
45-
return `DATETIME(${field}, '${this.timezone}')`;
45+
return this.timeStampCast(`DATETIME(${field}, '${this.timezone}')`);
4646
}
4747

4848
public timeStampCast(value) {
4949
return `TIMESTAMP(${value})`;
5050
}
5151

5252
public dateTimeCast(value) {
53+
return `TIMESTAMP(${value})`;
54+
}
55+
56+
private dateTimeForceCast(value) {
5357
return `DATETIME(TIMESTAMP(${value}))`;
5458
}
5559

@@ -58,7 +62,7 @@ export class BigqueryQuery extends BaseQuery {
5862
}
5963

6064
public timeGroupedColumn(granularity, dimension) {
61-
return `DATETIME_TRUNC(${dimension}, ${GRANULARITY_TO_INTERVAL[granularity]})`;
65+
return this.timeStampCast(`DATETIME_TRUNC(${dimension}, ${GRANULARITY_TO_INTERVAL[granularity]})`);
6266
}
6367

6468
/**
@@ -68,11 +72,11 @@ export class BigqueryQuery extends BaseQuery {
6872
*/
6973
public dateBin(interval: string, source: string, origin: string): string {
7074
const [intervalFormatted, timeUnit] = this.formatInterval(interval);
71-
const beginOfTime = this.dateTimeCast('\'1970-01-01T00:00:00\'');
75+
const beginOfTime = this.dateTimeForceCast('\'1970-01-01T00:00:00\'');
7276

73-
return `(${this.dateTimeCast(`'${origin}'`)} + INTERVAL ${intervalFormatted} *
77+
return `(${this.dateTimeForceCast(`'${origin}'`)} + INTERVAL ${intervalFormatted} *
7478
CAST(FLOOR(
75-
DATETIME_DIFF(${source}, ${this.dateTimeCast(`'${origin}'`)}, ${timeUnit}) /
79+
DATETIME_DIFF(${this.dateTimeForceCast(`${source}`)}, ${this.dateTimeForceCast(`'${origin}'`)}, ${timeUnit}) /
7680
DATETIME_DIFF(${beginOfTime} + INTERVAL ${intervalFormatted}, ${beginOfTime}, ${timeUnit})
7781
) AS INT64))`;
7882
}
@@ -112,7 +116,7 @@ export class BigqueryQuery extends BaseQuery {
112116
} else if (intervalParsed.month && intervalParsed.day && intervalParsed.hour && intervalParsed.minute && intervalParsed.second && intKeys === 5) {
113117
return [`'${intervalParsed.month} ${intervalParsed.day} ${intervalParsed.hour}:${intervalParsed.minute}:${intervalParsed.second}' MONTH TO SECOND`, 'SECOND'];
114118
} else if (intervalParsed.week && intKeys === 1) {
115-
return [`${intervalParsed.week} WEEK`, 'DAY'];
119+
return [`${intervalParsed.week} WEEK`, 'WEEK'];
116120
} else if (intervalParsed.day && intKeys === 1) {
117121
return [`${intervalParsed.day} DAY`, 'DAY'];
118122
} else if (intervalParsed.day && intervalParsed.hour && intKeys === 2) {
@@ -150,7 +154,7 @@ export class BigqueryQuery extends BaseQuery {
150154
const values = timeDimension.timeSeries().map(
151155
([from, to]) => `select '${from}' f, '${to}' t`
152156
).join(' UNION ALL ');
153-
return `SELECT ${this.dateTimeCast('dates.f')} date_from, ${this.dateTimeCast('dates.t')} date_to FROM (${values}) AS dates`;
157+
return `SELECT ${this.timeStampCast('dates.f')} date_from, ${this.timeStampCast('dates.t')} date_to FROM (${values}) AS dates`;
154158
}
155159

156160
public timestampFormat() {
@@ -182,16 +186,16 @@ export class BigqueryQuery extends BaseQuery {
182186
}
183187

184188
public subtractInterval(date, interval) {
185-
return `DATETIME_SUB(${date}, INTERVAL ${this.formatInterval(interval)[0]})`;
189+
return this.subtractTimestampInterval(date, interval);
186190
}
187191

188192
public addInterval(date, interval) {
189-
return `DATETIME_ADD(${date}, INTERVAL ${this.formatInterval(interval)[0]})`;
193+
return this.addTimestampInterval(date, interval);
190194
}
191195

192196
public subtractTimestampInterval(date, interval) {
193197
const [intervalFormatted, timeUnit] = this.formatInterval(interval);
194-
if (['YEAR', 'MONTH', 'QUARTER'].includes(timeUnit)) {
198+
if (['YEAR', 'MONTH', 'QUARTER', 'WEEK'].includes(timeUnit)) {
195199
return this.timeStampCast(`DATETIME_SUB(DATETIME(${date}), INTERVAL ${intervalFormatted})`);
196200
}
197201

@@ -200,7 +204,7 @@ export class BigqueryQuery extends BaseQuery {
200204

201205
public addTimestampInterval(date, interval) {
202206
const [intervalFormatted, timeUnit] = this.formatInterval(interval);
203-
if (['YEAR', 'MONTH', 'QUARTER'].includes(timeUnit)) {
207+
if (['YEAR', 'MONTH', 'QUARTER', 'WEEK'].includes(timeUnit)) {
204208
return this.timeStampCast(`DATETIME_ADD(DATETIME(${date}), INTERVAL ${intervalFormatted})`);
205209
}
206210

@@ -282,8 +286,8 @@ export class BigqueryQuery extends BaseQuery {
282286
'{% if not loop.last %} UNION ALL\n{% endif %}' +
283287
'{% endfor %}' +
284288
') AS dates';
285-
templates.statements.generated_time_series_select = 'SELECT DATETIME(d) AS date_from,\n' +
286-
'DATETIME_SUB(DATETIME_ADD(DATETIME(d), INTERVAL {{ granularity }}), INTERVAL 1 MILLISECOND) AS date_to \n' +
289+
templates.statements.generated_time_series_select = 'SELECT TIMESTAMP(d) AS date_from,\n' +
290+
'TIMESTAMP(DATETIME_SUB(DATETIME_ADD(DATETIME(d), INTERVAL {{ granularity }}), INTERVAL 1 MILLISECOND)) AS date_to \n' +
287291
'FROM UNNEST(\n' +
288292
'{% if minimal_time_unit|upper in ["DAY", "WEEK", "MONTH", "QUARTER", "YEAR"] %}' +
289293
'GENERATE_DATE_ARRAY(DATE({{ start }}), DATE({{ end }}), INTERVAL {{ granularity }})\n' +
@@ -292,8 +296,8 @@ export class BigqueryQuery extends BaseQuery {
292296
'{% endif %}' +
293297
') AS d';
294298

295-
templates.statements.generated_time_series_with_cte_range_source = 'SELECT DATETIME(d) AS date_from,\n' +
296-
'DATETIME_SUB(DATETIME_ADD(DATETIME(d), INTERVAL {{ granularity }}), INTERVAL 1 MILLISECOND) AS date_to \n' +
299+
templates.statements.generated_time_series_with_cte_range_source = 'SELECT TIMESTAMP(d) AS date_from,\n' +
300+
'TIMESTAMP(DATETIME_SUB(DATETIME_ADD(DATETIME(d), INTERVAL {{ granularity }}), INTERVAL 1 MILLISECOND)) AS date_to \n' +
297301
'FROM {{ range_source }}, UNNEST(\n' +
298302
'{% if minimal_time_unit|upper in ["DAY", "WEEK", "MONTH", "QUARTER", "YEAR"] %}' +
299303
'GENERATE_DATE_ARRAY(DATE({{ range_source }}.{{ min_name }}), DATE({{ range_source }}.{{ max_name }}), INTERVAL {{ granularity }})\n' +

0 commit comments

Comments
 (0)