From 18d0620e69c9a43a9d0e0d1634132959e40f6860 Mon Sep 17 00:00:00 2001 From: Pavel Tiunov Date: Wed, 21 Aug 2024 21:41:48 -0700 Subject: [PATCH] fix: Internal: ParserError("Expected AND, found: INTERVAL") in case of trailing and leading window parts are defined for pre-aggregated rolling window measure (#8611) --- .../src/adapter/CubeStoreQuery.ts | 2 +- .../postgresql/schema/PreAggregationTest.js | 11 +++++- ...i-postgresql-pre-aggregations.test.ts.snap | 35 +++++++++++++++++++ .../test/pre-aggregations-test-case.ts | 21 +++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/packages/cubejs-schema-compiler/src/adapter/CubeStoreQuery.ts b/packages/cubejs-schema-compiler/src/adapter/CubeStoreQuery.ts index 87c3fab47ca14..f714ded821ab8 100644 --- a/packages/cubejs-schema-compiler/src/adapter/CubeStoreQuery.ts +++ b/packages/cubejs-schema-compiler/src/adapter/CubeStoreQuery.ts @@ -229,7 +229,7 @@ export class CubeStoreQuery extends BaseQuery { const preceding = rollingWindow.trailing ? `${this.toInterval(rollingWindow.trailing)} PRECEDING` : ''; const following = rollingWindow.leading ? `${this.toInterval(rollingWindow.leading)} FOLLOWING` : ''; const offset = ` OFFSET ${rollingWindow.offset || 'end'}`; - const rollingMeasure = `ROLLING(${measureSql} ${preceding && following ? 'RANGE BETWEEN ' : 'RANGE '}${preceding}${preceding && following ? ' ' : ''}${following}${offset})`; + const rollingMeasure = `ROLLING(${measureSql} ${preceding && following ? 'RANGE BETWEEN ' : 'RANGE '}${preceding}${preceding && following ? ' AND ' : ''}${following}${offset})`; return this.topAggregateWrap(m.measureDefinition(), rollingMeasure); } else { const conditionFn = m.isCumulative() ? this.dateFromStartToEndConditionSql(m.dateJoinCondition(), true, true)[0] : timeDimension; diff --git a/packages/cubejs-testing/birdbox-fixtures/postgresql/schema/PreAggregationTest.js b/packages/cubejs-testing/birdbox-fixtures/postgresql/schema/PreAggregationTest.js index b5981b3f6af40..589eeb041b84f 100644 --- a/packages/cubejs-testing/birdbox-fixtures/postgresql/schema/PreAggregationTest.js +++ b/packages/cubejs-testing/birdbox-fixtures/postgresql/schema/PreAggregationTest.js @@ -42,6 +42,15 @@ cube(`visitors`, { } }, + checkinsPrevMonth: { + sql: `${checkinsCount}`, + type: 'sum', + rollingWindow: { + trailing: '60 day', + leading: '-30 day' + } + }, + uniqueSourceCount: { sql: 'source', type: 'countDistinct' @@ -174,7 +183,7 @@ cube(`visitors`, { }, partitionedRolling: { type: 'rollup', - measureReferences: [checkinsRollingTotal, checkinsRolling2day, count], + measureReferences: [checkinsRollingTotal, checkinsRolling2day, checkinsPrevMonth, count], dimensionReferences: [source], timeDimensionReference: createdAt, granularity: 'hour', diff --git a/packages/cubejs-testing/test/__snapshots__/cli-postgresql-pre-aggregations.test.ts.snap b/packages/cubejs-testing/test/__snapshots__/cli-postgresql-pre-aggregations.test.ts.snap index fc48303343348..bb945724fe91e 100644 --- a/packages/cubejs-testing/test/__snapshots__/cli-postgresql-pre-aggregations.test.ts.snap +++ b/packages/cubejs-testing/test/__snapshots__/cli-postgresql-pre-aggregations.test.ts.snap @@ -105,6 +105,41 @@ Array [ ] `; +exports[`postgresql HTTP Transport Rolling Prev Period: Rolling Prev Period 1`] = ` +Array [ + Object { + "visitors.checkinsPrevMonth": "1", + "visitors.createdAt": "2017-02-05T00:00:00.000", + "visitors.createdAt.day": "2017-02-05T00:00:00.000", + "visitors.source": "google", + }, + Object { + "visitors.checkinsPrevMonth": "3", + "visitors.createdAt": "2017-02-02T00:00:00.000", + "visitors.createdAt.day": "2017-02-02T00:00:00.000", + "visitors.source": "some", + }, + Object { + "visitors.checkinsPrevMonth": "3", + "visitors.createdAt": "2017-02-03T00:00:00.000", + "visitors.createdAt.day": "2017-02-03T00:00:00.000", + "visitors.source": "some", + }, + Object { + "visitors.checkinsPrevMonth": "5", + "visitors.createdAt": "2017-02-04T00:00:00.000", + "visitors.createdAt.day": "2017-02-04T00:00:00.000", + "visitors.source": "some", + }, + Object { + "visitors.checkinsPrevMonth": "5", + "visitors.createdAt": "2017-02-05T00:00:00.000", + "visitors.createdAt.day": "2017-02-05T00:00:00.000", + "visitors.source": "some", + }, +] +`; + exports[`postgresql HTTP Transport Rolling with Quarter granularity: Rolling with Quarter granularity 1`] = ` Array [ Object { diff --git a/packages/cubejs-testing/test/pre-aggregations-test-case.ts b/packages/cubejs-testing/test/pre-aggregations-test-case.ts index 44a9462463e33..aea4627f61922 100644 --- a/packages/cubejs-testing/test/pre-aggregations-test-case.ts +++ b/packages/cubejs-testing/test/pre-aggregations-test-case.ts @@ -95,6 +95,27 @@ const asserts: [options: QueryTestOptions, query: Query][] = [ } } ], + [ + { name: 'Rolling Prev Period' }, + { + measures: [ + 'visitors.checkinsPrevMonth' + ], + dimensions: [ + 'visitors.source' + ], + timezone: 'UTC', + timeDimensions: [{ + dimension: 'visitors.createdAt', + granularity: 'day', + dateRange: ['2017-02-02', '2017-02-05'] + }], + order: { + 'visitors.createdAt': 'asc', + 'visitors.source': 'asc' + } + } + ], [ { name: 'Rolling Mixed With Dimension No Granularity' }, {