HHH-17386 Fix type inference source reset for top level predicates

This commit is contained in:
Christian Beikov 2023-11-03 18:11:38 +01:00
parent 756f253ee2
commit 30ab52d07c
2 changed files with 19 additions and 7 deletions

View File

@ -6866,9 +6866,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
Expression otherwise = null;
for ( SqmCaseSearched.WhenFragment<?> whenFragment : expression.getWhenFragments() ) {
inferrableTypeAccessStack.push( () -> null );
final Predicate whenPredicate = visitNestedTopLevelPredicate( whenFragment.getPredicate() );
inferrableTypeAccessStack.pop();
final MappingModelExpressible<?> alreadyKnown = resolved;
inferrableTypeAccessStack.push(
() -> alreadyKnown == null && inferenceSupplier != null ? inferenceSupplier.get() : alreadyKnown
@ -7056,7 +7054,9 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
originalConjunctTableGroupTreatUsages = new IdentityHashMap<>( tableGroupEntityNameUses );
}
tableGroupEntityNameUses.clear();
inferrableTypeAccessStack.push( this::getBooleanType );
final Predicate result = (Predicate) predicate.accept( this );
inferrableTypeAccessStack.pop();
final Predicate finalPredicate = combinePredicates(
result,
consumeConjunctTreatTypeRestrictions()
@ -7866,7 +7866,9 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
@Override
public Object visitBooleanExpressionPredicate(SqmBooleanExpressionPredicate predicate) {
inferrableTypeAccessStack.push( this::getBooleanType );
final Expression booleanExpression = (Expression) predicate.getBooleanExpression().accept( this );
inferrableTypeAccessStack.pop();
if ( booleanExpression instanceof SelfRenderingExpression ) {
final Predicate sqlPredicate = new SelfRenderingPredicate( (SelfRenderingExpression) booleanExpression );
if ( predicate.isNegated() ) {
@ -7895,11 +7897,10 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
@Override
public Object visitExistsPredicate(SqmExistsPredicate predicate) {
return new ExistsPredicate(
(SelectStatement) predicate.getExpression().accept( this ),
predicate.isNegated(),
getBooleanType()
);
inferrableTypeAccessStack.push( () -> null );
final SelectStatement selectStatement = (SelectStatement) predicate.getExpression().accept( this );
inferrableTypeAccessStack.pop();
return new ExistsPredicate( selectStatement, predicate.isNegated(), getBooleanType() );
}
@Override

View File

@ -13,6 +13,7 @@ import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.orm.junit.JiraKey;
import org.junit.Before;
import org.junit.Test;
@ -61,6 +62,16 @@ public class InferenceTest extends BaseCoreFunctionalTestCase {
}
@Test
@JiraKey("HHH-17386")
public void testInferenceSourceResetForOnClause() {
doInHibernate( this::sessionFactory, session -> {
session.createQuery( "from Person p where p in (select p2 from Person p2 join Person p3 on exists (select 1 from Person p4))", Person.class )
.getResultList();
} );
}
@Entity(name = "Person")
public static class Person {