Skip to content

Commit 0b52588

Browse files
KSDaemonFrank-TXS
authored andcommitted
fix(schema-compiler): Fix date filter truncation for rolling window queries (cube-js#9743)
* fix(schema-compiler): Fix date filter truncation for rolling window queries * fix snapshots * fix BigQuery Dialect
1 parent 36b55da commit 0b52588

File tree

31 files changed

+177
-49
lines changed

31 files changed

+177
-49
lines changed

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

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,23 +1037,48 @@ export class BaseQuery {
10371037
}
10381038

10391039
rollingWindowToDateJoinCondition(granularity) {
1040-
return this.timeDimensions
1041-
.filter(td => td.granularity)
1042-
.map(
1043-
d => [
1044-
d,
1045-
(dateFrom, dateTo, dateField, _dimensionDateFrom, _dimensionDateTo, _isFromStartToEnd) => `${dateField} >= ${this.timeGroupedColumn(granularity, dateFrom)} AND ${dateField} <= ${dateTo}`
1046-
]
1047-
);
1040+
return Object.values(
1041+
this.timeDimensions.reduce((acc, td) => {
1042+
const key = td.dimension;
1043+
1044+
if (!acc[key]) {
1045+
acc[key] = td;
1046+
}
1047+
1048+
if (!acc[key].granularity && td.granularity) {
1049+
acc[key] = td;
1050+
}
1051+
1052+
return acc;
1053+
}, {})
1054+
).map(
1055+
d => [
1056+
d,
1057+
(dateFrom, dateTo, dateField, _dimensionDateFrom, _dimensionDateTo, _isFromStartToEnd) => `${dateField} >= ${this.timeGroupedColumn(granularity, dateFrom)} AND ${dateField} <= ${dateTo}`
1058+
]
1059+
);
10481060
}
10491061

10501062
rollingWindowDateJoinCondition(trailingInterval, leadingInterval, offset) {
10511063
offset = offset || 'end';
1052-
return this.timeDimensions
1053-
.filter(td => td.granularity)
1064+
return Object.values(
1065+
this.timeDimensions.reduce((acc, td) => {
1066+
const key = td.dimension;
1067+
1068+
if (!acc[key]) {
1069+
acc[key] = td;
1070+
}
1071+
1072+
if (!acc[key].granularity && td.granularity) {
1073+
acc[key] = td;
1074+
}
1075+
1076+
return acc;
1077+
}, {})
1078+
)
10541079
.map(
10551080
d => [d, (dateFrom, dateTo, dateField, _dimensionDateFrom, _dimensionDateTo, isFromStartToEnd) => {
1056-
// dateFrom based window
1081+
// dateFrom based window
10571082
const conditions = [];
10581083
if (trailingInterval !== 'unbounded') {
10591084
const startDate = isFromStartToEnd || offset === 'start' ? dateFrom : dateTo;

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,26 @@ export class BigqueryQuery extends BaseQuery {
216216
* joining conditions (note timeStampCast)
217217
*/
218218
public override rollingWindowToDateJoinCondition(granularity) {
219-
return this.timeDimensions
220-
.filter(td => td.granularity)
221-
.map(
222-
d => [
223-
d,
224-
(dateFrom: string, dateTo: string, dateField: string, _dimensionDateFrom: string, _dimensionDateTo: string, _isFromStartToEnd: boolean) => `${dateField} >= ${this.timeGroupedColumn(granularity, dateFrom)} AND ${dateField} <= ${this.timeStampCast(dateTo)}`
225-
]
226-
);
219+
return Object.values(
220+
this.timeDimensions.reduce((acc, td) => {
221+
const key = td.dimension;
222+
223+
if (!acc[key]) {
224+
acc[key] = td;
225+
}
226+
227+
if (!acc[key].granularity && td.granularity) {
228+
acc[key] = td;
229+
}
230+
231+
return acc;
232+
}, {})
233+
).map(
234+
d => [
235+
d,
236+
(dateFrom: string, dateTo: string, dateField: string, _dimensionDateFrom: string, _dimensionDateTo: string, _isFromStartToEnd: boolean) => `${dateField} >= ${this.timeGroupedColumn(granularity, dateFrom)} AND ${dateField} <= ${this.timeStampCast(dateTo)}`
237+
]
238+
);
227239
}
228240

229241
/**
@@ -233,8 +245,21 @@ export class BigqueryQuery extends BaseQuery {
233245
*/
234246
public override rollingWindowDateJoinCondition(trailingInterval, leadingInterval, offset) {
235247
offset = offset || 'end';
236-
return this.timeDimensions
237-
.filter(td => td.granularity)
248+
return Object.values(
249+
this.timeDimensions.reduce((acc, td) => {
250+
const key = td.dimension;
251+
252+
if (!acc[key]) {
253+
acc[key] = td;
254+
}
255+
256+
if (!acc[key].granularity && td.granularity) {
257+
acc[key] = td;
258+
}
259+
260+
return acc;
261+
}, {})
262+
)
238263
.map(
239264
d => [d, (dateFrom: string, dateTo: string, dateField: string, _dimensionDateFrom: string, _dimensionDateTo: string, isFromStartToEnd: boolean) => {
240265
// dateFrom based window

packages/cubejs-schema-compiler/test/integration/postgres/sql-generation.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,84 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
10701070
}
10711071
]));
10721072

1073+
it('rolling window with one time dimension with granularity', async () => runQueryTest({
1074+
measures: [
1075+
'visitors.countRollingWeekToDate'
1076+
],
1077+
timeDimensions: [
1078+
{
1079+
dimension: 'visitors.created_at',
1080+
granularity: 'day',
1081+
dateRange: ['2017-01-01', '2017-01-10']
1082+
}
1083+
],
1084+
order: [{
1085+
id: 'visitors.created_at'
1086+
}],
1087+
timezone: 'America/Los_Angeles'
1088+
}, [
1089+
{
1090+
visitors__count_rolling_week_to_date: null,
1091+
visitors__created_at_day: '2017-01-01T00:00:00.000Z',
1092+
},
1093+
{
1094+
visitors__count_rolling_week_to_date: '1',
1095+
visitors__created_at_day: '2017-01-02T00:00:00.000Z',
1096+
},
1097+
{
1098+
visitors__count_rolling_week_to_date: '1',
1099+
visitors__created_at_day: '2017-01-03T00:00:00.000Z',
1100+
},
1101+
{
1102+
visitors__count_rolling_week_to_date: '2',
1103+
visitors__created_at_day: '2017-01-04T00:00:00.000Z',
1104+
},
1105+
{
1106+
visitors__count_rolling_week_to_date: '3',
1107+
visitors__created_at_day: '2017-01-05T00:00:00.000Z',
1108+
},
1109+
{
1110+
visitors__count_rolling_week_to_date: '5',
1111+
visitors__created_at_day: '2017-01-06T00:00:00.000Z',
1112+
},
1113+
{
1114+
visitors__count_rolling_week_to_date: '5',
1115+
visitors__created_at_day: '2017-01-07T00:00:00.000Z',
1116+
},
1117+
{
1118+
visitors__count_rolling_week_to_date: '5',
1119+
visitors__created_at_day: '2017-01-08T00:00:00.000Z',
1120+
},
1121+
{
1122+
visitors__count_rolling_week_to_date: null,
1123+
visitors__created_at_day: '2017-01-09T00:00:00.000Z',
1124+
},
1125+
{
1126+
visitors__count_rolling_week_to_date: null,
1127+
visitors__created_at_day: '2017-01-10T00:00:00.000Z',
1128+
},
1129+
]));
1130+
1131+
it('rolling window with one time dimension without granularity', async () => runQueryTest({
1132+
measures: [
1133+
'visitors.countRollingWeekToDate'
1134+
],
1135+
timeDimensions: [
1136+
{
1137+
dimension: 'visitors.created_at',
1138+
dateRange: ['2017-01-01', '2017-01-10']
1139+
}
1140+
],
1141+
order: [{
1142+
id: 'visitors.created_at'
1143+
}],
1144+
timezone: 'America/Los_Angeles'
1145+
}, [
1146+
{
1147+
visitors__count_rolling_week_to_date: '5',
1148+
}
1149+
]));
1150+
10731151
it('rolling window with two time dimension granularities', async () => runQueryTest({
10741152
measures: [
10751153
'visitors.countRollingWeekToDate'

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4479,7 +4479,7 @@ Array [
44794479
exports[`Queries with the @cubejs-backend/athena-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
44804480
Array [
44814481
Object {
4482-
"BigECommerce.rollingCountYTD": "44",
4482+
"BigECommerce.rollingCountYTD": "3",
44834483
},
44844484
]
44854485
`;

packages/cubejs-testing-drivers/test/__snapshots__/bigquery-export-bucket-gcs-full.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8149,7 +8149,7 @@ Array [
81498149
exports[`Queries with the @cubejs-backend/bigquery-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
81508150
Array [
81518151
Object {
8152-
"BigECommerce.rollingCountYTD": 44,
8152+
"BigECommerce.rollingCountYTD": 3,
81538153
},
81548154
]
81558155
`;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4680,7 +4680,7 @@ Array [
46804680
exports[`Queries with the @cubejs-backend/clickhouse-driver export-bucket-s3 querying BigECommerce: rolling window YTD without granularity 1`] = `
46814681
Array [
46824682
Object {
4683-
"BigECommerce.rollingCountYTD": "44",
4683+
"BigECommerce.rollingCountYTD": "3",
46844684
},
46854685
]
46864686
`;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4680,7 +4680,7 @@ Array [
46804680
exports[`Queries with the @cubejs-backend/clickhouse-driver export-bucket-s3-prefix querying BigECommerce: rolling window YTD without granularity 1`] = `
46814681
Array [
46824682
Object {
4683-
"BigECommerce.rollingCountYTD": "44",
4683+
"BigECommerce.rollingCountYTD": "3",
46844684
},
46854685
]
46864686
`;

packages/cubejs-testing-drivers/test/__snapshots__/clickhouse-full.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4680,7 +4680,7 @@ Array [
46804680
exports[`Queries with the @cubejs-backend/clickhouse-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
46814681
Array [
46824682
Object {
4683-
"BigECommerce.rollingCountYTD": "44",
4683+
"BigECommerce.rollingCountYTD": "3",
46844684
},
46854685
]
46864686
`;

packages/cubejs-testing-drivers/test/__snapshots__/databricks-jdbc-export-bucket-azure-full.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10885,7 +10885,7 @@ Array [
1088510885
exports[`Queries with the @cubejs-backend/databricks-jdbc-driver export-bucket-azure querying BigECommerce: rolling window YTD without granularity 1`] = `
1088610886
Array [
1088710887
Object {
10888-
"BigECommerce.rollingCountYTD": "44",
10888+
"BigECommerce.rollingCountYTD": "3",
1088910889
},
1089010890
]
1089110891
`;

packages/cubejs-testing-drivers/test/__snapshots__/databricks-jdbc-export-bucket-azure-prefix-full.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10690,7 +10690,7 @@ Array [
1069010690
exports[`Queries with the @cubejs-backend/databricks-jdbc-driver export-bucket-azure-prefix querying BigECommerce: rolling window YTD without granularity 1`] = `
1069110691
Array [
1069210692
Object {
10693-
"BigECommerce.rollingCountYTD": "44",
10693+
"BigECommerce.rollingCountYTD": "3",
1069410694
},
1069510695
]
1069610696
`;

0 commit comments

Comments
 (0)