@@ -1320,33 +1320,44 @@ class BaseQuery {
1320
1320
) ;
1321
1321
}
1322
1322
1323
- cacheKeyQueries ( ) { // TODO collect sub queries
1324
- const preAggregationForQuery = this . preAggregations . findPreAggregationForQuery ( ) ;
1325
- if ( preAggregationForQuery ) {
1326
- return {
1327
- renewalThreshold : this . renewalThreshold ( ! ! preAggregationForQuery . refreshKey ) ,
1328
- queries : this . preAggregationInvalidateKeyQueries ( preAggregationForQuery . cube , preAggregationForQuery )
1329
- } ;
1323
+ cacheKeyQueries ( transformFn ) { // TODO collect sub queries
1324
+ if ( ! this . safeEvaluateSymbolContext ( ) . preAggregationQuery ) {
1325
+ const preAggregationForQuery = this . preAggregations . findPreAggregationForQuery ( ) ;
1326
+ if ( preAggregationForQuery ) {
1327
+ return {
1328
+ renewalThreshold : this . renewalThreshold ( ! ! preAggregationForQuery . refreshKey ) ,
1329
+ queries : this . preAggregationInvalidateKeyQueries ( preAggregationForQuery . cube , preAggregationForQuery )
1330
+ } ;
1331
+ }
1330
1332
}
1331
1333
let refreshKeyAllSetManually = true ;
1332
- const queries = this . collectCubeNames ( )
1333
- . map ( cube => {
1334
- const cubeFromPath = this . cubeEvaluator . cubeFromPath ( cube ) ;
1335
- if ( cubeFromPath . refreshKey ) {
1336
- return this . evaluateSql ( cube , cubeFromPath . refreshKey . sql ) ;
1337
- }
1338
- refreshKeyAllSetManually = false ;
1339
- const timeDimensions = this . cubeEvaluator . timeDimensionPathsForCube ( cube ) ;
1340
- if ( timeDimensions . length ) {
1341
- const dimension = timeDimensions . filter ( f => f . toLowerCase ( ) . indexOf ( 'update' ) !== - 1 ) [ 0 ] || timeDimensions [ 0 ] ;
1342
- const foundMainTimeDimension = this . newTimeDimension ( { dimension } ) ;
1343
- const aggSelect = this . aggSelectForDimension ( cube , foundMainTimeDimension , 'max' ) ;
1344
- if ( aggSelect ) {
1345
- return aggSelect ;
1346
- }
1334
+ const refreshKeyQueryByCube = cube => {
1335
+ const cubeFromPath = this . cubeEvaluator . cubeFromPath ( cube ) ;
1336
+ if ( cubeFromPath . refreshKey && cubeFromPath . refreshKey . sql ) {
1337
+ return this . evaluateSql ( cube , cubeFromPath . refreshKey . sql ) ;
1338
+ }
1339
+ refreshKeyAllSetManually = false ;
1340
+ const timeDimensions =
1341
+ ! ( cubeFromPath . refreshKey && cubeFromPath . refreshKey . immutable ) ?
1342
+ this . cubeEvaluator . timeDimensionPathsForCube ( cube ) :
1343
+ [ ] ;
1344
+ if ( timeDimensions . length ) {
1345
+ const dimension = timeDimensions . filter ( f => f . toLowerCase ( ) . indexOf ( 'update' ) !== - 1 ) [ 0 ] || timeDimensions [ 0 ] ;
1346
+ const foundMainTimeDimension = this . newTimeDimension ( { dimension } ) ;
1347
+ const aggSelect = this . aggSelectForDimension ( cube , foundMainTimeDimension , 'max' ) ;
1348
+ if ( aggSelect ) {
1349
+ return aggSelect ;
1347
1350
}
1348
- return `select count(*) from ${ this . cubeSql ( cube ) } ${ this . asSyntaxTable } ${ this . cubeAlias ( cube ) } ` ;
1349
- } ) . map ( paramAnnotatedSql => this . paramAllocator . buildSqlAndParams ( paramAnnotatedSql ) ) ;
1351
+ }
1352
+ return this . evaluateSymbolSqlWithContext (
1353
+ ( ) => `select count(*) from ${ this . cubeSql ( cube ) } ${ this . asSyntaxTable } ${ this . cubeAlias ( cube ) } ` ,
1354
+ { preAggregationQuery : true }
1355
+ ) ;
1356
+ } ;
1357
+ const queries = this . collectCubeNames ( )
1358
+ . map ( cube => [ cube , refreshKeyQueryByCube ( cube ) ] )
1359
+ . map ( ( [ cube , sql ] ) => ( transformFn ? transformFn ( sql , cube ) : sql ) )
1360
+ . map ( paramAnnotatedSql => this . paramAllocator . buildSqlAndParams ( paramAnnotatedSql ) ) ;
1350
1361
return {
1351
1362
queries,
1352
1363
renewalThreshold : this . renewalThreshold ( refreshKeyAllSetManually )
@@ -1423,14 +1434,46 @@ class BaseQuery {
1423
1434
}
1424
1435
1425
1436
preAggregationInvalidateKeyQueries ( cube , preAggregation ) {
1437
+ const preAggregationQueryForSql = this . preAggregationQueryForSqlEvaluation ( cube , preAggregation ) ;
1426
1438
if ( preAggregation . refreshKey ) {
1427
- return [ this . paramAllocator . buildSqlAndParams (
1428
- this . preAggregationQueryForSqlEvaluation ( cube , preAggregation ) . evaluateSql ( cube , preAggregation . refreshKey . sql )
1429
- ) ] ;
1439
+ if ( preAggregation . refreshKey . sql ) {
1440
+ return [ this . paramAllocator . buildSqlAndParams (
1441
+ preAggregationQueryForSql . evaluateSql ( cube , preAggregation . refreshKey . sql )
1442
+ ) ] ;
1443
+ }
1444
+ }
1445
+ if ( preAggregation . partitionGranularity ) {
1446
+ const cubeFromPath = this . cubeEvaluator . cubeFromPath ( cube ) ;
1447
+ return preAggregationQueryForSql . evaluateSymbolSqlWithContext (
1448
+ ( ) => preAggregationQueryForSql . cacheKeyQueries (
1449
+ ( originalRefreshKey , refreshKeyCube ) => {
1450
+ if ( cubeFromPath . refreshKey && cubeFromPath . refreshKey . immutable ) {
1451
+ return preAggregationQueryForSql . evaluateSql (
1452
+ cube ,
1453
+ ( FILTER_PARAMS ) => `SELECT ${ preAggregationQueryForSql . caseWhenStatement ( [ {
1454
+ sql : FILTER_PARAMS [
1455
+ preAggregationQueryForSql . timeDimensions [ 0 ] . path ( ) [ 0 ]
1456
+ ] [
1457
+ preAggregationQueryForSql . timeDimensions [ 0 ] . path ( ) [ 1 ]
1458
+ ] . filter ( ( from , to ) => `${ preAggregationQueryForSql . nowTimestampSql ( ) } < ${ this . timeStampCast ( to ) } ` ) ,
1459
+ label : `(${ originalRefreshKey } )`
1460
+ } ] ) } `
1461
+ ) ;
1462
+ } else {
1463
+ // TODO handle WHERE while generating originalRefreshKey
1464
+ return refreshKeyCube === preAggregationQueryForSql . timeDimensions [ 0 ] . path ( ) [ 0 ] ?
1465
+ `${ originalRefreshKey } WHERE ${ preAggregationQueryForSql . timeDimensions [ 0 ] . filterToWhere ( ) } ` :
1466
+ originalRefreshKey ;
1467
+ }
1468
+ }
1469
+ ) ,
1470
+ { preAggregationQuery : true }
1471
+ ) . queries ;
1430
1472
}
1431
- return [ this . paramAllocator . buildSqlAndParams (
1432
- `SELECT ${ this . timeGroupedColumn ( 'hour' , this . convertTz ( this . nowTimestampSql ( ) ) ) } as current_hour`
1433
- ) ] ;
1473
+ return preAggregationQueryForSql . evaluateSymbolSqlWithContext (
1474
+ ( ) => preAggregationQueryForSql . cacheKeyQueries ( ) ,
1475
+ { preAggregationQuery : true }
1476
+ ) . queries ;
1434
1477
}
1435
1478
1436
1479
preAggregationStartEndQueries ( cube , preAggregation ) {
0 commit comments