@@ -278,11 +278,9 @@ export class BaseQuery {
278278 return dimension ;
279279 } ) . filter ( R . identity ) . map ( this . newTimeDimension . bind ( this ) ) ;
280280 this . allFilters = this . timeDimensions . concat ( this . segments ) . concat ( this . filters ) ;
281+ this . useNativeSqlPlanner = this . options . useNativeSqlPlanner ?? getEnv ( 'nativeSqlPlanner' ) ;
282+ this . prebuildJoin ( ) ;
281283
282- if ( ! getEnv ( 'nativeSqlPlanner' ) ) {
283- // Tesseract doesn't require join to be prebuilt and there's a case where single join can't be built for multi-fact query
284- this . join = this . joinGraph . buildJoin ( this . allJoinHints ) ;
285- }
286284 this . cubeAliasPrefix = this . options . cubeAliasPrefix ;
287285 this . preAggregationsSchemaOption = this . options . preAggregationsSchema ?? DEFAULT_PREAGGREGATIONS_SCHEMA ;
288286 this . externalQueryClass = this . options . externalQueryClass ;
@@ -299,6 +297,15 @@ export class BaseQuery {
299297 this . initUngrouped ( ) ;
300298 }
301299
300+ prebuildJoin ( ) {
301+ /* 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
302+ // Tesseract doesn't require join to be prebuilt and there's a case where single join can't be built for multi-fact query
303+ this.join = this.joinGraph.buildJoin(this.allJoinHints);
304+ } */
305+
306+ this . join = this . joinGraph . buildJoin ( this . allJoinHints ) ;
307+ }
308+
302309 cacheValue ( key , fn , { contextPropNames, inputProps, cache } = { } ) {
303310 const currentContext = this . safeEvaluateSymbolContext ( ) ;
304311 if ( contextPropNames ) {
@@ -375,8 +382,7 @@ export class BaseQuery {
375382 initUngrouped ( ) {
376383 this . ungrouped = this . options . ungrouped ;
377384 if ( this . ungrouped ) {
378- // this.join is not defined for Tesseract
379- if ( ! this . options . allowUngroupedWithoutPrimaryKey && ! getEnv ( 'nativeSqlPlanner' ) ) {
385+ if ( ! this . options . allowUngroupedWithoutPrimaryKey ) {
380386 const cubes = R . uniq ( [ this . join . root ] . concat ( this . join . joins . map ( j => j . originalTo ) ) ) ;
381387 const primaryKeyNames = cubes . flatMap ( c => this . primaryKeyNames ( c ) ) ;
382388 const missingPrimaryKeys = primaryKeyNames . filter ( key => ! this . dimensions . find ( d => d . dimension === key ) ) ;
@@ -603,33 +609,61 @@ export class BaseQuery {
603609 return false ;
604610 }
605611
612+ newQueryNotUseNative ( ) {
613+ const QueryClass = this . constructor ;
614+ return new QueryClass ( this . compilers , { ...this . options , useNativeSqlPlanner : false } ) ;
615+ }
616+
606617 /**
607618 * Returns a pair of SQL query string and parameter values for the query.
608619 * @param {boolean } [exportAnnotatedSql] - returns annotated sql with not rendered params if true
609620 * @returns {[string, Array<unknown>] }
610621 */
611622 buildSqlAndParams ( exportAnnotatedSql ) {
612- if ( getEnv ( 'nativeSqlPlanner' ) ) {
613- return this . buildSqlAndParamsRust ( exportAnnotatedSql ) ;
614- } else {
615- if ( ! this . options . preAggregationQuery && ! this . options . disableExternalPreAggregations && this . externalQueryClass ) {
616- if ( this . externalPreAggregationQuery ( ) ) { // TODO performance
617- return this . externalQuery ( ) . buildSqlAndParams ( exportAnnotatedSql ) ;
623+ if ( ! this . options . preAggregationQuery && ! this . options . disableExternalPreAggregations && this . externalQueryClass ) {
624+ if ( this . externalPreAggregationQuery ( ) ) { // TODO performance
625+ return this . externalQuery ( ) . buildSqlAndParams ( exportAnnotatedSql ) ;
626+ }
627+ }
628+
629+ if ( this . useNativeSqlPlanner ) {
630+ let isRelatedToPreAggregation = false ;
631+ if ( this . options . preAggregationQuery ) {
632+ isRelatedToPreAggregation = true ;
633+ } else if ( ! this . options . disableExternalPreAggregations && this . externalQueryClass ) {
634+ if ( this . externalPreAggregationQuery ( ) ) {
635+ isRelatedToPreAggregation = true ;
636+ }
637+ } else {
638+ let preAggForQuery =
639+ this . preAggregations . findPreAggregationForQuery ( ) ;
640+ if ( this . options . disableExternalPreAggregations && preAggForQuery && preAggForQuery . preAggregation . external ) {
641+ preAggForQuery = undefined ;
642+ }
643+ if ( preAggForQuery ) {
644+ isRelatedToPreAggregation = true ;
618645 }
619646 }
620- return this . compilers . compiler . withQuery (
621- this ,
622- ( ) => this . cacheValue (
623- [ 'buildSqlAndParams' , exportAnnotatedSql ] ,
624- ( ) => this . paramAllocator . buildSqlAndParams (
625- this . buildParamAnnotatedSql ( ) ,
626- exportAnnotatedSql ,
627- this . shouldReuseParams
628- ) ,
629- { cache : this . queryCache }
630- )
631- ) ;
647+
648+ if ( isRelatedToPreAggregation ) {
649+ return this . newQueryNotUseNative ( ) . buildSqlAndParams ( exportAnnotatedSql ) ;
650+ }
651+
652+ return this . buildSqlAndParamsRust ( exportAnnotatedSql ) ;
632653 }
654+
655+ return this . compilers . compiler . withQuery (
656+ this ,
657+ ( ) => this . cacheValue (
658+ [ 'buildSqlAndParams' , exportAnnotatedSql ] ,
659+ ( ) => this . paramAllocator . buildSqlAndParams (
660+ this . buildParamAnnotatedSql ( ) ,
661+ exportAnnotatedSql ,
662+ this . shouldReuseParams
663+ ) ,
664+ { cache : this . queryCache }
665+ )
666+ ) ;
633667 }
634668
635669 buildSqlAndParamsRust ( exportAnnotatedSql ) {
@@ -2960,6 +2994,7 @@ export class BaseQuery {
29602994 }
29612995
29622996 newSubQueryForCube ( cube , options ) {
2997+ options = { ...options , useNativeSqlPlanner : false } ; // We not use tesseract for pre-aggregations generation yet
29632998 if ( this . options . queryFactory ) {
29642999 // When dealing with rollup joins, it's crucial to use the correct parameter allocator for the specific cube in use.
29653000 // By default, we'll use BaseQuery, but it's important to note that different databases (Oracle, PostgreSQL, MySQL, Druid, etc.)
@@ -2983,6 +3018,7 @@ export class BaseQuery {
29833018 historyQueries : this . options . historyQueries ,
29843019 externalQueryClass : this . options . externalQueryClass ,
29853020 queryFactory : this . options . queryFactory ,
3021+ useNativeSqlPlanner : this . options . useNativeSqlPlanner ,
29863022 ...options ,
29873023 } ;
29883024 }
0 commit comments