Skip to content

Commit

Permalink
HHH-18851 Fix parameter type inference issue when IN predicate is use…
Browse files Browse the repository at this point in the history
…s array_contains()
  • Loading branch information
beikov committed Nov 20, 2024
1 parent a26e505 commit b5178d0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@ public MappingModelExpressible<?> resolveFunctionArgumentType(
}
}
else if ( argumentIndex == 1 ) {
final SqmTypedNode<?> nodeToResolve = function.getArguments().get( 1 );
if ( nodeToResolve.getExpressible() instanceof MappingModelExpressible<?> ) {
// If the node already has suitable type, don't infer it to be treated as an array
return null;
}
final SqmTypedNode<?> node = function.getArguments().get( 0 );
if ( node instanceof SqmExpression<?> ) {
final MappingModelExpressible<?> expressible = converter.determineValueMapping( (SqmExpression<?>) node );
if ( expressible != null ) {
if ( expressible instanceof BasicPluralType<?, ?> ) {
return expressible;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2886,11 +2886,14 @@ else if ( inListContext instanceof HqlParser.ArrayInListContext ) {

final SqmExpression<?> arrayExpr = (SqmExpression<?>) arrayInListContext.expression().accept( this );
final SqmExpressible<?> arrayExpressible = arrayExpr.getExpressible();
if ( arrayExpressible != null && !( arrayExpressible.getSqmType() instanceof BasicPluralType<?, ?>) ) {
throw new SemanticException(
"Right operand for in-array predicate must be a basic plural type expression, but found: " + arrayExpressible.getSqmType(),
query
);
if ( arrayExpressible != null ) {
if ( !(arrayExpressible.getSqmType() instanceof BasicPluralType<?, ?>) ) {
throw new SemanticException(
"Right operand for in-array predicate must be a basic plural type expression, but found: " + arrayExpressible.getSqmType(),
query
);
}
testExpression.applyInferableType( ( (BasicPluralType<?, ?>) arrayExpressible.getSqmType() ).getElementType() );
}
final SelfRenderingSqmFunction<Boolean> contains = getFunctionDescriptor( "array_contains" ).generateSqmExpression(
asList( arrayExpr, testExpression ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hibernate.testing.orm.junit.BootstrapServiceRegistry;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
Expand Down Expand Up @@ -157,4 +158,22 @@ public void testInSyntax(SessionFactoryScope scope) {
} );
}

@Test
@JiraKey( "HHH-18851" )
public void testInArray(SessionFactoryScope scope) {
scope.inSession( em -> {
List<Tuple> results = em.createQuery(
"select e.id " +
"from EntityWithArrays e " +
"where :p in e.theArray",
Tuple.class
)
.setParameter( "p", "abc" )
.getResultList();

assertEquals( 1, results.size() );
assertEquals( 2L, results.get( 0 ).get( 0 ) );
} );
}

}

0 comments on commit b5178d0

Please sign in to comment.