diff --git a/packages/cubejs-schema-compiler/test/unit/pre-aggregations.test.ts b/packages/cubejs-schema-compiler/test/unit/pre-aggregations.test.ts index fac3744374c3e..3d762722414cb 100644 --- a/packages/cubejs-schema-compiler/test/unit/pre-aggregations.test.ts +++ b/packages/cubejs-schema-compiler/test/unit/pre-aggregations.test.ts @@ -9,36 +9,36 @@ describe('pre-aggregations', () => { ` cube(\`Users\`, { sql: \`SELECT * FROM public.users\`, - + preAggregations: { usersRollup: { dimensions: [CUBE.id], }, }, - + measures: { count: { type: \`count\`, }, }, - + dimensions: { id: { sql: \`id\`, type: \`string\`, primaryKey: true, }, - + name: { sql: \`name\`, type: \`string\`, }, }, }); - + cube('Orders', { sql: \`SELECT * FROM orders\`, - + preAggregations: { ordersRollup: { measures: [CUBE.count], @@ -52,20 +52,20 @@ describe('pre-aggregations', () => { rollups: [Users.usersRollup, CUBE.ordersRollup], }, }, - + joins: { Users: { relationship: \`belongsTo\`, sql: \`\${CUBE.userId} = \${Users.id}\`, }, }, - + measures: { count: { type: \`count\`, }, }, - + dimensions: { id: { sql: \`id\`, @@ -232,4 +232,79 @@ describe('pre-aggregations', () => { expect(indexesSql[0].indexName).toEqual('orders_indexes_orders_by_day_with_day_by_status_regular_index'); expect(indexesSql[1].indexName).toEqual('orders_indexes_orders_by_day_with_day_by_status_agg_index'); }); + + it('pre-aggregation with FILTER_PARAMS', async () => { + const { compiler, cubeEvaluator, joinGraph } = prepareYamlCompiler( + createSchemaYaml({ + cubes: [ + { + name: 'orders', + sql_table: 'orders', + measures: [{ + name: 'count', + type: 'count', + }], + dimensions: [ + { + name: 'created_at', + sql: 'created_at', + type: 'time', + }, + { + name: 'updated_at', + sql: '{created_at}', + type: 'time', + }, + { + name: 'status', + sql: 'status', + type: 'string', + } + ], + preAggregations: [ + { + name: 'orders_by_day_with_day', + measures: ['count'], + dimensions: ['status'], + timeDimension: 'created_at', + granularity: 'day', + partition_granularity: 'day', + build_range_start: { + sql: 'SELECT \'2022-01-01\'::timestamp', + }, + build_range_end: { + sql: 'SELECT \'2024-01-01\'::timestamp' + }, + refresh_key: { + every: '4 hours', + sql: ` + SELECT max(created_at) as max_created_at + FROM orders + WHERE {FILTER_PARAMS.orders.created_at.filter('date(created_at)')}`, + }, + }, + ] + } + ] + }) + ); + + await compiler.compile(); + + const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, { + measures: [ + 'orders.count' + ], + timeDimensions: [{ + dimension: 'orders.created_at', + granularity: 'day', + dateRange: ['2023-01-01', '2023-01-10'] + }], + dimensions: ['orders.status'] + }); + + const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription(); + expect(preAggregationsDescription[0].loadSql[0].includes('WHERE ("orders".created_at >= $1::timestamptz AND "orders".created_at <= $2::timestamptz)')).toBeTruthy(); + expect(preAggregationsDescription[0].loadSql[1]).toEqual(['__FROM_PARTITION_RANGE', '__TO_PARTITION_RANGE']); + }); });