Skip to content

Commit 236e3e2

Browse files
mbelladebeikov
authored andcommitted
HHH-17667 Fix pruning of root table with discriminator predicate
1 parent ce0217d commit 236e3e2

File tree

1 file changed

+48
-54
lines changed

1 file changed

+48
-54
lines changed

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

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
import org.hibernate.persister.entity.AbstractEntityPersister;
9494
import org.hibernate.persister.entity.EntityNameUse;
9595
import org.hibernate.persister.entity.EntityPersister;
96-
import org.hibernate.persister.entity.SingleTableEntityPersister;
9796
import org.hibernate.query.BindableType;
9897
import org.hibernate.query.QueryLogging;
9998
import org.hibernate.query.ReturnableType;
@@ -3167,9 +3166,8 @@ protected void registerTypeUsage(TableGroup tableGroup) {
31673166
registerEntityNameUsage( tableGroup, EntityNameUse.PROJECTION, persister.getEntityName(), true );
31683167
}
31693168
else {
3170-
// Avoid doing this for single table entity persisters, as the table span includes secondary tables,
3171-
// which we don't want to resolve, though we know that there is only a single table anyway
3172-
if ( persister instanceof SingleTableEntityPersister ) {
3169+
// Avoid resolving subclass tables for persisters with physical discriminators as we won't need them
3170+
if ( persister.getDiscriminatorMapping().hasPhysicalColumn() ) {
31733171
return;
31743172
}
31753173
final int subclassTableSpan = persister.getSubclassTableSpan();
@@ -7557,46 +7555,56 @@ else if ( rhs instanceof DiscriminatorPathInterpretation ) {
75577555
else {
75587556
return;
75597557
}
7560-
if ( literalExpression == null ) {
7561-
// We have to assume all types are possible and can't do optimizations
7562-
final TableGroup tableGroup = getFromClauseIndex().getTableGroup( typeExpression.getNavigablePath().getParent() );
7563-
final EntityMappingType entityMappingType = (EntityMappingType) tableGroup.getModelPart().getPartMappingType();
7564-
registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, entityMappingType.getEntityName() );
7565-
for ( EntityMappingType subMappingType : entityMappingType.getSubMappingTypes() ) {
7566-
registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, subMappingType.getEntityName() );
7567-
}
7568-
}
7569-
else {
7570-
handleTypeComparison( typeExpression, Collections.singletonList( literalExpression ), inclusive );
7571-
}
7558+
handleTypeComparison(
7559+
typeExpression,
7560+
literalExpression != null ? singletonList( literalExpression ) : null,
7561+
inclusive
7562+
);
75727563
}
75737564

