@@ -1595,33 +1595,47 @@ export class PreAggregationPartitionRangeLoader {
15951595
15961596 public async loadPreAggregations ( ) : Promise < LoadPreAggregationResult > {
15971597 if ( this . preAggregation . partitionGranularity && ! this . preAggregation . expandedPartition ) {
1598- const { buildRange, partitionRanges } = await this . partitionRanges ( ) ;
1599- const partitionLoaders = partitionRanges . map ( range => new PreAggregationLoader (
1600- this . redisPrefix ,
1601- this . driverFactory ,
1602- this . logger ,
1603- this . queryCache ,
1604- this . preAggregations ,
1605- this . partitionPreAggregationDescription ( range , buildRange ) ,
1606- this . preAggregationsTablesToTempTables ,
1607- this . loadCache ,
1608- this . options ,
1609- ) ) ;
1610- const resolveResults = await Promise . all ( partitionLoaders . map ( async ( l , i ) => {
1611- const result = await l . loadPreAggregation ( false ) ;
1612- return result && {
1613- ...result ,
1614- partitionRange : partitionRanges [ i ]
1615- } ;
1616- } ) ) ;
1617- let loadResults = resolveResults . filter ( res => res !== null ) ;
1618- if ( this . options . externalRefresh && loadResults . length === 0 ) {
1598+ const loadPreAggregationsByPartitionRanges = async ( { buildRange, partitionRanges } : PartitionRanges ) => {
1599+ const partitionLoaders = partitionRanges . map ( range => new PreAggregationLoader (
1600+ this . redisPrefix ,
1601+ this . driverFactory ,
1602+ this . logger ,
1603+ this . queryCache ,
1604+ this . preAggregations ,
1605+ this . partitionPreAggregationDescription ( range , buildRange ) ,
1606+ this . preAggregationsTablesToTempTables ,
1607+ this . loadCache ,
1608+ this . options ,
1609+ ) ) ;
1610+ const resolveResults = await Promise . all ( partitionLoaders . map ( async ( l , i ) => {
1611+ const result = await l . loadPreAggregation ( false ) ;
1612+ return result && {
1613+ ...result ,
1614+ partitionRange : partitionRanges [ i ]
1615+ } ;
1616+ } ) ) ;
1617+ return { loadResults : resolveResults . filter ( res => res !== null ) , partitionLoaders } ;
1618+ } ;
1619+
1620+ // eslint-disable-next-line prefer-const
1621+ let loadResultAndLoaders = await loadPreAggregationsByPartitionRanges ( await this . partitionRanges ( ) ) ;
1622+ if ( this . options . externalRefresh && loadResultAndLoaders . loadResults . length === 0 ) {
1623+ loadResultAndLoaders = await loadPreAggregationsByPartitionRanges ( await this . partitionRanges ( true ) ) ;
1624+ // In case there're no partitions ready at matched time dimension intersection then no data can be retrieved.
1625+ // We need to provide any table so query can just execute successfully.
1626+ if ( loadResultAndLoaders . loadResults . length > 0 ) {
1627+ loadResultAndLoaders . loadResults = [ loadResultAndLoaders . loadResults [ loadResultAndLoaders . loadResults . length - 1 ] ] ;
1628+ }
1629+ }
1630+ if ( this . options . externalRefresh && loadResultAndLoaders . loadResults . length === 0 ) {
16191631 throw new Error (
16201632 // eslint-disable-next-line no-use-before-define
1621- PreAggregations . noPreAggregationPartitionsBuiltMessage ( partitionLoaders . map ( p => p . preAggregation ) )
1633+ PreAggregations . noPreAggregationPartitionsBuiltMessage ( loadResultAndLoaders . partitionLoaders . map ( p => p . preAggregation ) )
16221634 ) ;
16231635 }
16241636
1637+ let { loadResults } = loadResultAndLoaders ;
1638+
16251639 let lambdaTable : InlineTable ;
16261640 let emptyResult = false ;
16271641
@@ -1727,14 +1741,14 @@ export class PreAggregationPartitionRangeLoader {
17271741 }
17281742 }
17291743
1730- private async partitionRanges ( ) : Promise < PartitionRanges > {
1744+ private async partitionRanges ( ignoreMatchedDateRange ?: boolean ) : Promise < PartitionRanges > {
17311745 const buildRange = await this . loadBuildRange ( ) ;
17321746 if ( ! buildRange [ 0 ] || ! buildRange [ 1 ] ) {
17331747 return { buildRange, partitionRanges : [ ] } ;
17341748 }
17351749 let dateRange = PreAggregationPartitionRangeLoader . intersectDateRanges (
17361750 buildRange ,
1737- this . preAggregation . matchedTimeDimensionDateRange ,
1751+ ignoreMatchedDateRange ? undefined : this . preAggregation . matchedTimeDimensionDateRange ,
17381752 ) ;
17391753 if ( ! dateRange ) {
17401754 // If there's no date range intersection between query data range and pre-aggregation build range
0 commit comments