Skip to content

Commit 73ffc45

Browse files
committed
HHH-18851 Fix parameter type inference issue when IN predicate is uses array_contains()
1 parent 7cdab31 commit 73ffc45

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
@@ -3494,11 +3494,14 @@ else if ( inListContext instanceof HqlParser.ArrayInListContext ) {
34943494

34953495
final SqmExpression<?> arrayExpr = (SqmExpression<?>) arrayInListContext.expression().accept( this );
34963496
final SqmExpressible<?> arrayExpressible = arrayExpr.getExpressible();
3497-
if ( arrayExpressible != null && !( arrayExpressible.getSqmType() instanceof BasicPluralType<?, ?>) ) {
3498-
throw new SemanticException(
3499-
"Right operand for in-array predicate must be a basic plural type expression, but found: " + arrayExpressible.getSqmType(),
3500-
query
3501-
);
3497+
if ( arrayExpressible != null ) {
3498+
if ( !(arrayExpressible.getSqmType() instanceof BasicPluralType<?, ?> pluralType) ) {
3499+
throw new SemanticException(
3500+
"Right operand for in-array predicate must be a basic plural type expression, but found: " + arrayExpressible.getSqmType(),
3501+
query
3502+
);
3503+
}
3504+
testExpression.applyInferableType( pluralType.getElementType() );
35023505
}
35033506
final SelfRenderingSqmFunction<Boolean> contains = getFunctionDescriptor( "array_contains" ).generateSqmExpression(
35043507
asList( arrayExpr, testExpression ),

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.hibernate.testing.orm.junit.BootstrapServiceRegistry;
1818
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
1919
import org.hibernate.testing.orm.junit.DomainModel;
20+
import org.hibernate.testing.orm.junit.JiraKey;
2021
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
2122
import org.hibernate.testing.orm.junit.SessionFactory;
2223
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@@ -82,6 +83,24 @@ public void testUnnest(SessionFactoryScope scope) {
8283
} );
8384
}
8485

86+
@Test
87+
@JiraKey( "HHH-18851" )
88+
public void testInArray(SessionFactoryScope scope) {
89+
scope.inSession( em -> {
90+
List<Tuple> results = em.createQuery(
91+
"select e.id " +
92+
"from EntityWithArrays e " +
93+
"where :p in e.theArray",
94+
Tuple.class
95+
)
96+
.setParameter( "p", "abc" )
97+
.getResultList();
98+
99+
assertEquals( 1, results.size() );
100+
assertEquals( 2L, results.get( 0 ).get( 0 ) );
101+
} );
102+
}
103+
85104
@Test
86105
@SkipForDialect(dialectClass = SybaseASEDialect.class, reason = "xmltable can't be used with a left join")
87106
public void testNodeBuilderUnnest(SessionFactoryScope scope) {

0 commit comments

Comments
 (0)