Skip to content

fix(schema-compiler): Fix date filter truncation for rolling window queries #9743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -1037,23 +1037,48 @@ export class BaseQuery {
}

rollingWindowToDateJoinCondition(granularity) {
return this.timeDimensions
.filter(td => td.granularity)
.map(
d => [
d,
(dateFrom, dateTo, dateField, _dimensionDateFrom, _dimensionDateTo, _isFromStartToEnd) => `${dateField} >= ${this.timeGroupedColumn(granularity, dateFrom)} AND ${dateField} <= ${dateTo}`
]
);
return Object.values(
this.timeDimensions.reduce((acc, td) => {
const key = td.dimension;

if (!acc[key]) {
acc[key] = td;
}

if (!acc[key].granularity && td.granularity) {
acc[key] = td;
}

return acc;
}, {})
).map(
d => [
d,
(dateFrom, dateTo, dateField, _dimensionDateFrom, _dimensionDateTo, _isFromStartToEnd) => `${dateField} >= ${this.timeGroupedColumn(granularity, dateFrom)} AND ${dateField} <= ${dateTo}`
]
);
}

rollingWindowDateJoinCondition(trailingInterval, leadingInterval, offset) {
offset = offset || 'end';
return this.timeDimensions
.filter(td => td.granularity)
return Object.values(
this.timeDimensions.reduce((acc, td) => {
const key = td.dimension;

if (!acc[key]) {
acc[key] = td;
}

if (!acc[key].granularity && td.granularity) {
acc[key] = td;
}

return acc;
}, {})
)
.map(
d => [d, (dateFrom, dateTo, dateField, _dimensionDateFrom, _dimensionDateTo, isFromStartToEnd) => {
// dateFrom based window
// dateFrom based window
const conditions = [];
if (trailingInterval !== 'unbounded') {
const startDate = isFromStartToEnd || offset === 'start' ? dateFrom : dateTo;
Expand Down
45 changes: 35 additions & 10 deletions packages/cubejs-schema-compiler/src/adapter/BigqueryQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,26 @@ export class BigqueryQuery extends BaseQuery {
* joining conditions (note timeStampCast)
*/
public override rollingWindowToDateJoinCondition(granularity) {
return this.timeDimensions
.filter(td => td.granularity)
.map(
d => [
d,
(dateFrom: string, dateTo: string, dateField: string, _dimensionDateFrom: string, _dimensionDateTo: string, _isFromStartToEnd: boolean) => `${dateField} >= ${this.timeGroupedColumn(granularity, dateFrom)} AND ${dateField} <= ${this.timeStampCast(dateTo)}`
]
);
return Object.values(
this.timeDimensions.reduce((acc, td) => {
const key = td.dimension;

if (!acc[key]) {
acc[key] = td;
}

if (!acc[key].granularity && td.granularity) {
acc[key] = td;
}

return acc;
}, {})
).map(
d => [
d,
(dateFrom: string, dateTo: string, dateField: string, _dimensionDateFrom: string, _dimensionDateTo: string, _isFromStartToEnd: boolean) => `${dateField} >= ${this.timeGroupedColumn(granularity, dateFrom)} AND ${dateField} <= ${this.timeStampCast(dateTo)}`
]
);
}

/**
Expand All @@ -233,8 +245,21 @@ export class BigqueryQuery extends BaseQuery {
*/
public override rollingWindowDateJoinCondition(trailingInterval, leadingInterval, offset) {
offset = offset || 'end';
return this.timeDimensions
.filter(td => td.granularity)
return Object.values(
this.timeDimensions.reduce((acc, td) => {
const key = td.dimension;

if (!acc[key]) {
acc[key] = td;
}

if (!acc[key].granularity && td.granularity) {
acc[key] = td;
}

return acc;
}, {})
)
.map(
d => [d, (dateFrom: string, dateTo: string, dateField: string, _dimensionDateFrom: string, _dimensionDateTo: string, isFromStartToEnd: boolean) => {
// dateFrom based window
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,84 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
}
]));

it('rolling window with one time dimension with granularity', async () => runQueryTest({
measures: [
'visitors.countRollingWeekToDate'
],
timeDimensions: [
{
dimension: 'visitors.created_at',
granularity: 'day',
dateRange: ['2017-01-01', '2017-01-10']
}
],
order: [{
id: 'visitors.created_at'
}],
timezone: 'America/Los_Angeles'
}, [
{
visitors__count_rolling_week_to_date: null,
visitors__created_at_day: '2017-01-01T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '1',
visitors__created_at_day: '2017-01-02T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '1',
visitors__created_at_day: '2017-01-03T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '2',
visitors__created_at_day: '2017-01-04T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '3',
visitors__created_at_day: '2017-01-05T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '5',
visitors__created_at_day: '2017-01-06T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '5',
visitors__created_at_day: '2017-01-07T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: '5',
visitors__created_at_day: '2017-01-08T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: null,
visitors__created_at_day: '2017-01-09T00:00:00.000Z',
},
{
visitors__count_rolling_week_to_date: null,
visitors__created_at_day: '2017-01-10T00:00:00.000Z',
},
]));

it('rolling window with one time dimension without granularity', async () => runQueryTest({
measures: [
'visitors.countRollingWeekToDate'
],
timeDimensions: [
{
dimension: 'visitors.created_at',
dateRange: ['2017-01-01', '2017-01-10']
}
],
order: [{
id: 'visitors.created_at'
}],
timezone: 'America/Los_Angeles'
}, [
{
visitors__count_rolling_week_to_date: '5',
}
]));

it('rolling window with two time dimension granularities', async () => runQueryTest({
measures: [
'visitors.countRollingWeekToDate'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4479,7 +4479,7 @@ Array [
exports[`Queries with the @cubejs-backend/athena-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8149,7 +8149,7 @@ Array [
exports[`Queries with the @cubejs-backend/bigquery-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": 44,
"BigECommerce.rollingCountYTD": 3,
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4680,7 +4680,7 @@ Array [
exports[`Queries with the @cubejs-backend/clickhouse-driver export-bucket-s3 querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4680,7 +4680,7 @@ Array [
exports[`Queries with the @cubejs-backend/clickhouse-driver export-bucket-s3-prefix querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4680,7 +4680,7 @@ Array [
exports[`Queries with the @cubejs-backend/clickhouse-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10885,7 +10885,7 @@ Array [
exports[`Queries with the @cubejs-backend/databricks-jdbc-driver export-bucket-azure querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10690,7 +10690,7 @@ Array [
exports[`Queries with the @cubejs-backend/databricks-jdbc-driver export-bucket-azure-prefix querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10885,7 +10885,7 @@ Array [
exports[`Queries with the @cubejs-backend/databricks-jdbc-driver export-bucket-gcs querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10690,7 +10690,7 @@ Array [
exports[`Queries with the @cubejs-backend/databricks-jdbc-driver export-bucket-gcs-prefix querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10885,7 +10885,7 @@ Array [
exports[`Queries with the @cubejs-backend/databricks-jdbc-driver export-bucket-s3 querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10690,7 +10690,7 @@ Array [
exports[`Queries with the @cubejs-backend/databricks-jdbc-driver export-bucket-s3-prefix querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10885,7 +10885,7 @@ Array [
exports[`Queries with the @cubejs-backend/databricks-jdbc-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3965,7 +3965,7 @@ Array [
exports[`Queries with the @cubejs-backend/mssql-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "440000",
"BigECommerce.rollingCountYTD": "30000",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3978,7 +3978,7 @@ Array [
exports[`Queries with the @cubejs-backend/mysql-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": 44,
"BigECommerce.rollingCountYTD": 3,
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13021,7 +13021,7 @@ Array [
exports[`Queries with the @cubejs-backend/postgres-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12753,7 +12753,7 @@ Array [
exports[`Queries with the @cubejs-backend/redshift-driver export-bucket-s3 querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12753,7 +12753,7 @@ Array [
exports[`Queries with the @cubejs-backend/redshift-driver querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17984,7 +17984,7 @@ Array [
exports[`Queries with the @cubejs-backend/snowflake-driver encrypted-pk querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12718,7 +12718,7 @@ Array [
exports[`Queries with the @cubejs-backend/snowflake-driver export-bucket-azure querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12523,7 +12523,7 @@ Array [
exports[`Queries with the @cubejs-backend/snowflake-driver export-bucket-azure-prefix querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12925,7 +12925,7 @@ Array [
exports[`Queries with the @cubejs-backend/snowflake-driver export-bucket-azure-via-storage-integration querying BigECommerce: rolling window YTD without granularity 1`] = `
Array [
Object {
"BigECommerce.rollingCountYTD": "44",
"BigECommerce.rollingCountYTD": "3",
},
]
`;
Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy