From ca328ee6015dc5499fe203892489b3d206d3abef Mon Sep 17 00:00:00 2001 From: Dmitry Patsura Date: Thu, 27 Nov 2025 14:51:01 +0100 Subject: [PATCH 1/2] fix(query-orchestrator): Table used cache should not be used for unions --- .../src/orchestrator/PreAggregationPartitionRangeLoader.ts | 2 ++ .../src/orchestrator/PreAggregations.ts | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationPartitionRangeLoader.ts b/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationPartitionRangeLoader.ts index c81625a1e102f..99ce8033d72da 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 || emptyResult, }; } 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( From 433f2736deefee90409cba9a7932990f0db8871c Mon Sep 17 00:00:00 2001 From: Dmitry Patsura Date: Thu, 27 Nov 2025 15:42:38 +0100 Subject: [PATCH 2/2] chore :fix --- .../src/orchestrator/PreAggregationPartitionRangeLoader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationPartitionRangeLoader.ts b/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationPartitionRangeLoader.ts index 99ce8033d72da..c7e7cd0b360fb 100644 --- a/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationPartitionRangeLoader.ts +++ b/packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationPartitionRangeLoader.ts @@ -303,7 +303,7 @@ export class PreAggregationPartitionRangeLoader { buildRangeEnd: !emptyResult && loadResults.length && loadResults[loadResults.length - 1].buildRangeEnd, lambdaTable, rollupLambdaId: this.preAggregation.rollupLambdaId, - isMultiTableUnion: allTableTargetNames.length > 1 || emptyResult, + isMultiTableUnion: allTableTargetNames.length > 1, }; } else { return new PreAggregationLoader(