@@ -122,6 +122,10 @@ class PreAggregationLoadCache {
122
122
return this . queryResults [ this . queryCache . queryRedisKey ( keyQuery ) ] ;
123
123
}
124
124
125
+ hasKeyQueryResult ( keyQuery ) {
126
+ return ! ! this . queryResults [ this . queryCache . queryRedisKey ( keyQuery ) ] ;
127
+ }
128
+
125
129
async getQueryStage ( stageQueryKey ) {
126
130
const queue = this . preAggregations . getQueue ( ) ;
127
131
if ( ! this . queryStageState ) {
@@ -164,6 +168,30 @@ class PreAggregationLoader {
164
168
}
165
169
166
170
async loadPreAggregation ( ) {
171
+ const notLoadedKey = ( this . preAggregation . invalidateKeyQueries || [ ] )
172
+ . find ( keyQuery => ! this . loadCache . hasKeyQueryResult ( keyQuery ) ) ;
173
+ if ( notLoadedKey && ! this . waitForRenew ) {
174
+ const structureVersion = version ( this . preAggregation . loadSql ) ;
175
+ const versionEntries = await this . loadCache . getVersionEntries ( this . preAggregation ) ;
176
+ const versionEntryByStructureVersion = versionEntries . find (
177
+ v => v . table_name === this . preAggregation . tableName && v . structure_version === structureVersion
178
+ ) ;
179
+ if ( versionEntryByStructureVersion ) {
180
+ this . loadPreAggregationWithKeys ( ) . catch ( e => {
181
+ if ( ! ( e instanceof ContinueWaitError ) ) {
182
+ this . logger ( 'Error loading pre-aggregation' , { error : ( e . stack || e ) , preAggregation : this . preAggregation } ) ;
183
+ }
184
+ } ) ;
185
+ return this . targetTableName ( versionEntryByStructureVersion ) ;
186
+ } else {
187
+ return this . loadPreAggregationWithKeys ( ) ;
188
+ }
189
+ } else {
190
+ return this . loadPreAggregationWithKeys ( ) ;
191
+ }
192
+ }
193
+
194
+ async loadPreAggregationWithKeys ( ) {
167
195
const invalidationKeys = await Promise . all (
168
196
( this . preAggregation . invalidateKeyQueries || [ ] )
169
197
. map ( keyQuery => this . loadCache . keyQueryResult ( keyQuery ) )
@@ -182,6 +210,7 @@ class PreAggregationLoader {
182
210
return this . targetTableName ( versionEntryByContentVersion ) ;
183
211
}
184
212
213
+ // TODO this check can be redundant due to structure version is already checked in loadPreAggregation()
185
214
if (
186
215
! this . waitForRenew &&
187
216
await this . loadCache . getQueryStage ( PreAggregations . preAggregationQueryCacheKey ( this . preAggregation ) )
0 commit comments