Skip to content
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

fix(gateway): dryRun to return pre-rewritten queries #9091

Merged
merged 5 commits into from
Jan 14, 2025
Merged
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
43 changes: 25 additions & 18 deletions packages/cubejs-api-gateway/src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ class ApiGateway {
context: RequestContext,
persistent = false,
memberExpressions: boolean = false,
): Promise<[QueryType, NormalizedQuery[]]> {
): Promise<[QueryType, NormalizedQuery[], NormalizedQuery[]]> {
let query = this.parseQueryParam(inputQuery);

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

let normalizedQueries: NormalizedQuery[] = await Promise.all(
queries.map(
async (currentQuery) => {
const hasExpressionsInQuery =
this.hasExpressionsInQuery(currentQuery);
const queryNormalizationResult: Array<{
normalizedQuery: NormalizedQuery,
hasExpressionsInQuery: boolean
}> = queries.map((currentQuery) => {
const hasExpressionsInQuery = this.hasExpressionsInQuery(currentQuery);

if (hasExpressionsInQuery) {
if (!memberExpressions) {
throw new Error('Expressions are not allowed in this context');
}
if (hasExpressionsInQuery) {
if (!memberExpressions) {
throw new Error('Expressions are not allowed in this context');
}

currentQuery = this.parseMemberExpressionsInQuery(currentQuery);
}
currentQuery = this.parseMemberExpressionsInQuery(currentQuery);
}

const normalizedQuery = normalizeQuery(currentQuery, persistent);
let evaluatedQuery = normalizedQuery;
return {
normalizedQuery: (normalizeQuery(currentQuery, persistent)),
hasExpressionsInQuery
};
});

let normalizedQueries: NormalizedQuery[] = await Promise.all(
queryNormalizationResult.map(
async ({ normalizedQuery, hasExpressionsInQuery }) => {
let evaluatedQuery: Query | NormalizedQuery = normalizedQuery;

if (hasExpressionsInQuery) {
// We need to parse/eval all member expressions early as applyRowLevelSecurity
// needs to access the full SQL query in order to evaluate rules
evaluatedQuery =
this.evalMemberExpressionsInQuery(normalizedQuery);
evaluatedQuery = this.evalMemberExpressionsInQuery(normalizedQuery);
}

// First apply cube/view level security policies
Expand Down Expand Up @@ -1257,7 +1264,7 @@ class ApiGateway {
}
}

return [queryType, normalizedQueries];
return [queryType, normalizedQueries, queryNormalizationResult.map((it) => remapToQueryAdapterFormat(it.normalizedQuery))];
}

public async sql({
Expand Down Expand Up @@ -1454,7 +1461,7 @@ class ApiGateway {
try {
await this.assertApiScope('data', context.securityContext);

const [queryType, normalizedQueries] = await this.getNormalizedQueries(query, context);
const [queryType, _, normalizedQueries] = await this.getNormalizedQueries(query, context, undefined, undefined);

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