@@ -94,19 +94,24 @@ describe('PreAggregations', function test() {
94
94
measureReferences: [checkinsTotal],
95
95
segmentReferences: [google],
96
96
timeDimensionReference: createdAt,
97
- granularity: 'day ',
97
+ granularity: 'week ',
98
98
},
99
99
approx: {
100
100
type: 'rollup',
101
101
measureReferences: [countDistinctApprox],
102
102
timeDimensionReference: createdAt,
103
103
granularity: 'day'
104
104
},
105
- ratio: {
105
+ multiStage: {
106
+ useOriginalSqlPreAggregations: true,
106
107
type: 'rollup',
107
- measureReferences: [checkinsTotal, uniqueSourceCount ],
108
+ measureReferences: [checkinsTotal],
108
109
timeDimensionReference: createdAt,
109
- granularity: 'day'
110
+ granularity: 'month',
111
+ partitionGranularity: 'day',
112
+ refreshKey: {
113
+ sql: \`SELECT CASE WHEN \${FILTER_PARAMS.visitors.createdAt.filter((from, to) => \`\${to}::timestamp > now()\`)} THEN now() END\`
114
+ }
110
115
},
111
116
partitioned: {
112
117
type: 'rollup',
@@ -116,16 +121,11 @@ describe('PreAggregations', function test() {
116
121
granularity: 'day',
117
122
partitionGranularity: 'month'
118
123
},
119
- multiStage: {
120
- useOriginalSqlPreAggregations: true,
124
+ ratio: {
121
125
type: 'rollup',
122
- measureReferences: [checkinsTotal],
126
+ measureReferences: [checkinsTotal, uniqueSourceCount ],
123
127
timeDimensionReference: createdAt,
124
- granularity: 'month',
125
- partitionGranularity: 'day',
126
- refreshKey: {
127
- sql: \`SELECT CASE WHEN \${FILTER_PARAMS.visitors.createdAt.filter((from, to) => \`\${to}::timestamp > now()\`)} THEN now() END\`
128
- }
128
+ granularity: 'day'
129
129
}
130
130
}
131
131
})
@@ -518,7 +518,7 @@ describe('PreAggregations', function test() {
518
518
preAggregationsSchema : '' ,
519
519
timeDimensions : [ {
520
520
dimension : 'visitors.createdAt' ,
521
- granularity : 'date ' ,
521
+ granularity : 'week ' ,
522
522
dateRange : [ '2016-12-30' , '2017-01-05' ]
523
523
} ] ,
524
524
order : [ {
@@ -542,7 +542,7 @@ describe('PreAggregations', function test() {
542
542
res . should . be . deepEqual (
543
543
[
544
544
{
545
- "visitors__created_at_date " : "2017-01-05T00 :00:00.000Z" ,
545
+ "visitors__created_at_week " : "2017-01-02T00 :00:00.000Z" ,
546
546
"visitors__checkins_total" : "1"
547
547
}
548
548
]
@@ -590,3 +590,157 @@ describe('PreAggregations', function test() {
590
590
} ) ;
591
591
} ) ;
592
592
} ) ;
593
+
594
+
595
+ describe ( 'PreAggregations in time hierarchy' , function test ( ) {
596
+ this . timeout ( 20000 ) ;
597
+
598
+ after ( async ( ) => {
599
+ await dbRunner . tearDown ( ) ;
600
+ } ) ;
601
+
602
+ const { compiler, joinGraph, cubeEvaluator } = prepareCompiler ( `
603
+ cube(\`visitors\`, {
604
+ sql: \`
605
+ select * from visitors
606
+ \`,
607
+
608
+ measures: {
609
+ count: {
610
+ type: 'count'
611
+ }
612
+ },
613
+
614
+ dimensions: {
615
+ createdAt: {
616
+ type: 'time',
617
+ sql: 'created_at'
618
+ },
619
+ },
620
+
621
+ preAggregations: {
622
+ month: {
623
+ type: 'rollup',
624
+ measureReferences: [count],
625
+ timeDimensionReference: createdAt,
626
+ granularity: 'month',
627
+ },
628
+ day: {
629
+ type: 'rollup',
630
+ measureReferences: [count],
631
+ timeDimensionReference: createdAt,
632
+ granularity: 'day',
633
+ },
634
+ }
635
+ })
636
+ ` ) ;
637
+
638
+ function replaceTableName ( query , preAggregation , suffix ) {
639
+ const [ toReplace , params ] = query ;
640
+ console . log ( toReplace ) ;
641
+ preAggregation = Array . isArray ( preAggregation ) ? preAggregation : [ preAggregation ] ;
642
+ return [
643
+ preAggregation . reduce ( ( replacedQuery , desc ) =>
644
+ replacedQuery . replace ( new RegExp ( desc . tableName , 'g' ) , desc . tableName + '_' + suffix ) , toReplace
645
+ ) ,
646
+ params
647
+ ] ;
648
+ }
649
+
650
+ function tempTablePreAggregations ( preAggregationsDescriptions ) {
651
+ return R . unnest ( preAggregationsDescriptions . map ( desc =>
652
+ desc . invalidateKeyQueries . concat ( [
653
+ [ desc . loadSql [ 0 ] . replace ( 'CREATE TABLE' , 'CREATE TEMP TABLE' ) , desc . loadSql [ 1 ] ]
654
+ ] )
655
+ ) ) ;
656
+ }
657
+
658
+ it ( 'query on year match to pre-agg on month' , ( ) => {
659
+ return compiler . compile ( ) . then ( ( ) => {
660
+ const query = new PostgresQuery ( { joinGraph, cubeEvaluator, compiler } , {
661
+ measures : [
662
+ 'visitors.count'
663
+ ] ,
664
+ dimensions : [ ] ,
665
+ timezone : 'America/Los_Angeles' ,
666
+ timeDimensions : [ {
667
+ dimension : 'visitors.createdAt' ,
668
+ granularity : 'year' ,
669
+ dateRange : [ '2016-12-30' , '2018-12-30' ]
670
+ } ] ,
671
+ preAggregationsSchema : '' ,
672
+ order : [ ] ,
673
+ } ) ;
674
+
675
+ const queryAndParams = query . buildSqlAndParams ( ) ;
676
+
677
+ query . preAggregations . preAggregationForQuery . preAggregation . granularity . should . be . equal ( 'month' ) ;
678
+
679
+ console . log ( queryAndParams ) ;
680
+ const preAggregationsDescription = query . preAggregations . preAggregationsDescription ( ) ;
681
+ console . log ( preAggregationsDescription ) ;
682
+
683
+ const queries = tempTablePreAggregations ( preAggregationsDescription ) ;
684
+
685
+ console . log ( JSON . stringify ( queries . concat ( queryAndParams ) ) ) ;
686
+
687
+ return dbRunner . testQueries (
688
+ queries . concat ( [ queryAndParams ] ) . map ( q => replaceTableName ( q , preAggregationsDescription , 1 ) )
689
+ ) . then ( res => {
690
+ console . log ( JSON . stringify ( res ) ) ;
691
+ res . should . be . deepEqual (
692
+ [
693
+ {
694
+ "visitors__count" : "5" ,
695
+ "visitors__created_at_year" : "2017-01-01T00:00:00.000Z"
696
+ } ,
697
+ ]
698
+ ) ;
699
+ } ) ;
700
+ } ) ;
701
+ } ) ;
702
+ it ( 'query on week match to pre-agg on day' , ( ) => {
703
+ return compiler . compile ( ) . then ( ( ) => {
704
+ const query = new PostgresQuery ( { joinGraph, cubeEvaluator, compiler } , {
705
+ measures : [
706
+ 'visitors.count'
707
+ ] ,
708
+ dimensions : [ ] ,
709
+ timezone : 'America/Los_Angeles' ,
710
+ timeDimensions : [ {
711
+ dimension : 'visitors.createdAt' ,
712
+ granularity : 'week' ,
713
+ dateRange : [ '2017-01-02' , '2019-02-08' ]
714
+ } ] ,
715
+ preAggregationsSchema : '' ,
716
+ order : [ ] ,
717
+ } ) ;
718
+
719
+ const queryAndParams = query . buildSqlAndParams ( ) ;
720
+
721
+ query . preAggregations . preAggregationForQuery . preAggregation . granularity . should . be . equal ( 'day' ) ;
722
+
723
+ console . log ( queryAndParams ) ;
724
+ const preAggregationsDescription = query . preAggregations . preAggregationsDescription ( ) ;
725
+ console . log ( preAggregationsDescription ) ;
726
+
727
+ const queries = tempTablePreAggregations ( preAggregationsDescription ) ;
728
+
729
+ console . log ( JSON . stringify ( queries . concat ( queryAndParams ) ) ) ;
730
+
731
+ return dbRunner . testQueries (
732
+ queries . concat ( [ queryAndParams ] ) . map ( q => replaceTableName ( q , preAggregationsDescription , 1 ) )
733
+ ) . then ( res => {
734
+ console . log ( JSON . stringify ( res ) ) ;
735
+ res . should . be . deepEqual (
736
+ [
737
+ {
738
+ "visitors__count" : "5" ,
739
+ "visitors__created_at_week" : "2017-01-02T00:00:00.000Z"
740
+ } ,
741
+ ]
742
+ ) ;
743
+ } ) ;
744
+ } ) ;
745
+ } ) ;
746
+ } ) ;
0 commit comments