Skip to content

Commit f48495c

Browse files
authored
fix(gateway): dryRun to return pre-rewritten queries (#9091)
1 parent 646bd9d commit f48495c

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

packages/cubejs-api-gateway/src/gateway.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ class ApiGateway {
11611161
context: RequestContext,
11621162
persistent = false,
11631163
memberExpressions: boolean = false,
1164-
): Promise<[QueryType, NormalizedQuery[]]> {
1164+
): Promise<[QueryType, NormalizedQuery[], NormalizedQuery[]]> {
11651165
let query = this.parseQueryParam(inputQuery);
11661166

11671167
let queryType: QueryType = QueryTypeEnum.REGULAR_QUERY;
@@ -1184,28 +1184,35 @@ class ApiGateway {
11841184
const startTime = new Date().getTime();
11851185
const compilerApi = await this.getCompilerApi(context);
11861186

1187-
let normalizedQueries: NormalizedQuery[] = await Promise.all(
1188-
queries.map(
1189-
async (currentQuery) => {
1190-
const hasExpressionsInQuery =
1191-
this.hasExpressionsInQuery(currentQuery);
1187+
const queryNormalizationResult: Array<{
1188+
normalizedQuery: NormalizedQuery,
1189+
hasExpressionsInQuery: boolean
1190+
}> = queries.map((currentQuery) => {
1191+
const hasExpressionsInQuery = this.hasExpressionsInQuery(currentQuery);
11921192

1193-
if (hasExpressionsInQuery) {
1194-
if (!memberExpressions) {
1195-
throw new Error('Expressions are not allowed in this context');
1196-
}
1193+
if (hasExpressionsInQuery) {
1194+
if (!memberExpressions) {
1195+
throw new Error('Expressions are not allowed in this context');
1196+
}
11971197

1198-
currentQuery = this.parseMemberExpressionsInQuery(currentQuery);
1199-
}
1198+
currentQuery = this.parseMemberExpressionsInQuery(currentQuery);
1199+
}
12001200

1201-
const normalizedQuery = normalizeQuery(currentQuery, persistent);
1202-
let evaluatedQuery = normalizedQuery;
1201+
return {
1202+
normalizedQuery: (normalizeQuery(currentQuery, persistent)),
1203+
hasExpressionsInQuery
1204+
};
1205+
});
1206+
1207+
let normalizedQueries: NormalizedQuery[] = await Promise.all(
1208+
queryNormalizationResult.map(
1209+
async ({ normalizedQuery, hasExpressionsInQuery }) => {
1210+
let evaluatedQuery: Query | NormalizedQuery = normalizedQuery;
12031211

12041212
if (hasExpressionsInQuery) {
12051213
// We need to parse/eval all member expressions early as applyRowLevelSecurity
12061214
// needs to access the full SQL query in order to evaluate rules
1207-
evaluatedQuery =
1208-
this.evalMemberExpressionsInQuery(normalizedQuery);
1215+
evaluatedQuery = this.evalMemberExpressionsInQuery(normalizedQuery);
12091216
}
12101217

12111218
// First apply cube/view level security policies
@@ -1257,7 +1264,7 @@ class ApiGateway {
12571264
}
12581265
}
12591266

1260-
return [queryType, normalizedQueries];
1267+
return [queryType, normalizedQueries, queryNormalizationResult.map((it) => remapToQueryAdapterFormat(it.normalizedQuery))];
12611268
}
12621269

12631270
public async sql({
@@ -1454,7 +1461,7 @@ class ApiGateway {
14541461
try {
14551462
await this.assertApiScope('data', context.securityContext);
14561463

1457-
const [queryType, normalizedQueries] = await this.getNormalizedQueries(query, context);
1464+
const [queryType, _, normalizedQueries] = await this.getNormalizedQueries(query, context, undefined, undefined);
14581465

14591466
const sqlQueries = await Promise.all<any>(
14601467
normalizedQueries.map(async (normalizedQuery) => (await this.getCompilerApi(context)).getSql(

0 commit comments

Comments
 (0)