@@ -7425,13 +7425,7 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
7425
7425
new ArrayList <>( predicate .getPredicates ().size () ),
7426
7426
getBooleanType ()
7427
7427
);
7428
- final Map <TableGroup , Map <String , EntityNameUse >> previousTableGroupEntityNameUses ;
7429
- if ( tableGroupEntityNameUses .isEmpty () ) {
7430
- previousTableGroupEntityNameUses = null ;
7431
- }
7432
- else {
7433
- previousTableGroupEntityNameUses = new IdentityHashMap <>( tableGroupEntityNameUses );
7434
- }
7428
+ final Map <TableGroup , Map <String , EntityNameUse >> previousTableGroupEntityNameUses = new IdentityHashMap <>( tableGroupEntityNameUses );
7435
7429
Map <TableGroup , Map <String , EntityNameUse >>[] disjunctEntityNameUsesArray = null ;
7436
7430
Map <TableGroup , Map <String , EntityNameUse >> entityNameUsesToPropagate = null ;
7437
7431
List <TableGroup > treatedTableGroups = null ;
@@ -7443,9 +7437,7 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
7443
7437
if ( !tableGroupEntityNameUses .isEmpty () ) {
7444
7438
if ( disjunctEntityNameUsesArray == null ) {
7445
7439
disjunctEntityNameUsesArray = new Map [predicate .getPredicates ().size ()];
7446
- entityNameUsesToPropagate = previousTableGroupEntityNameUses == null
7447
- ? new IdentityHashMap <>()
7448
- : new IdentityHashMap <>( previousTableGroupEntityNameUses );
7440
+ entityNameUsesToPropagate = new IdentityHashMap <>( previousTableGroupEntityNameUses );
7449
7441
}
7450
7442
if ( i == 0 ) {
7451
7443
// Collect the table groups for which filters are registered
@@ -7483,18 +7475,32 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
7483
7475
// If every disjunct contains a FILTER, we can merge the filters
7484
7476
// If every disjunct contains a TREAT, we can merge the treats
7485
7477
// Otherwise, we downgrade the entity name uses to expression uses
7486
- for ( Map .Entry <TableGroup , Map <String , EntityNameUse >> entry : tableGroupEntityNameUses .entrySet () ) {
7478
+ final Iterator <Map .Entry <TableGroup , Map <String , EntityNameUse >>> iterator = tableGroupEntityNameUses .entrySet ().iterator ();
7479
+ while ( iterator .hasNext () ) {
7480
+ final Map .Entry <TableGroup , Map <String , EntityNameUse >> entry = iterator .next ();
7487
7481
final TableGroup tableGroup = entry .getKey ();
7488
7482
final Map <String , EntityNameUse > entityNameUses = entityNameUsesToPropagate .computeIfAbsent (
7489
7483
tableGroup ,
7490
7484
k -> new HashMap <>()
7491
7485
);
7492
7486
final boolean downgradeTreatUses ;
7493
7487
final boolean downgradeFilterUses ;
7494
- if ( i == 0 ) {
7495
- // Never downgrade the treat uses of the first disjunct
7488
+ if ( getFromClauseAccess ().findTableGroup ( tableGroup .getNavigablePath () ) == null ) {
7489
+ // Always preserver name uses for table groups not found in the current from clause index
7490
+ previousTableGroupEntityNameUses .put ( tableGroup , entry .getValue () );
7491
+ // Remove from the current junction context since no more processing is required
7492
+ if ( treatedTableGroups != null ) {
7493
+ treatedTableGroups .remove ( tableGroup );
7494
+ }
7495
+ if ( filteredTableGroups != null ) {
7496
+ filteredTableGroups .remove ( tableGroup );
7497
+ }
7498
+ iterator .remove ();
7499
+ continue ;
7500
+ }
7501
+ else if ( i == 0 ) {
7502
+ // Never downgrade treat or filter uses of the first disjunct
7496
7503
downgradeTreatUses = false ;
7497
- // Never downgrade the filter uses of the first disjunct
7498
7504
downgradeFilterUses = false ;
7499
7505
}
7500
7506
else {
@@ -7581,9 +7587,7 @@ else if ( useKind == EntityNameUse.UseKind.FILTER ) {
7581
7587
}
7582
7588
}
7583
7589
if ( disjunctEntityNameUsesArray == null ) {
7584
- if ( previousTableGroupEntityNameUses != null ) {
7585
- tableGroupEntityNameUses .putAll ( previousTableGroupEntityNameUses );
7586
- }
7590
+ tableGroupEntityNameUses .putAll ( previousTableGroupEntityNameUses );
7587
7591
return disjunction ;
7588
7592
}
7589
7593
@@ -7654,9 +7658,7 @@ else if ( useKind == EntityNameUse.UseKind.FILTER ) {
7654
7658
7655
7659
// Restore the parent context entity name uses state
7656
7660
tableGroupEntityNameUses .clear ();
7657
- if ( previousTableGroupEntityNameUses != null ) {
7658
- tableGroupEntityNameUses .putAll ( previousTableGroupEntityNameUses );
7659
- }
7661
+ tableGroupEntityNameUses .putAll ( previousTableGroupEntityNameUses );
7660
7662
// Propagate the union of the entity name uses upwards
7661
7663
for ( Map .Entry <TableGroup , Map <String , EntityNameUse >> entry : entityNameUsesToPropagate .entrySet () ) {
7662
7664
final Map <String , EntityNameUse > entityNameUses = tableGroupEntityNameUses .putIfAbsent (
0 commit comments