Skip to content

Commit 47f1a12

Browse files
committed
HHH-18851 Fix parameter type inference issue when IN predicate is uses array_contains()
1 parent 26ed1e2 commit 47f1a12

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/function/array/ArrayContainsArgumentTypeResolver.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ public class ArrayContainsArgumentTypeResolver extends AbstractFunctionArgumentT
4343
}
4444
}
4545
else if ( argumentIndex == 1 ) {
46+
final SqmTypedNode<?> nodeToResolve = arguments.get( 1 );
47+
if ( nodeToResolve.getExpressible() instanceof MappingModelExpressible<?> ) {
48+
// If the node already has suitable type, don't infer it to be treated as an array
49+
return null;
50+
}
4651
final SqmTypedNode<?> node = arguments.get( 0 );
4752
if ( node instanceof SqmExpression<?> ) {
4853
final MappingModelExpressible<?> expressible = converter.determineValueMapping( (SqmExpression<?>) node );
49-
if ( expressible != null ) {
54+
if ( expressible instanceof BasicPluralType<?, ?> ) {
5055
return expressible;
5156
}
5257
}

hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3391,12 +3391,15 @@ else if ( inListContext instanceof HqlParser.ArrayInListContext arrayInListConte
33913391

33923392
final SqmExpression<?> arrayExpr = (SqmExpression<?>) arrayInListContext.expression().accept( this );
33933393
final SqmExpressible<?> arrayExpressible = arrayExpr.getExpressible();
3394-
if ( arrayExpressible != null && !( arrayExpressible.getSqmType() instanceof BasicPluralType<?, ?>) ) {
3395-
throw new SemanticException(
3396-
"Right operand for in-array predicate must be a basic plural type expression, but found: "
3394+
if ( arrayExpressible != null ) {
3395+
if ( !(arrayExpressible.getSqmType() instanceof BasicPluralType<?, ?> pluralType) ) {
3396+
throw new SemanticException(
3397+
"Right operand for in-array predicate must be a basic plural type expression, but found: "
33973398
+ arrayExpressible.getSqmType(),
3398-
query
3399-
);
3399+
query
3400+
);
3401+
}
3402+
testExpression.applyInferableType( pluralType.getElementType() );
34003403
}
34013404
final SelfRenderingSqmFunction<Boolean> contains = getFunctionDescriptor( "array_contains" ).generateSqmExpression(
34023405
asList( arrayExpr, testExpression ),

hibernate-core/src/test/java/org/hibernate/orm/test/function/array/ArrayContainsTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.hibernate.testing.orm.junit.BootstrapServiceRegistry;
1616
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
1717
import org.hibernate.testing.orm.junit.DomainModel;
18+
import org.hibernate.testing.orm.junit.JiraKey;
1819
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
1920
import org.hibernate.testing.orm.junit.SessionFactory;
2021
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@@ -156,4 +157,22 @@ public void testInSyntax(SessionFactoryScope scope) {
156157
} );
157158
}
158159

160+
@Test
161+
@JiraKey( "HHH-18851" )
162+
public void testInArray(SessionFactoryScope scope) {
163+
scope.inSession( em -> {
164+
List<Tuple> results = em.createQuery(
165+
"select e.id " +
166+
"from EntityWithArrays e " +
167+
"where :p in e.theArray",
168+
Tuple.class
169+
)
170+
.setParameter( "p", "abc" )
171+
.getResultList();
172+
173+
assertEquals( 1, results.size() );
174+
assertEquals( 2L, results.get( 0 ).get( 0 ) );
175+
} );
176+
}
177+
159178
}

0 commit comments

Comments
 (0)