Skip to content

Commit

Permalink
HHH-16984 Disable use of arrays for batch and multi-loader on H2
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Jul 24, 2023
1 parent 90eb697 commit eb562d5
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ public boolean supportsStandardArrays() {
return getVersion().isSameOrAfter( 2 );
}

@Override
public boolean useArrayForMultiValuedParameters() {
// Performance is worse than the in-predicate version
return false;
}

@Override
protected String columnType(int sqlTypeCode) {
switch ( sqlTypeCode ) {
Expand Down
11 changes: 11 additions & 0 deletions hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -4204,6 +4204,17 @@ public boolean supportsStandardArrays() {
return false;
}

/**
* Does this database prefer to use array types for multi-valued parameters.
*
* @return boolean
*
* @since 6.3
*/
public boolean useArrayForMultiValuedParameters() {
return supportsStandardArrays() && getPreferredSqlTypeCodeForArray() == SqlTypes.ARRAY;
}

/**
* The SQL type name for the array type with elements of the given type name.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ public boolean supportsStandardArrays() {
return getVersion().isSameOrAfter( 2 );
}

@Override
public boolean useArrayForMultiValuedParameters() {
// Performance is worse than the in-predicate version
return false;
}

@Override
protected String columnType(int sqlTypeCode) {
switch ( sqlTypeCode ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ private MultiKeyLoadHelper() {
}

public static boolean supportsSqlArrayType(Dialect dialect) {
return dialect.supportsStandardArrays()
&& dialect.getPreferredSqlTypeCodeForArray() == SqlTypes.ARRAY;
return dialect.useArrayForMultiValuedParameters();
}

public static JdbcMapping resolveArrayJdbcMapping(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public HqlInterpretation resolveHqlInterpretation(
Class<?> expectedResultType,
HqlTranslator translator) {
log.tracef( "QueryPlan#resolveHqlInterpretation( `%s` )", queryString );
final StatisticsImplementor statistics = statisticsSupplier.get();

final Object cacheKey;
if ( expectedResultType != null ) {
Expand All @@ -130,30 +131,35 @@ public HqlInterpretation resolveHqlInterpretation(
}
final HqlInterpretation existing = hqlInterpretationCache.get( cacheKey );
if ( existing != null ) {
final StatisticsImplementor statistics = statisticsSupplier.get();
if ( statistics.isStatisticsEnabled() ) {
statistics.queryPlanCacheHit( queryString );
}
return existing;
}
else {
final HqlInterpretation hqlInterpretation = createHqlInterpretation(
queryString,
expectedResultType,
translator,
statisticsSupplier
);
hqlInterpretationCache.put( cacheKey, hqlInterpretation );
return hqlInterpretation;
else if ( expectedResultType != null ) {
final HqlInterpretation existingQueryOnly = hqlInterpretationCache.get( queryString );
if ( existingQueryOnly != null ) {
if ( statistics.isStatisticsEnabled() ) {
statistics.queryPlanCacheHit( queryString );
}
return existingQueryOnly;
}
}
final HqlInterpretation hqlInterpretation = createHqlInterpretation(
queryString,
expectedResultType,
translator,
statistics
);
hqlInterpretationCache.put( cacheKey, hqlInterpretation );
return hqlInterpretation;
}

protected static HqlInterpretation createHqlInterpretation(
String queryString,
Class<?> expectedResultType,
HqlTranslator translator,
Supplier<StatisticsImplementor> statisticsSupplier) {
final StatisticsImplementor statistics = statisticsSupplier.get();
StatisticsImplementor statistics) {
final boolean stats = statistics.isStatisticsEnabled();
final long startTime = stats ? System.nanoTime() : 0L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void testSelectChild(SessionFactoryScope scope){
}
statementInspector.assertExecutedCount( 2 );

if ( scope.getSessionFactory().getJdbcServices().getDialect().supportsStandardArrays() ) {
if ( scope.getSessionFactory().getJdbcServices().getDialect().useArrayForMultiValuedParameters() ) {
Assertions.assertThat( statementInspector.getSqlQueries().get( 0 ) ).containsOnlyOnce( "?" );
Assertions.assertThat( statementInspector.getSqlQueries().get( 1 ) ).containsOnlyOnce( "?" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void testSelectChild(SessionFactoryScope scope){
statementInspector.clear();
List<Child> children = session.createQuery( "select c from Child c", Child.class ).getResultList();
statementInspector.assertExecutedCount( 3 );
if ( scope.getSessionFactory().getJdbcServices().getDialect().supportsStandardArrays() ) {
if ( scope.getSessionFactory().getJdbcServices().getDialect().useArrayForMultiValuedParameters() ) {
Assertions.assertThat( statementInspector.getSqlQueries().get( 1 ) ).containsOnlyOnce( "?" );
Assertions.assertThat( statementInspector.getSqlQueries().get( 2 ) ).containsOnlyOnce( "?" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void testSelectChild(SessionFactoryScope scope) {
statementInspector.clear();
List<Child> children = session.createQuery( "select c from Child c", Child.class ).getResultList();
statementInspector.assertExecutedCount( 3 );
if ( scope.getSessionFactory().getJdbcServices().getDialect().supportsStandardArrays() ) {
if ( scope.getSessionFactory().getJdbcServices().getDialect().useArrayForMultiValuedParameters() ) {
Assertions.assertThat( statementInspector.getSqlQueries().get( 1 ) ).containsOnlyOnce( "?" );
Assertions.assertThat( statementInspector.getSqlQueries().get( 2 ) ).containsOnlyOnce( "?" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void testSelectChild(SessionFactoryScope scope) {
}
statementInspector.assertExecutedCount( 2 );

if ( scope.getSessionFactory().getJdbcServices().getDialect().supportsStandardArrays() ) {
if ( scope.getSessionFactory().getJdbcServices().getDialect().useArrayForMultiValuedParameters() ) {
Assertions.assertThat( statementInspector.getSqlQueries().get( 0 ) ).containsOnlyOnce( "?" );
Assertions.assertThat( statementInspector.getSqlQueries().get( 1 ) ).containsOnlyOnce( "?" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void testSelectChild(SessionFactoryScope scope) {
statementInspector.clear();
List<Child> children = session.createQuery( "select c from Child c", Child.class ).getResultList();
statementInspector.assertExecutedCount( 3 );
if ( scope.getSessionFactory().getJdbcServices().getDialect().supportsStandardArrays() ) {
if ( scope.getSessionFactory().getJdbcServices().getDialect().useArrayForMultiValuedParameters() ) {
assertThat( statementInspector.getSqlQueries().get( 1 ) ).containsOnlyOnce( "?" );
assertThat( statementInspector.getSqlQueries().get( 2 ) ).containsOnlyOnce( "?" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public void after(SessionFactoryScope scope) {
@TestForIssue(jiraKey = "HHH-12740")
public void testSubselect(SessionFactoryScope scope) {
final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
final Dialect dialect = scope.getSessionFactory().getFastSessionServices().jdbcServices.getDialect();
statementInspector.clear();

scope.inTransaction(
Expand All @@ -117,7 +118,12 @@ public void testSubselect(SessionFactoryScope scope) {
assertEquals( 56, list.size() );

// None of the collections should be loaded yet
assertThat( statementInspector.getSqlQueries() ).hasSize( 1 );
if ( dialect.useArrayForMultiValuedParameters() ) {
assertThat( statementInspector.getSqlQueries() ).hasSize( 1 );
}
else {
assertThat( statementInspector.getSqlQueries() ).hasSize( 2 );
}
for ( Parent p : list ) {
assertFalse( Hibernate.isInitialized( p.children ) );
}
Expand All @@ -128,8 +134,7 @@ public void testSubselect(SessionFactoryScope scope) {
Hibernate.initialize( list.get( 0 ).children );

// exactly how depends on whether the Dialect supports use of SQL ARRAY
final Dialect dialect = scope.getSessionFactory().getFastSessionServices().jdbcServices.getDialect();
if ( MultiKeyLoadHelper.supportsSqlArrayType( dialect ) ) {
if ( dialect.useArrayForMultiValuedParameters() ) {
assertThat( Hibernate.isInitialized( list.get( 0 ).children ) ).isTrue();
assertThat( Hibernate.isInitialized( list.get( 50 ).children ) ).isTrue();
assertThat( Hibernate.isInitialized( list.get( 52 ).children ) ).isTrue();
Expand Down

0 comments on commit eb562d5

Please sign in to comment.