@@ -286,11 +286,9 @@ export class BaseQuery {
286286 return dimension ;
287287 } ) . filter ( R . identity ) . map ( this . newTimeDimension . bind ( this ) ) ;
288288 this . allFilters = this . timeDimensions . concat ( this . segments ) . concat ( this . filters ) ;
289+ this . useNativeSqlPlanner = this . options . useNativeSqlPlanner ?? getEnv ( 'nativeSqlPlanner' ) ;
290+ this . prebuildJoin ( ) ;
289291
290- if ( ! getEnv ( 'nativeSqlPlanner' ) ) {
291- // Tesseract doesn't require join to be prebuilt and there's a case where single join can't be built for multi-fact query
292- this . join = this . joinGraph . buildJoin ( this . allJoinHints ) ;
293- }
294292 this . cubeAliasPrefix = this . options . cubeAliasPrefix ;
295293 this . preAggregationsSchemaOption = this . options . preAggregationsSchema ?? DEFAULT_PREAGGREGATIONS_SCHEMA ;
296294 this . externalQueryClass = this . options . externalQueryClass ;
@@ -307,6 +305,15 @@ export class BaseQuery {
307305 this . initUngrouped ( ) ;
308306 }
309307
308+ prebuildJoin ( ) {
309+ /* if (!this.useNativeSqlPlanner) { We still need this join for the follback to preaggregation to work properly. This condition should be returned after the tesseract starts working with pre-aggregations
310+ // Tesseract doesn't require join to be prebuilt and there's a case where single join can't be built for multi-fact query
311+ this.join = this.joinGraph.buildJoin(this.allJoinHints);
312+ } */
313+
314+ this . join = this . joinGraph . buildJoin ( this . allJoinHints ) ;
315+ }
316+
310317 cacheValue ( key , fn , { contextPropNames, inputProps, cache } = { } ) {
311318 const currentContext = this . safeEvaluateSymbolContext ( ) ;
312319 if ( contextPropNames ) {
@@ -383,8 +390,7 @@ export class BaseQuery {
383390 initUngrouped ( ) {
384391 this . ungrouped = this . options . ungrouped ;
385392 if ( this . ungrouped ) {
386- // this.join is not defined for Tesseract
387- if ( ! this . options . allowUngroupedWithoutPrimaryKey && ! getEnv ( 'nativeSqlPlanner' ) ) {
393+ if ( ! this . options . allowUngroupedWithoutPrimaryKey ) {
388394 const cubes = R . uniq ( [ this . join . root ] . concat ( this . join . joins . map ( j => j . originalTo ) ) ) ;
389395 const primaryKeyNames = cubes . flatMap ( c => this . primaryKeyNames ( c ) ) ;
390396 const missingPrimaryKeys = primaryKeyNames . filter ( key => ! this . dimensions . find ( d => d . dimension === key ) ) ;
@@ -611,33 +617,61 @@ export class BaseQuery {
611617 return false ;
612618 }
613619
620+ newQueryNotUseNative ( ) {
621+ const QueryClass = this . constructor ;
622+ return new QueryClass ( this . compilers , { ...this . options , useNativeSqlPlanner : false } ) ;
623+ }
624+
614625 /**
615626 * Returns a pair of SQL query string and parameter values for the query.
616627 * @param {boolean } [exportAnnotatedSql] - returns annotated sql with not rendered params if true
617628 * @returns {[string, Array<unknown>] }
618629 */
619630 buildSqlAndParams ( exportAnnotatedSql ) {
620- if ( getEnv ( 'nativeSqlPlanner' ) ) {
621- return this . buildSqlAndParamsRust ( exportAnnotatedSql ) ;
622- } else {
623- if ( ! this . options . preAggregationQuery && ! this . options . disableExternalPreAggregations && this . externalQueryClass ) {
624- if ( this . externalPreAggregationQuery ( ) ) { // TODO performance
625- return this . externalQuery ( ) . buildSqlAndParams ( exportAnnotatedSql ) ;
631+ if ( ! this . options . preAggregationQuery && ! this . options . disableExternalPreAggregations && this . externalQueryClass ) {
632+ if ( this . externalPreAggregationQuery ( ) ) { // TODO performance
633+ return this . externalQuery ( ) . buildSqlAndParams ( exportAnnotatedSql ) ;
634+ }
635+ }
636+
637+ if ( this . useNativeSqlPlanner ) {
638+ let isRelatedToPreAggregation = false ;
639+ if ( this . options . preAggregationQuery ) {
640+ isRelatedToPreAggregation = true ;
641+ } else if ( ! this . options . disableExternalPreAggregations && this . externalQueryClass ) {
642+ if ( this . externalPreAggregationQuery ( ) ) {
643+ isRelatedToPreAggregation = true ;
644+ }
645+ } else {
646+ let preAggForQuery =
647+ this . preAggregations . findPreAggregationForQuery ( ) ;
648+ if ( this . options . disableExternalPreAggregations && preAggForQuery && preAggForQuery . preAggregation . external ) {
649+ preAggForQuery = undefined ;
650+ }
651+ if ( preAggForQuery ) {
652+ isRelatedToPreAggregation = true ;
626653 }
627654 }
628- return this . compilers . compiler . withQuery (
629- this ,
630- ( ) => this . cacheValue (
631- [ 'buildSqlAndParams' , exportAnnotatedSql ] ,
632- ( ) => this . paramAllocator . buildSqlAndParams (
633- this . buildParamAnnotatedSql ( ) ,
634- exportAnnotatedSql ,
635- this . shouldReuseParams
636- ) ,
637- { cache : this . queryCache }
638- )
639- ) ;
655+
656+ if ( isRelatedToPreAggregation ) {
657+ return this . newQueryNotUseNative ( ) . buildSqlAndParams ( exportAnnotatedSql ) ;
658+ }
659+
660+ return this . buildSqlAndParamsRust ( exportAnnotatedSql ) ;
640661 }
662+
663+ return this . compilers . compiler . withQuery (
664+ this ,
665+ ( ) => this . cacheValue (
666+ [ 'buildSqlAndParams' , exportAnnotatedSql ] ,
667+ ( ) => this . paramAllocator . buildSqlAndParams (
668+ this . buildParamAnnotatedSql ( ) ,
669+ exportAnnotatedSql ,
670+ this . shouldReuseParams
671+ ) ,
672+ { cache : this . queryCache }
673+ )
674+ ) ;
641675 }
642676
643677 buildSqlAndParamsRust ( exportAnnotatedSql ) {
@@ -3052,6 +3086,7 @@ export class BaseQuery {
30523086 }
30533087
30543088 newSubQueryForCube ( cube , options ) {
3089+ options = { ...options , useNativeSqlPlanner : false } ; // We not use tesseract for pre-aggregations generation yet
30553090 if ( this . options . queryFactory ) {
30563091 // When dealing with rollup joins, it's crucial to use the correct parameter allocator for the specific cube in use.
30573092 // By default, we'll use BaseQuery, but it's important to note that different databases (Oracle, PostgreSQL, MySQL, Druid, etc.)
@@ -3075,6 +3110,7 @@ export class BaseQuery {
30753110 historyQueries : this . options . historyQueries ,
30763111 externalQueryClass : this . options . externalQueryClass ,
30773112 queryFactory : this . options . queryFactory ,
3113+ useNativeSqlPlanner : this . options . useNativeSqlPlanner ,
30783114 ...options ,
30793115 } ;
30803116 }
0 commit comments