Skip to content

Commit b5178d0

Browse files
committed
HHH-18851 Fix parameter type inference issue when IN predicate is uses array_contains()
1 parent a26e505 commit b5178d0

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
@@ -47,10 +47,15 @@ public MappingModelExpressible<?> resolveFunctionArgumentType(
4747
}
4848
}
4949
else if ( argumentIndex == 1 ) {
50+
final SqmTypedNode<?> nodeToResolve = function.getArguments().get( 1 );
51+
if ( nodeToResolve.getExpressible() instanceof MappingModelExpressible<?> ) {
52+
// If the node already has suitable type, don't infer it to be treated as an array
53+
return null;
54+
}
5055
final SqmTypedNode<?> node = function.getArguments().get( 0 );
5156
if ( node instanceof SqmExpression<?> ) {
5257
final MappingModelExpressible<?> expressible = converter.determineValueMapping( (SqmExpression<?>) node );
53-
if ( expressible != null ) {
58+
if ( expressible instanceof BasicPluralType<?, ?> ) {
5459
return expressible;
5560
}
5661
}

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

28872887
final SqmExpression<?> arrayExpr = (SqmExpression<?>) arrayInListContext.expression().accept( this );
28882888
final SqmExpressible<?> arrayExpressible = arrayExpr.getExpressible();
2889-
if ( arrayExpressible != null && !( arrayExpressible.getSqmType() instanceof BasicPluralType<?, ?>) ) {
2890-
throw new SemanticException(
2891-
"Right operand for in-array predicate must be a basic plural type expression, but found: " + arrayExpressible.getSqmType(),
2892-
query
2893-
);
2889+
if ( arrayExpressible != null ) {
2890+
if ( !(arrayExpressible.getSqmType() instanceof BasicPluralType<?, ?>) ) {
2891+
throw new SemanticException(
2892+
"Right operand for in-array predicate must be a basic plural type expression, but found: " + arrayExpressible.getSqmType(),
2893+
query
2894+
);
2895+
}
2896+
testExpression.applyInferableType( ( (BasicPluralType<?, ?>) arrayExpressible.getSqmType() ).getElementType() );
28942897
}
28952898
final SelfRenderingSqmFunction<Boolean> contains = getFunctionDescriptor( "array_contains" ).generateSqmExpression(
28962899
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
@@ -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;
@@ -157,4 +158,22 @@ public void testInSyntax(SessionFactoryScope scope) {
157158
} );
158159
}
159160

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

0 commit comments

Comments
 (0)