75747565
private void handleTypeComparison(
7575-
DiscriminatorPathInterpretation typeExpression,
7566+
DiscriminatorPathInterpretation<?> typeExpression,
75767567
List<EntityTypeLiteral> literalExpressions,
75777568
boolean inclusive) {
75787569
final TableGroup tableGroup = getFromClauseIndex().getTableGroup( typeExpression.getNavigablePath().getParent() );
7579-
if ( inclusive ) {
7580-
for ( EntityTypeLiteral literalExpr : literalExpressions ) {
7581-
registerEntityNameUsage(
7582-
tableGroup,
7583-
EntityNameUse.FILTER,
7584-
literalExpr.getEntityTypeDescriptor().getEntityName()
7585-
);
7570+
final EntityMappingType entityMappingType = (EntityMappingType) tableGroup.getModelPart().getPartMappingType();
7571+
if ( entityMappingType.getDiscriminatorMapping().hasPhysicalColumn() ) {
7572+
// Prevent pruning of the root type's table reference containing the physical discriminator column
7573+
registerEntityNameUsage(
7574+
tableGroup,
7575+
EntityNameUse.EXPRESSION,
7576+
entityMappingType.getRootEntityDescriptor().getEntityName()
7577+
);
7578+
}
7579+
if ( literalExpressions == null ) {
7580+
// We have to assume all types are possible and can't do optimizations
7581+
registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, entityMappingType.getEntityName() );
7582+
for ( EntityMappingType subMappingType : entityMappingType.getSubMappingTypes() ) {
7583+
registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, subMappingType.getEntityName() );
75867584
}
75877585
}
75887586
else {
7589-
final EntityMappingType entityMappingType = (EntityMappingType) tableGroup.getModelPart().getPartMappingType();
7590-
final Set<String> excludedEntityNames = new HashSet<>(entityMappingType.getSubMappingTypes().size());
7591-
for ( EntityTypeLiteral literalExpr : literalExpressions ) {
7592-
excludedEntityNames.add( literalExpr.getEntityTypeDescriptor().getEntityName() );
7593-
}
7594-
if ( !excludedEntityNames.contains( entityMappingType.getEntityName() ) ) {
7595-
registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, entityMappingType.getEntityName() );
7587+
if ( inclusive ) {
7588+
for ( EntityTypeLiteral literalExpr : literalExpressions ) {
7589+
registerEntityNameUsage(
7590+
tableGroup,
7591+
EntityNameUse.FILTER,
7592+
literalExpr.getEntityTypeDescriptor().getEntityName()
7593+
);
7594+
}
75967595
}
7597-
for ( EntityMappingType subMappingType : entityMappingType.getSubMappingTypes() ) {
7598-
if ( !excludedEntityNames.contains( subMappingType.getEntityName() ) ) {
7599-
registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, subMappingType.getEntityName() );
7596+
else {
7597+
final Set<String> excludedEntityNames = new HashSet<>( entityMappingType.getSubMappingTypes().size() );
7598+
for ( EntityTypeLiteral literalExpr : literalExpressions ) {
7599+
excludedEntityNames.add( literalExpr.getEntityTypeDescriptor().getEntityName() );
7600+
}
7601+
if ( !excludedEntityNames.contains( entityMappingType.getEntityName() ) ) {
7602+
registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, entityMappingType.getEntityName() );
7603+
}
7604+
for ( EntityMappingType subMappingType : entityMappingType.getSubMappingTypes() ) {
7605+
if ( !excludedEntityNames.contains( subMappingType.getEntityName() ) ) {
7606+
registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, subMappingType.getEntityName() );
7607+
}
76007608
}
76017609
}
76027610
}
@@ -7852,26 +7860,12 @@ private void handleTypeComparison(InListPredicate inPredicate) {
78527860
break;
78537861
}
78547862
}
7855-
if ( containsNonLiteral ) {
7856-
// We have to assume all types are possible and can't do optimizations
7857-
final TableGroup tableGroup = getFromClauseIndex().getTableGroup(
7858-
typeExpression.getNavigablePath().getParent()
7859-
);
7860-
final EntityMappingType entityMappingType = (EntityMappingType) tableGroup.getModelPart()
7861-
.getPartMappingType();
7862-
registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, entityMappingType.getEntityName() );
7863-
for ( EntityMappingType subMappingType : entityMappingType.getSubMappingTypes() ) {
7864-
registerEntityNameUsage( tableGroup, EntityNameUse.FILTER, subMappingType.getEntityName() );
7865-
}
7866-
}
7867-
else {
7868-
//noinspection unchecked
7869-
handleTypeComparison(
7870-
typeExpression,
7871-
(List<EntityTypeLiteral>) (List<?>) inPredicate.getListExpressions(),
7872-
!inPredicate.isNegated()
7873-
);
7874-
}
7863+
//noinspection unchecked
7864+
handleTypeComparison(
7865+
typeExpression,
7866+
containsNonLiteral ? null : (List<EntityTypeLiteral>) (List<?>) inPredicate.getListExpressions(),
7867+
!inPredicate.isNegated()
7868+
);
78757869
}
78767870
}
78777871

0 commit comments

Comments
 (0)