@@ -418,40 +418,38 @@ export class BaseQuery {
418418 get allJoinHints ( ) {
419419 if ( ! this . collectedJoinHints ) {
420420 // this.collectedJoinHints = this.collectJoinHints();
421- const allMembersJoinHints = this . collectJoinHintsFromMembers ( this . allMembersConcat ( false ) ) ;
421+ const [ rootOfJoin , ... allMembersJoinHints ] = this . collectJoinHintsFromMembers ( this . allMembersConcat ( false ) ) ;
422422 const customSubQueryJoinHints = this . collectJoinHintsFromMembers ( this . joinMembersFromCustomSubQuery ( ) ) ;
423423 let joinMembersJoinHints = this . collectJoinHintsFromMembers ( this . joinMembersFromJoin ( ) ) ;
424424
425425 // One cube may join the other cube via transitive joined cubes,
426426 // members from which are referenced in the join `on` clauses.
427- // We need to collect such join hints and push them upfront of the joining one.
427+ // We need to collect such join hints and push them upfront of the joining one
428+ // but only if they don't exist yet. Cause in other case we might affect what
429+ // join path will be constructed in join graph.
428430 // It is important to use queryLevelJoinHints during the calculation if it is set.
429431
432+ const constructJP = ( ) => {
433+ const filteredJoinMembersJoinHints = joinMembersJoinHints . filter ( m => ! allMembersJoinHints . includes ( m ) ) ;
434+ return [
435+ ...this . queryLevelJoinHints ,
436+ rootOfJoin ,
437+ ...filteredJoinMembersJoinHints ,
438+ ...allMembersJoinHints ,
439+ ...customSubQueryJoinHints ,
440+ ] ;
441+ } ;
442+
430443 const prevJoins = this . join ;
431444
432- let newJoin = this . joinGraph . buildJoin ( [
433- ...this . queryLevelJoinHints ,
434- ...allMembersJoinHints ,
435- ...joinMembersJoinHints ,
436- ...customSubQueryJoinHints ,
437- ] ) ;
445+ let newJoin = this . joinGraph . buildJoin ( constructJP ( ) ) ;
438446 while ( newJoin ?. joins . length > 0 && ! R . equals ( this . join , newJoin ) ) {
439447 this . join = newJoin ;
440448 joinMembersJoinHints = this . collectJoinHintsFromMembers ( this . joinMembersFromJoin ( ) ) ;
441- newJoin = this . joinGraph . buildJoin ( [
442- ...this . queryLevelJoinHints ,
443- ...allMembersJoinHints ,
444- ...joinMembersJoinHints ,
445- ...customSubQueryJoinHints ,
446- ] ) ;
449+ newJoin = this . joinGraph . buildJoin ( constructJP ( ) ) ;
447450 }
448451
449- this . collectedJoinHints = [
450- ...this . queryLevelJoinHints ,
451- ...allMembersJoinHints ,
452- ...joinMembersJoinHints ,
453- ...customSubQueryJoinHints ,
454- ] ;
452+ this . collectedJoinHints = constructJP ( ) ;
455453
456454 this . join = prevJoins ;
457455 }
0 commit comments