Skip to content

Commit

Permalink
HHH-18174 Fix junction entity name uses algorithm for subqueries
Browse files Browse the repository at this point in the history
(cherry picked from commit 95ef45b)
  • Loading branch information
mbladel authored and sebersole committed Sep 16, 2024
1 parent 0e9577a commit 744732b
Showing 1 changed file with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7424,13 +7424,7 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
new ArrayList<>( predicate.getPredicates().size() ),
getBooleanType()
);
final Map<TableGroup, Map<String, EntityNameUse>> previousTableGroupEntityNameUses;
if ( tableGroupEntityNameUses.isEmpty() ) {
previousTableGroupEntityNameUses = null;
}
else {
previousTableGroupEntityNameUses = new IdentityHashMap<>( tableGroupEntityNameUses );
}
final Map<TableGroup, Map<String, EntityNameUse>> previousTableGroupEntityNameUses = new IdentityHashMap<>( tableGroupEntityNameUses );
Map<TableGroup, Map<String, EntityNameUse>>[] disjunctEntityNameUsesArray = null;
Map<TableGroup, Map<String, EntityNameUse>> entityNameUsesToPropagate = null;
List<TableGroup> treatedTableGroups = null;
Expand All @@ -7442,9 +7436,7 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
if ( !tableGroupEntityNameUses.isEmpty() ) {
if ( disjunctEntityNameUsesArray == null ) {
disjunctEntityNameUsesArray = new Map[predicate.getPredicates().size()];
entityNameUsesToPropagate = previousTableGroupEntityNameUses == null
? new IdentityHashMap<>()
: new IdentityHashMap<>( previousTableGroupEntityNameUses );
entityNameUsesToPropagate = new IdentityHashMap<>( previousTableGroupEntityNameUses );
}
if ( i == 0 ) {
// Collect the table groups for which filters are registered
Expand Down Expand Up @@ -7482,18 +7474,32 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
// If every disjunct contains a FILTER, we can merge the filters
// If every disjunct contains a TREAT, we can merge the treats
// Otherwise, we downgrade the entity name uses to expression uses
for ( Map.Entry<TableGroup, Map<String, EntityNameUse>> entry : tableGroupEntityNameUses.entrySet() ) {
final Iterator<Map.Entry<TableGroup, Map<String, EntityNameUse>>> iterator = tableGroupEntityNameUses.entrySet().iterator();
while ( iterator.hasNext() ) {
final Map.Entry<TableGroup, Map<String, EntityNameUse>> entry = iterator.next();
final TableGroup tableGroup = entry.getKey();
final Map<String, EntityNameUse> entityNameUses = entityNameUsesToPropagate.computeIfAbsent(
tableGroup,
k -> new HashMap<>()
);
final boolean downgradeTreatUses;
final boolean downgradeFilterUses;
if ( i == 0 ) {
// Never downgrade the treat uses of the first disjunct
if ( getFromClauseAccess().findTableGroup( tableGroup.getNavigablePath() ) == null ) {
// Always preserver name uses for table groups not found in the current from clause index
previousTableGroupEntityNameUses.put( tableGroup, entry.getValue() );
// Remove from the current junction context since no more processing is required
if ( treatedTableGroups != null ) {
treatedTableGroups.remove( tableGroup );
}
if ( filteredTableGroups != null ) {
filteredTableGroups.remove( tableGroup );
}
iterator.remove();
continue;
}
else if ( i == 0 ) {
// Never downgrade treat or filter uses of the first disjunct
downgradeTreatUses = false;
// Never downgrade the filter uses of the first disjunct
downgradeFilterUses = false;
}
else {
Expand Down Expand Up @@ -7580,9 +7586,7 @@ else if ( useKind == EntityNameUse.UseKind.FILTER ) {
}
}
if ( disjunctEntityNameUsesArray == null ) {
if ( previousTableGroupEntityNameUses != null ) {
tableGroupEntityNameUses.putAll( previousTableGroupEntityNameUses );
}
tableGroupEntityNameUses.putAll( previousTableGroupEntityNameUses );
return disjunction;
}

Expand Down Expand Up @@ -7653,9 +7657,7 @@ else if ( useKind == EntityNameUse.UseKind.FILTER ) {

// Restore the parent context entity name uses state
tableGroupEntityNameUses.clear();
if ( previousTableGroupEntityNameUses != null ) {
tableGroupEntityNameUses.putAll( previousTableGroupEntityNameUses );
}
tableGroupEntityNameUses.putAll( previousTableGroupEntityNameUses );
// Propagate the union of the entity name uses upwards
for ( Map.Entry<TableGroup, Map<String, EntityNameUse>> entry : entityNameUsesToPropagate.entrySet() ) {
final Map<String, EntityNameUse> entityNameUses = tableGroupEntityNameUses.putIfAbsent(
Expand Down

0 comments on commit 744732b

Please sign in to comment.