HHH-18174 Fix junction entity name uses algorithm for subqueries

(cherry picked from commit 95ef45bef8473670f2080037d98fa8f453751b6e)
This commit is contained in:
Marco Belladelli 2024-06-05 13:51:51 +02:00 committed by Steve Ebersole
parent 0e9577a4d6
commit 744732b726

View File

@ -7424,13 +7424,7 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
new ArrayList<>( predicate.getPredicates().size() ), new ArrayList<>( predicate.getPredicates().size() ),
getBooleanType() getBooleanType()
); );
final Map<TableGroup, Map<String, EntityNameUse>> previousTableGroupEntityNameUses; final Map<TableGroup, Map<String, EntityNameUse>> previousTableGroupEntityNameUses = new IdentityHashMap<>( tableGroupEntityNameUses );
if ( tableGroupEntityNameUses.isEmpty() ) {
previousTableGroupEntityNameUses = null;
}
else {
previousTableGroupEntityNameUses = new IdentityHashMap<>( tableGroupEntityNameUses );
}
Map<TableGroup, Map<String, EntityNameUse>>[] disjunctEntityNameUsesArray = null; Map<TableGroup, Map<String, EntityNameUse>>[] disjunctEntityNameUsesArray = null;
Map<TableGroup, Map<String, EntityNameUse>> entityNameUsesToPropagate = null; Map<TableGroup, Map<String, EntityNameUse>> entityNameUsesToPropagate = null;
List<TableGroup> treatedTableGroups = null; List<TableGroup> treatedTableGroups = null;
@ -7442,9 +7436,7 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
if ( !tableGroupEntityNameUses.isEmpty() ) { if ( !tableGroupEntityNameUses.isEmpty() ) {
if ( disjunctEntityNameUsesArray == null ) { if ( disjunctEntityNameUsesArray == null ) {
disjunctEntityNameUsesArray = new Map[predicate.getPredicates().size()]; disjunctEntityNameUsesArray = new Map[predicate.getPredicates().size()];
entityNameUsesToPropagate = previousTableGroupEntityNameUses == null entityNameUsesToPropagate = new IdentityHashMap<>( previousTableGroupEntityNameUses );
? new IdentityHashMap<>()
: new IdentityHashMap<>( previousTableGroupEntityNameUses );
} }
if ( i == 0 ) { if ( i == 0 ) {
// Collect the table groups for which filters are registered // Collect the table groups for which filters are registered
@ -7482,7 +7474,9 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
// If every disjunct contains a FILTER, we can merge the filters // If every disjunct contains a FILTER, we can merge the filters
// If every disjunct contains a TREAT, we can merge the treats // If every disjunct contains a TREAT, we can merge the treats
// Otherwise, we downgrade the entity name uses to expression uses // 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 TableGroup tableGroup = entry.getKey();
final Map<String, EntityNameUse> entityNameUses = entityNameUsesToPropagate.computeIfAbsent( final Map<String, EntityNameUse> entityNameUses = entityNameUsesToPropagate.computeIfAbsent(
tableGroup, tableGroup,
@ -7490,10 +7484,22 @@ public Junction visitJunctionPredicate(SqmJunctionPredicate predicate) {
); );
final boolean downgradeTreatUses; final boolean downgradeTreatUses;
final boolean downgradeFilterUses; final boolean downgradeFilterUses;
if ( i == 0 ) { if ( getFromClauseAccess().findTableGroup( tableGroup.getNavigablePath() ) == null ) {
// Never downgrade the treat uses of the first disjunct // 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; downgradeTreatUses = false;
// Never downgrade the filter uses of the first disjunct
downgradeFilterUses = false; downgradeFilterUses = false;
} }
else { else {
@ -7580,9 +7586,7 @@ else if ( useKind == EntityNameUse.UseKind.FILTER ) {
} }
} }
if ( disjunctEntityNameUsesArray == null ) { if ( disjunctEntityNameUsesArray == null ) {
if ( previousTableGroupEntityNameUses != null ) { tableGroupEntityNameUses.putAll( previousTableGroupEntityNameUses );
tableGroupEntityNameUses.putAll( previousTableGroupEntityNameUses );
}
return disjunction; return disjunction;
} }
@ -7653,9 +7657,7 @@ else if ( useKind == EntityNameUse.UseKind.FILTER ) {
// Restore the parent context entity name uses state // Restore the parent context entity name uses state
tableGroupEntityNameUses.clear(); tableGroupEntityNameUses.clear();
if ( previousTableGroupEntityNameUses != null ) { tableGroupEntityNameUses.putAll( previousTableGroupEntityNameUses );
tableGroupEntityNameUses.putAll( previousTableGroupEntityNameUses );
}
// Propagate the union of the entity name uses upwards // Propagate the union of the entity name uses upwards
for ( Map.Entry<TableGroup, Map<String, EntityNameUse>> entry : entityNameUsesToPropagate.entrySet() ) { for ( Map.Entry<TableGroup, Map<String, EntityNameUse>> entry : entityNameUsesToPropagate.entrySet() ) {
final Map<String, EntityNameUse> entityNameUses = tableGroupEntityNameUses.putIfAbsent( final Map<String, EntityNameUse> entityNameUses = tableGroupEntityNameUses.putIfAbsent(