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