Skip to content

Commit b3d2505

Browse files
committed
fix(schema-compiler): Fix date filter truncation for rolling window queries
1 parent a8b611c commit b3d2505

File tree

2 files changed

+114
-11
lines changed

2 files changed

+114
-11
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/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'

0 commit comments

Comments
 (0)