@@ -13,7 +13,9 @@ export class BaseTimeDimension extends BaseFilter {
1313
1414 public readonly isPredefined : boolean ;
1515
16- public readonly baseGranularity : string ;
16+ public readonly baseGranularity : string | undefined ;
17+
18+ public readonly granularitySql : Function | undefined ;
1719
1820 public readonly boundaryDateRange : any ;
1921
@@ -31,9 +33,14 @@ export class BaseTimeDimension extends BaseFilter {
3133 this . dateRange = timeDimension . dateRange ;
3234 this . granularity = timeDimension . granularity ;
3335 this . isPredefined = isPredefinedGranularity ( this . granularity ) ;
34- this . baseGranularity = this . query . cubeEvaluator
35- . byPath ( 'dimensions' , timeDimension . dimension )
36- . granularities ?. [ this . granularity ] ?. baseGranularity ;
36+ if ( ! this . isPredefined ) {
37+ const customGranularity = this . query . cubeEvaluator
38+ . byPath ( 'dimensions' , timeDimension . dimension )
39+ . granularities ?. [ this . granularity ] ;
40+
41+ this . baseGranularity = customGranularity ?. baseGranularity ;
42+ this . granularitySql = customGranularity ?. sql ;
43+ }
3744 this . boundaryDateRange = timeDimension . boundaryDateRange ;
3845 this . shiftInterval = timeDimension . shiftInterval ;
3946 }
@@ -95,12 +102,22 @@ export class BaseTimeDimension extends BaseFilter {
95102 if ( context . rollupGranularity === this . granularity ) {
96103 return super . dimensionSql ( ) ;
97104 }
98- return this . query . timeGroupedColumn ( granularity , this . query . dimensionSql ( this ) ) ;
105+ if ( this . isPredefined || ! this . granularity ) {
106+ return this . query . timeGroupedColumn ( granularity , this . query . dimensionSql ( this ) ) ;
107+ } else {
108+ return this . query . dimensionGranularitySql ( this ) ;
109+ }
99110 }
111+
100112 if ( context . ungrouped ) {
101113 return this . convertedToTz ( ) ;
102114 }
103- return this . query . timeGroupedColumn ( granularity , this . convertedToTz ( ) ) ;
115+
116+ if ( this . isPredefined ) {
117+ return this . query . timeGroupedColumn ( granularity , this . convertedToTz ( ) ) ;
118+ } else {
119+ return this . granularityConvertedToTz ( ) ;
120+ }
104121 }
105122
106123 public dimensionDefinition ( ) : DimensionDefinition | SegmentDefinition {
@@ -115,7 +132,11 @@ export class BaseTimeDimension extends BaseFilter {
115132 }
116133
117134 public convertedToTz ( ) {
118- return this . query . convertTz ( this . query . dimensionSql ( this ) ) ;
135+ return this . query . convertTz ( `${ this . query . dimensionSql ( this ) } ` ) ;
136+ }
137+
138+ public granularityConvertedToTz ( ) {
139+ return this . query . convertTz ( `(${ this . query . dimensionGranularitySql ( this ) } )` ) ;
119140 }
120141
121142 public filterToWhere ( ) {
0 commit comments