From 30ab52d07cc77c9a1be723c6f95a2d16152834cb Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Fri, 3 Nov 2023 18:11:38 +0100 Subject: [PATCH] HHH-17386 Fix type inference source reset for top level predicates --- .../query/sqm/sql/BaseSqmToSqlAstConverter.java | 15 ++++++++------- .../org/hibernate/orm/test/hql/InferenceTest.java | 11 +++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java index a970d62c1e..bb1fc8b809 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java @@ -6866,9 +6866,7 @@ public abstract class BaseSqmToSqlAstConverter 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 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 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 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 diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/hql/InferenceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/hql/InferenceTest.java index ae3f5edf19..a93d49cf9c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/hql/InferenceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/hql/InferenceTest.java @@ -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 {