Skip to content

Commit 7666e1e

Browse files
committed
refactor(schema-compiler): Extract testWithPreAggregation helper function
1 parent 7d38c8d commit 7666e1e

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { BaseQuery } from '../../../src';
2+
import { dbRunner } from './PostgresDBRunner';
3+
4+
export type QueryWithParams = [string, Array<unknown>];
5+
6+
export async function testWithPreAggregation(
7+
preAggregationsDescription: { loadSql: QueryWithParams, invalidateKeyQueries: Array<QueryWithParams> },
8+
query: BaseQuery,
9+
) {
10+
const preAggSql = preAggregationsDescription
11+
.loadSql[0]
12+
// Without `ON COMMIT DROP` temp tables are session-bound, and can live across multiple transactions
13+
.replace(/CREATE TABLE (.+) AS SELECT/, 'CREATE TEMP TABLE $1 ON COMMIT DROP AS SELECT');
14+
const preAggParams = preAggregationsDescription.loadSql[1];
15+
16+
const queries = [
17+
...preAggregationsDescription.invalidateKeyQueries,
18+
[preAggSql, preAggParams],
19+
query.buildSqlAndParams(),
20+
];
21+
22+
return dbRunner.testQueries(queries);
23+
}

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

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { getEnv } from '@cubejs-backend/shared';
22
import { UserError } from '../../../src/compiler/UserError';
3-
import type { BaseQuery } from '../../../src';
43
import { PostgresQuery } from '../../../src/adapter/PostgresQuery';
54
import { BigqueryQuery } from '../../../src/adapter/BigqueryQuery';
65
import { PrestodbQuery } from '../../../src/adapter/PrestodbQuery';
76
import { prepareJsCompiler } from '../../unit/PrepareCompiler';
87
import { dbRunner } from './PostgresDBRunner';
98
import { createJoinedCubesSchema } from '../../unit/utils';
9+
import { testWithPreAggregation } from './pre-aggregation-utils';
1010

1111
describe('SQL Generation', () => {
1212
jest.setTimeout(200000);
@@ -885,27 +885,6 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
885885
);
886886
}
887887

888-
type QueryWithParams = [string, Array<unknown>];
889-
890-
async function testWithPreAgg(
891-
preAggregationsDescription: { loadSql: QueryWithParams, invalidateKeyQueries: Array<QueryWithParams> },
892-
query: BaseQuery,
893-
) {
894-
const preAggSql = preAggregationsDescription
895-
.loadSql[0]
896-
// Without `ON COMMIT DROP` temp tables are session-bound, and can live across multiple transactions
897-
.replace(/CREATE TABLE (.+) AS SELECT/, 'CREATE TEMP TABLE $1 ON COMMIT DROP AS SELECT');
898-
const preAggParams = preAggregationsDescription.loadSql[1];
899-
900-
const queries = [
901-
...preAggregationsDescription.invalidateKeyQueries,
902-
[preAggSql, preAggParams],
903-
query.buildSqlAndParams(),
904-
];
905-
906-
return dbRunner.testQueries(queries);
907-
}
908-
909888
it('simple join total', async () => runQueryTest({
910889
measures: [
911890
'visitors.visitor_revenue',
@@ -2184,7 +2163,7 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
21842163

21852164
const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription()[0];
21862165

2187-
const res = await testWithPreAgg(preAggregationsDescription, query);
2166+
const res = await testWithPreAggregation(preAggregationsDescription, query);
21882167
expect(res).toEqual(
21892168
// Empty result set, only segments in query
21902169
[{}]
@@ -2225,7 +2204,7 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
22252204
const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription()[0];
22262205
console.log(preAggregationsDescription);
22272206

2228-
const res = await testWithPreAgg(preAggregationsDescription, query);
2207+
const res = await testWithPreAggregation(preAggregationsDescription, query);
22292208
console.log(JSON.stringify(res));
22302209
expect(res).toEqual(
22312210
[
@@ -2266,7 +2245,7 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
22662245
const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription()[0];
22672246
console.log(preAggregationsDescription);
22682247

2269-
const res = await testWithPreAgg(preAggregationsDescription, query);
2248+
const res = await testWithPreAggregation(preAggregationsDescription, query);
22702249
console.log(JSON.stringify(res));
22712250
expect(res).toEqual(
22722251
[{

0 commit comments

Comments
 (0)