Skip to content

Commit 1a6451c

Browse files
authored
fix(cubesql): SQL push down can be incorrectly routed to Cube Store pre-aggregations (#7752)
1 parent 768325c commit 1a6451c

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,16 @@ class ApiGateway {
11721172
return [queryType, normalizedQueries];
11731173
}
11741174

1175-
public async sql({ query, context, res, memberToAlias, exportAnnotatedSql, memberExpressions, expressionParams }: QueryRequest) {
1175+
public async sql({
1176+
query,
1177+
context,
1178+
res,
1179+
memberToAlias,
1180+
exportAnnotatedSql,
1181+
memberExpressions,
1182+
expressionParams,
1183+
disableExternalPreAggregations
1184+
}: QueryRequest) {
11761185
const requestStarted = new Date();
11771186

11781187
try {
@@ -1188,7 +1197,7 @@ class ApiGateway {
11881197

11891198
const sqlQueries = await Promise.all<any>(
11901199
normalizedQueries.map(async (normalizedQuery) => (await this.getCompilerApi(context)).getSql(
1191-
this.coerceForSqlQuery({ ...normalizedQuery, memberToAlias, expressionParams }, context),
1200+
this.coerceForSqlQuery({ ...normalizedQuery, memberToAlias, expressionParams, disableExternalPreAggregations }, context),
11921201
{
11931202
includeDebugInfo: getEnv('devMode') || context.signedWithPlaygroundAuthSecret,
11941203
exportAnnotatedSql,
@@ -1708,7 +1717,10 @@ class ApiGateway {
17081717
metaConfigResult = this.filterVisibleItemsInMeta(context, metaConfigResult);
17091718

17101719
const sqlQueries = await this
1711-
.getSqlQueriesInternal(context, normalizedQueries);
1720+
.getSqlQueriesInternal(
1721+
context,
1722+
normalizedQueries.map(q => ({ ...q, disableExternalPreAggregations: request.sqlQuery }))
1723+
);
17121724

17131725
let results;
17141726

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export class SQLServer {
157157
expressionParams,
158158
exportAnnotatedSql: true,
159159
memberExpressions: true,
160+
disableExternalPreAggregations: true,
160161
queryType: 'multi',
161162
context,
162163
res: (message) => {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ type QueryRequest = BaseRequest & {
126126
expressionParams?: string[];
127127
exportAnnotatedSql?: boolean;
128128
memberExpressions?: boolean;
129+
disableExternalPreAggregations?: boolean;
129130
};
130131

131132
type SqlApiRequest = BaseRequest & {

packages/cubejs-schema-compiler/src/adapter/BaseQuery.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ export class BaseQuery {
202202
className: this.constructor.name,
203203
externalClassName: this.options.externalQueryClass && this.options.externalQueryClass.name,
204204
preAggregationQuery: this.options.preAggregationQuery,
205+
disableExternalPreAggregations: this.options.disableExternalPreAggregations,
205206
useOriginalSqlPreAggregationsInPreAggregation: this.options.useOriginalSqlPreAggregationsInPreAggregation,
206207
cubeLatticeCache: this.options.cubeLatticeCache, // TODO too heavy for key
207208
historyQueries: this.options.historyQueries, // TODO too heavy for key
@@ -467,6 +468,9 @@ export class BaseQuery {
467468
if (!this.options.preAggregationQuery && !this.ungrouped) {
468469
preAggForQuery =
469470
this.preAggregations.findPreAggregationForQuery();
471+
if (this.options.disableExternalPreAggregations && preAggForQuery.preAggregation.external) {
472+
preAggForQuery = undefined;
473+
}
470474
}
471475
if (preAggForQuery) {
472476
const {
@@ -537,7 +541,7 @@ export class BaseQuery {
537541
}
538542

539543
externalPreAggregationQuery() {
540-
if (!this.options.preAggregationQuery && this.externalQueryClass) {
544+
if (!this.options.preAggregationQuery && !this.options.disableExternalPreAggregations && this.externalQueryClass) {
541545
const preAggregationForQuery = this.preAggregations.findPreAggregationForQuery();
542546
if (preAggregationForQuery && preAggregationForQuery.preAggregation.external) {
543547
return true;
@@ -556,7 +560,7 @@ export class BaseQuery {
556560
* @returns {Array<string>}
557561
*/
558562
buildSqlAndParams(exportAnnotatedSql) {
559-
if (!this.options.preAggregationQuery && this.externalQueryClass) {
563+
if (!this.options.preAggregationQuery && !this.options.disableExternalPreAggregations && this.externalQueryClass) {
560564
if (this.externalPreAggregationQuery()) { // TODO performance
561565
return this.externalQuery().buildSqlAndParams(exportAnnotatedSql);
562566
}

0 commit comments

Comments
 (0)