HHH-14109 Use 'in expression count limit' if calculated count is greather than 'in expression count limit'

This commit is contained in:
Oliver Breidenbach 2020-07-17 12:17:17 +02:00 committed by Sanne Grinovero
parent 84b3167f26
commit e8f310a659
2 changed files with 31 additions and 6 deletions

View File

@ -538,7 +538,11 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings {
if ( inClauseParameterPaddingEnabled ) { if ( inClauseParameterPaddingEnabled ) {
int bindValuePaddingCount = MathHelper.ceilingPowerOfTwo( bindValueCount ); int bindValuePaddingCount = MathHelper.ceilingPowerOfTwo( bindValueCount );
if ( bindValueCount < bindValuePaddingCount && (inExprLimit == 0 || bindValuePaddingCount < inExprLimit) ) { if ( inExprLimit > 0 && bindValuePaddingCount > inExprLimit ) {
bindValuePaddingCount = inExprLimit;
}
if ( bindValueCount < bindValuePaddingCount ) {
bindValueMaxCount = bindValuePaddingCount; bindValueMaxCount = bindValuePaddingCount;
} }
} }

View File

@ -74,11 +74,8 @@ public class MaxInExpressionParameterPaddingTest extends BaseEntityManagerFuncti
sqlStatementInterceptor.clear(); sqlStatementInterceptor.clear();
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
return entityManager.createQuery( return entityManager.createQuery( "select p from Person p where p.id in :ids" )
"select p " + .setParameter( "ids", IntStream.range( 0, MAX_COUNT ).boxed().collect( Collectors.toList() ) )
"from Person p " +
"where p.id in :ids" )
.setParameter( "ids", IntStream.range( 0, MAX_COUNT ).boxed().collect(Collectors.toList()) )
.getResultList(); .getResultList();
} ); } );
@ -89,6 +86,30 @@ public class MaxInExpressionParameterPaddingTest extends BaseEntityManagerFuncti
} }
expectedInClause.append( ")" ); expectedInClause.append( ")" );
assertTrue( sqlStatementInterceptor.getSqlQueries().get( 0 ).endsWith( expectedInClause.toString() ) );
}
@TestForIssue( jiraKey = "HHH-14109" )
@Test
public void testInClauseParameterPaddingToLimit() {
sqlStatementInterceptor.clear();
doInJPA( this::entityManagerFactory, entityManager -> {
return entityManager.createQuery(
"select p " +
"from Person p " +
"where p.id in :ids" )
.setParameter( "ids", IntStream.range( 0, 10 ).boxed().collect(Collectors.toList()) )
.getResultList();
} );
StringBuilder expectedInClause = new StringBuilder();
expectedInClause.append( "in (?" );
for ( int i = 1; i < MAX_COUNT; i++ ) {
expectedInClause.append( " , ?" );
}
expectedInClause.append( ")" );
assertTrue(sqlStatementInterceptor.getSqlQueries().get( 0 ).endsWith( expectedInClause.toString() )); assertTrue(sqlStatementInterceptor.getSqlQueries().get( 0 ).endsWith( expectedInClause.toString() ));
} }