@@ -7424,13 +7424,7 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
74247424 new ArrayList <>( predicate .getPredicates ().size () ),
74257425 getBooleanType ()
74267426 );
7427- final Map <TableGroup , Map <String , EntityNameUse >> previousTableGroupEntityNameUses ;
7428- if ( tableGroupEntityNameUses .isEmpty () ) {
7429- previousTableGroupEntityNameUses = null ;
7430- }
7431- else {
7432- previousTableGroupEntityNameUses = new IdentityHashMap <>( tableGroupEntityNameUses );
7433- }
7427+ final Map <TableGroup , Map <String , EntityNameUse >> previousTableGroupEntityNameUses = new IdentityHashMap <>( tableGroupEntityNameUses );
74347428 Map <TableGroup , Map <String , EntityNameUse >>[] disjunctEntityNameUsesArray = null ;
74357429 Map <TableGroup , Map <String , EntityNameUse >> entityNameUsesToPropagate = null ;
74367430 List <TableGroup > treatedTableGroups = null ;
@@ -7442,9 +7436,7 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
74427436 if ( !tableGroupEntityNameUses .isEmpty () ) {
74437437 if ( disjunctEntityNameUsesArray == null ) {
74447438 disjunctEntityNameUsesArray = new Map [predicate .getPredicates ().size ()];
7445- entityNameUsesToPropagate = previousTableGroupEntityNameUses == null
7446- ? new IdentityHashMap <>()
7447- : new IdentityHashMap <>( previousTableGroupEntityNameUses );
7439+ entityNameUsesToPropagate = new IdentityHashMap <>( previousTableGroupEntityNameUses );
74487440 }
74497441 if ( i == 0 ) {
74507442 // Collect the table groups for which filters are registered
@@ -7482,18 +7474,32 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
74827474 // If every disjunct contains a FILTER, we can merge the filters
74837475 // If every disjunct contains a TREAT, we can merge the treats
74847476 // Otherwise, we downgrade the entity name uses to expression uses
7485- for ( Map .Entry <TableGroup , Map <String , EntityNameUse >> entry : tableGroupEntityNameUses .entrySet () ) {
7477+ final Iterator <Map .Entry <TableGroup , Map <String , EntityNameUse >>> iterator = tableGroupEntityNameUses .entrySet ().iterator ();
7478+ while ( iterator .hasNext () ) {
7479+ final Map .Entry <TableGroup , Map <String , EntityNameUse >> entry = iterator .next ();
74867480 final TableGroup tableGroup = entry .getKey ();
74877481 final Map <String , EntityNameUse > entityNameUses = entityNameUsesToPropagate .computeIfAbsent (
74887482 tableGroup ,
74897483 k -> new HashMap <>()
74907484 );
74917485 final boolean downgradeTreatUses ;
74927486 final boolean downgradeFilterUses ;
7493- if ( i == 0 ) {
7494- // Never downgrade the treat uses of the first disjunct
7487+ if ( getFromClauseAccess ().findTableGroup ( tableGroup .getNavigablePath () ) == null ) {
7488+ // Always preserver name uses for table groups not found in the current from clause index
7489+ previousTableGroupEntityNameUses .put ( tableGroup , entry .getValue () );
7490+ // Remove from the current junction context since no more processing is required
7491+ if ( treatedTableGroups != null ) {
7492+ treatedTableGroups .remove ( tableGroup );
7493+ }
7494+ if ( filteredTableGroups != null ) {
7495+ filteredTableGroups .remove ( tableGroup );
7496+ }
7497+ iterator .remove ();
7498+ continue ;
7499+ }
7500+ else if ( i == 0 ) {
7501+ // Never downgrade treat or filter uses of the first disjunct
74957502 downgradeTreatUses = false ;
7496- // Never downgrade the filter uses of the first disjunct
74977503 downgradeFilterUses = false ;
74987504 }
74997505 else {
@@ -7580,9 +7586,7 @@ else if ( useKind == EntityNameUse.UseKind.FILTER ) {
75807586 }
75817587 }
75827588 if ( disjunctEntityNameUsesArray == null ) {
7583- if ( previousTableGroupEntityNameUses != null ) {
7584- tableGroupEntityNameUses .putAll ( previousTableGroupEntityNameUses );
7585- }
7589+ tableGroupEntityNameUses .putAll ( previousTableGroupEntityNameUses );
75867590 return disjunction ;
75877591 }
75887592
@@ -7653,9 +7657,7 @@ else if ( useKind == EntityNameUse.UseKind.FILTER ) {
76537657
76547658 // Restore the parent context entity name uses state
76557659 tableGroupEntityNameUses .clear ();
7656- if ( previousTableGroupEntityNameUses != null ) {
7657- tableGroupEntityNameUses .putAll ( previousTableGroupEntityNameUses );
7658- }
7660+ tableGroupEntityNameUses .putAll ( previousTableGroupEntityNameUses );
76597661 // Propagate the union of the entity name uses upwards
76607662 for ( Map .Entry <TableGroup , Map <String , EntityNameUse >> entry : entityNameUsesToPropagate .entrySet () ) {
76617663 final Map <String , EntityNameUse > entityNameUses = tableGroupEntityNameUses .putIfAbsent (
0 commit comments