Skip to content

Commit 95ef45b

Browse files
mbelladesebersole
authored andcommitted
HHH-18174 Fix junction entity name uses algorithm for subqueries
1 parent a33ae2d commit 95ef45b

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7425,13 +7425,7 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
74257425
new ArrayList<>( predicate.getPredicates().size() ),
74267426
getBooleanType()
74277427
);
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 );
74357429
Map<TableGroup, Map<String, EntityNameUse>>[] disjunctEntityNameUsesArray = null;
74367430
Map<TableGroup, Map<String, EntityNameUse>> entityNameUsesToPropagate = null;
74377431
List<TableGroup> treatedTableGroups = null;
@@ -7443,9 +7437,7 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
74437437
if ( !tableGroupEntityNameUses.isEmpty() ) {
74447438
if ( disjunctEntityNameUsesArray == null ) {
74457439
disjunctEntityNameUsesArray = new Map[predicate.getPredicates().size()];
7446-
entityNameUsesToPropagate = previousTableGroupEntityNameUses == null
7447-
? new IdentityHashMap<>()
7448-
: new IdentityHashMap<>( previousTableGroupEntityNameUses );
7440+
entityNameUsesToPropagate = new IdentityHashMap<>( previousTableGroupEntityNameUses );
74497441
}
74507442
if ( i == 0 ) {
74517443
// Collect the table groups for which filters are registered
@@ -7483,18 +7475,32 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
74837475
// If every disjunct contains a FILTER, we can merge the filters
74847476
// If every disjunct contains a TREAT, we can merge the treats
74857477
// 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();
74877481
final TableGroup tableGroup = entry.getKey();
74887482
final Map<String, EntityNameUse> entityNameUses = entityNameUsesToPropagate.computeIfAbsent(
74897483
tableGroup,
74907484
k -> new HashMap<>()
74917485
);
74927486
final boolean downgradeTreatUses;
74937487
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
74967503
downgradeTreatUses = false;
7497-
// Never downgrade the filter uses of the first disjunct
74987504
downgradeFilterUses = false;
74997505
}
75007506
else {
@@ -7581,9 +7587,7 @@ else if ( useKind == EntityNameUse.UseKind.FILTER ) {
75817587
}
75827588
}
75837589
if ( disjunctEntityNameUsesArray == null ) {
7584-
if ( previousTableGroupEntityNameUses != null ) {
7585-
tableGroupEntityNameUses.putAll( previousTableGroupEntityNameUses );
7586-
}
7590+
tableGroupEntityNameUses.putAll( previousTableGroupEntityNameUses );
75877591
return disjunction;
75887592
}
75897593

@@ -7654,9 +7658,7 @@ else if ( useKind == EntityNameUse.UseKind.FILTER ) {
76547658

76557659
// Restore the parent context entity name uses state
76567660
tableGroupEntityNameUses.clear();
7657-
if ( previousTableGroupEntityNameUses != null ) {
7658-
tableGroupEntityNameUses.putAll( previousTableGroupEntityNameUses );
7659-
}
7661+
tableGroupEntityNameUses.putAll( previousTableGroupEntityNameUses );
76607662
// Propagate the union of the entity name uses upwards
76617663
for ( Map.Entry<TableGroup, Map<String, EntityNameUse>> entry : entityNameUsesToPropagate.entrySet() ) {
76627664
final Map<String, EntityNameUse> entityNameUses = tableGroupEntityNameUses.putIfAbsent(

0 commit comments

Comments
 (0)