diff --git a/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationPartitionRangeLoader.ts b/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationPartitionRangeLoader.ts index c81625a1e102f..c7e7cd0b360fb 100644 --- a/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationPartitionRangeLoader.ts +++ b/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationPartitionRangeLoader.ts @@ -295,6 +295,7 @@ export class PreAggregationPartitionRangeLoader { const unionTargetTableName = allTableTargetNames .map(targetTableName => `SELECT * FROM ${targetTableName}${emptyResult ? ' WHERE 1 = 0' : ''}`) .join(' UNION ALL '); + return { targetTableName: allTableTargetNames.length === 1 && !emptyResult ? allTableTargetNames[0] : `(${unionTargetTableName})`, refreshKeyValues: loadResults.map(t => t.refreshKeyValues), @@ -302,6 +303,7 @@ export class PreAggregationPartitionRangeLoader { buildRangeEnd: !emptyResult && loadResults.length && loadResults[loadResults.length - 1].buildRangeEnd, lambdaTable, rollupLambdaId: this.preAggregation.rollupLambdaId, + isMultiTableUnion: allTableTargetNames.length > 1, }; } else { return new PreAggregationLoader( diff --git a/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregations.ts b/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregations.ts index 14c1150bb2c69..43604fc040234 100644 --- a/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregations.ts +++ b/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregations.ts @@ -142,6 +142,7 @@ export type LoadPreAggregationResult = { queryKey?: any[]; rollupLambdaId?: string; partitionRange?: QueryDateRange; + isMultiTableUnion?: boolean; }; export type PreAggregationTableToTempTable = [string, LoadPreAggregationResult]; @@ -515,7 +516,9 @@ export class PreAggregations { ...loadResult, type: p.type, }; - await this.addTableUsed(usedPreAggregation.targetTableName); + if (!usedPreAggregation.isMultiTableUnion) { + await this.addTableUsed(usedPreAggregation.targetTableName); + } if (i === preAggregations.length - 1 && queryBody.values) { queryParamsReplacement = await loader.replaceQueryBuildRangeParams(