@@ -7424,13 +7424,7 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
7424
7424
new ArrayList <>( predicate .getPredicates ().size () ),
7425
7425
getBooleanType ()
7426
7426
);
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 );
7434
7428
Map <TableGroup , Map <String , EntityNameUse >>[] disjunctEntityNameUsesArray = null ;
7435
7429
Map <TableGroup , Map <String , EntityNameUse >> entityNameUsesToPropagate = null ;
7436
7430
List <TableGroup > treatedTableGroups = null ;
@@ -7442,9 +7436,7 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
7442
7436
if ( !tableGroupEntityNameUses .isEmpty () ) {
7443
7437
if ( disjunctEntityNameUsesArray == null ) {
7444
7438
disjunctEntityNameUsesArray = new Map [predicate .getPredicates ().size ()];
7445
- entityNameUsesToPropagate = previousTableGroupEntityNameUses == null
7446
- ? new IdentityHashMap <>()
7447
- : new IdentityHashMap <>( previousTableGroupEntityNameUses );
7439
+ entityNameUsesToPropagate = new IdentityHashMap <>( previousTableGroupEntityNameUses );
7448
7440
}
7449
7441
if ( i == 0 ) {
7450
7442
// Collect the table groups for which filters are registered
@@ -7482,18 +7474,32 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
7482
7474
// If every disjunct contains a FILTER, we can merge the filters
7483
7475
// If every disjunct contains a TREAT, we can merge the treats
7484
7476
// 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 ();
7486
7480
final TableGroup tableGroup = entry .getKey ();
7487
7481
final Map <String , EntityNameUse > entityNameUses = entityNameUsesToPropagate .computeIfAbsent (
7488
7482
tableGroup ,
7489
7483
k -> new HashMap <>()
7490
7484
);
7491
7485
final boolean downgradeTreatUses ;
7492
7486
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
7495
7502
downgradeTreatUses = false ;
7496
- // Never downgrade the filter uses of the first disjunct
7497
7503
downgradeFilterUses = false ;
7498
7504
}
7499
7505
else {
@@ -7580,9 +7586,7 @@ else if ( useKind == EntityNameUse.UseKind.FILTER ) {
7580
7586
}
7581
7587
}
7582
7588
if ( disjunctEntityNameUsesArray == null ) {
7583
- if ( previousTableGroupEntityNameUses != null ) {
7584
- tableGroupEntityNameUses .putAll ( previousTableGroupEntityNameUses );
7585
- }
7589
+ tableGroupEntityNameUses .putAll ( previousTableGroupEntityNameUses );
7586
7590
return disjunction ;
7587
7591
}
7588
7592
@@ -7653,9 +7657,7 @@ else if ( useKind == EntityNameUse.UseKind.FILTER ) {
7653
7657
7654
7658
// Restore the parent context entity name uses state
7655
7659
tableGroupEntityNameUses .clear ();
7656
- if ( previousTableGroupEntityNameUses != null ) {
7657
- tableGroupEntityNameUses .putAll ( previousTableGroupEntityNameUses );
7658
- }
7660
+ tableGroupEntityNameUses .putAll ( previousTableGroupEntityNameUses );
7659
7661
// Propagate the union of the entity name uses upwards
7660
7662
for ( Map .Entry <TableGroup , Map <String , EntityNameUse >> entry : entityNameUsesToPropagate .entrySet () ) {
7661
7663
final Map <String , EntityNameUse > entityNameUses = tableGroupEntityNameUses .putIfAbsent (
0 commit comments