Skip to content

Commit 1f6cf8f

Browse files
authored
fix(schema-compiler): Fix BigQuery rolling window time series queries (#9718)
* fix(schema-compiler): Fix BigQuery rolling window time series queries Fix for datetime ←→ timestamp comparisons * add tests * update bq snapshot * update athena snapshot * update databricks snapshots * update mssql snapshots * update mysql snapshots * update postgres snapshots * fix snapshots * upd redshift snapshots * update snowflake snapshots * fix snapshot name * fix tests
1 parent c16175b commit 1f6cf8f

28 files changed

+6557
-5314
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,8 @@ export class BaseQuery {
19631963
}
19641964

19651965
/**
1966+
* BigQuery has strict date type and can not automatically convert between date
1967+
* and timestamp, so we override seriesSql() in BigQuery Dialect
19661968
* @param {import('./BaseTimeDimension').BaseTimeDimension} timeDimension
19671969
* @return {string}
19681970
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class BigqueryQuery extends BaseQuery {
146146
const values = timeDimension.timeSeries().map(
147147
([from, to]) => `select '${from}' f, '${to}' t`
148148
).join(' UNION ALL ');
149-
return `SELECT ${this.dateTimeCast('dates.f')} date_from, ${this.dateTimeCast('dates.t')} date_to FROM (${values}) AS dates`;
149+
return `SELECT ${this.timeStampCast('dates.f')} date_from, ${this.timeStampCast('dates.t')} date_to FROM (${values}) AS dates`;
150150
}
151151

152152
public timestampFormat() {

packages/cubejs-testing-drivers/fixtures/_schemas.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@
370370
"sql": "profit",
371371
"type": "sum"
372372
},
373+
{
374+
"name": "customersCountPrev1Month",
375+
"type": "count",
376+
"rolling_window": {
377+
"trailing": "1 month",
378+
"offset": "start"
379+
}
380+
},
373381
{
374382
"name": "totalProfitYearAgo",
375383
"type": "number",

packages/cubejs-testing-drivers/fixtures/clickhouse.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
"querying BigECommerce: rolling window YTD (month + week + no gran)",
192192
"querying BigECommerce: rolling window YTD (month + week + day)",
193193
"querying BigECommerce: rolling window YTD (month + week + day + no gran)",
194+
"querying BigECommerce: time series in rolling window",
194195

195196
"---------------------------------------",
196197
"Requires Tesseract. ",

packages/cubejs-testing-drivers/src/tests/testQueries.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,23 @@ export function testQueries(type: string, { includeIncrementalSchemaSuite, exten
14921492
expect(response.rawData()).toMatchSnapshot();
14931493
});
14941494

1495+
execute('querying BigECommerce: time series in rolling window', async () => {
1496+
const response = await client.load({
1497+
measures: [
1498+
'BigECommerce.customersCountPrev1Month',
1499+
],
1500+
timeDimensions: [{
1501+
dimension: 'BigECommerce.orderDate',
1502+
granularity: 'month',
1503+
dateRange: ['2020-01-01', '2020-12-31'],
1504+
}],
1505+
order: {
1506+
'BigECommerce.orderDate': 'asc',
1507+
}
1508+
});
1509+
expect(response.rawData()).toMatchSnapshot();
1510+
});
1511+
14951512
execute('querying BigECommerce: null sum', async () => {
14961513
const response = await client.load({
14971514
measures: [

packages/cubejs-testing-drivers/test/__snapshots__/athena-export-bucket-s3-full.test.ts.snap

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4411,14 +4411,6 @@ Array [
44114411
]
44124412
`;
44134413

4414-
exports[`Queries with the @cubejs-backend/athena-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
4415-
Array [
4416-
Object {
4417-
"BigECommerce.rollingCountYTD": "44",
4418-
},
4419-
]
4420-
`;
4421-
44224414
exports[`Queries with the @cubejs-backend/athena-driver querying BigECommerce: rolling window YTD without date range 1`] = `
44234415
Array [
44244416
Object {
@@ -4484,6 +4476,14 @@ Array [
44844476
]
44854477
`;
44864478

4479+
exports[`Queries with the @cubejs-backend/athena-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
4480+
Array [
4481+
Object {
4482+
"BigECommerce.rollingCountYTD": "44",
4483+
},
4484+
]
4485+
`;
4486+
44874487
exports[`Queries with the @cubejs-backend/athena-driver querying BigECommerce: rolling window by 2 day 1`] = `
44884488
Array [
44894489
Object {
@@ -4744,6 +4744,71 @@ Array [
47444744
]
47454745
`;
47464746

4747+
exports[`Queries with the @cubejs-backend/athena-driver querying BigECommerce: time series in rolling window 1`] = `
4748+
Array [
4749+
Object {
4750+
"BigECommerce.customersCountPrev1Month": null,
4751+
"BigECommerce.orderDate": "2020-01-01T00:00:00.000",
4752+
"BigECommerce.orderDate.month": "2020-01-01T00:00:00.000",
4753+
},
4754+
Object {
4755+
"BigECommerce.customersCountPrev1Month": "2",
4756+
"BigECommerce.orderDate": "2020-02-01T00:00:00.000",
4757+
"BigECommerce.orderDate.month": "2020-02-01T00:00:00.000",
4758+
},
4759+
Object {
4760+
"BigECommerce.customersCountPrev1Month": "1",
4761+
"BigECommerce.orderDate": "2020-03-01T00:00:00.000",
4762+
"BigECommerce.orderDate.month": "2020-03-01T00:00:00.000",
4763+
},
4764+
Object {
4765+
"BigECommerce.customersCountPrev1Month": "2",
4766+
"BigECommerce.orderDate": "2020-04-01T00:00:00.000",
4767+
"BigECommerce.orderDate.month": "2020-04-01T00:00:00.000",
4768+
},
4769+
Object {
4770+
"BigECommerce.customersCountPrev1Month": "1",
4771+
"BigECommerce.orderDate": "2020-05-01T00:00:00.000",
4772+
"BigECommerce.orderDate.month": "2020-05-01T00:00:00.000",
4773+
},
4774+
Object {
4775+
"BigECommerce.customersCountPrev1Month": "5",
4776+
"BigECommerce.orderDate": "2020-06-01T00:00:00.000",
4777+
"BigECommerce.orderDate.month": "2020-06-01T00:00:00.000",
4778+
},
4779+
Object {
4780+
"BigECommerce.customersCountPrev1Month": "7",
4781+
"BigECommerce.orderDate": "2020-07-01T00:00:00.000",
4782+
"BigECommerce.orderDate.month": "2020-07-01T00:00:00.000",
4783+
},
4784+
Object {
4785+
"BigECommerce.customersCountPrev1Month": null,
4786+
"BigECommerce.orderDate": "2020-08-01T00:00:00.000",
4787+
"BigECommerce.orderDate.month": "2020-08-01T00:00:00.000",
4788+
},
4789+
Object {
4790+
"BigECommerce.customersCountPrev1Month": null,
4791+
"BigECommerce.orderDate": "2020-09-01T00:00:00.000",
4792+
"BigECommerce.orderDate.month": "2020-09-01T00:00:00.000",
4793+
},
4794+
Object {
4795+
"BigECommerce.customersCountPrev1Month": "6",
4796+
"BigECommerce.orderDate": "2020-10-01T00:00:00.000",
4797+
"BigECommerce.orderDate.month": "2020-10-01T00:00:00.000",
4798+
},
4799+
Object {
4800+
"BigECommerce.customersCountPrev1Month": "4",
4801+
"BigECommerce.orderDate": "2020-11-01T00:00:00.000",
4802+
"BigECommerce.orderDate.month": "2020-11-01T00:00:00.000",
4803+
},
4804+
Object {
4805+
"BigECommerce.customersCountPrev1Month": "9",
4806+
"BigECommerce.orderDate": "2020-12-01T00:00:00.000",
4807+
"BigECommerce.orderDate.month": "2020-12-01T00:00:00.000",
4808+
},
4809+
]
4810+
`;
4811+
47474812
exports[`Queries with the @cubejs-backend/athena-driver querying BigECommerce: totalProfitYearAgo 1`] = `Array []`;
47484813

47494814
exports[`Queries with the @cubejs-backend/athena-driver querying Customers: dimensions + limit 1`] = `

0 commit comments

Comments
 (0)
close