From c9df6af30a8f30d7951a8f79e833a1065d39121c Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Fri, 25 Nov 2022 14:58:00 +0000 Subject: [PATCH] HHH-15761 Extract creation of execution context in ConcreteSqmSelectQueryPlan --- .../internal/ConcreteSqmSelectQueryPlan.java | 60 +++++++++++-------- .../query/sqm/internal/QuerySqmImpl.java | 52 ++++++++-------- 2 files changed, 62 insertions(+), 50 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java index 683dd0f6bc..e339b9bd3e 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java @@ -113,25 +113,7 @@ public class ConcreteSqmSelectQueryPlan implements SelectQueryPlan { return session.getFactory().getJdbcServices().getJdbcSelectExecutor().list( jdbcSelect, jdbcParameterBindings, - new SqmJdbcExecutionContextAdapter( executionContext, jdbcSelect ) { - @Override - public void registerLoadingEntityEntry(EntityKey entityKey, LoadingEntityEntry entry) { - subSelectFetchKeyHandler.addKey( entityKey, entry ); - } - - @Override - public String getQueryIdentifier(String sql) { - if ( CRITERIA_HQL_STRING.equals( hql ) ) { - return "[CRITERIA] " + sql; - } - return hql; - } - - @Override - public boolean hasQueryExecutionToBeAddedToStatistics() { - return true; - } - }, + listInterpreterExecutionContext( hql, executionContext, jdbcSelect, subSelectFetchKeyHandler ), rowTransformer, uniqueSemantic ); @@ -179,6 +161,32 @@ public class ConcreteSqmSelectQueryPlan implements SelectQueryPlan { // `#performList` and `#performScroll`. } + protected static SqmJdbcExecutionContextAdapter listInterpreterExecutionContext( + String hql, + DomainQueryExecutionContext executionContext, + JdbcOperationQuerySelect jdbcSelect, + SubselectFetch.RegistrationHandler subSelectFetchKeyHandler) { + return new SqmJdbcExecutionContextAdapter( executionContext, jdbcSelect ) { + @Override + public void registerLoadingEntityEntry(EntityKey entityKey, LoadingEntityEntry entry) { + subSelectFetchKeyHandler.addKey( entityKey, entry ); + } + + @Override + public String getQueryIdentifier(String sql) { + if ( CRITERIA_HQL_STRING.equals( hql ) ) { + return "[CRITERIA] " + sql; + } + return hql; + } + + @Override + public boolean hasQueryExecutionToBeAddedToStatistics() { + return true; + } + }; + } + private static boolean containsCollectionFetches(QueryOptions queryOptions) { final AppliedGraph appliedGraph = queryOptions.getAppliedGraph(); return appliedGraph != null && appliedGraph.getGraph() != null && containsCollectionFetches( appliedGraph.getGraph() ); @@ -199,9 +207,9 @@ public class ConcreteSqmSelectQueryPlan implements SelectQueryPlan { } @SuppressWarnings("unchecked") - private RowTransformer determineRowTransformer( + protected static RowTransformer determineRowTransformer( SqmSelectStatement sqm, - Class resultType, + Class resultType, TupleMetadata tupleMetadata, QueryOptions queryOptions) { if ( queryOptions.getTupleTransformer() != null ) { @@ -213,7 +221,7 @@ public class ConcreteSqmSelectQueryPlan implements SelectQueryPlan { } if ( resultType.isArray() ) { - return (RowTransformer) RowTransformerArrayImpl.instance(); + return (RowTransformer) RowTransformerArrayImpl.instance(); } // NOTE : if we get here : @@ -224,7 +232,7 @@ public class ConcreteSqmSelectQueryPlan implements SelectQueryPlan { if ( tupleMetadata != null ) { // resultType is Tuple.. if ( queryOptions.getTupleTransformer() == null ) { - return (RowTransformer) new RowTransformerJpaTupleImpl( tupleMetadata ); + return (RowTransformer) new RowTransformerJpaTupleImpl( tupleMetadata ); } throw new IllegalArgumentException( @@ -243,7 +251,7 @@ public class ConcreteSqmSelectQueryPlan implements SelectQueryPlan { } } - private RowTransformer makeRowTransformerTupleTransformerAdapter( + private static RowTransformer makeRowTransformerTupleTransformerAdapter( SqmSelectStatement sqm, QueryOptions queryOptions) { final List aliases = new ArrayList<>(); @@ -261,8 +269,8 @@ public class ConcreteSqmSelectQueryPlan implements SelectQueryPlan { @SuppressWarnings("unchecked") - TupleTransformer tupleTransformer = (TupleTransformer) queryOptions.getTupleTransformer(); - return new RowTransformerTupleTransformerAdapter( + TupleTransformer tupleTransformer = (TupleTransformer) queryOptions.getTupleTransformer(); + return new RowTransformerTupleTransformerAdapter( ArrayHelper.toStringArray( aliases ), tupleTransformer ); diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java index 1c2bfd70cb..56776a7f39 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java @@ -516,6 +516,33 @@ public class QuerySqmImpl final boolean needsDistinct = containsCollectionFetches && ( sqmStatement.usesDistinct() || hasAppliedGraph( getQueryOptions() ) || hasLimit ); + final List list = resolveSelectQueryPlan() + .performList( executionContextFordoList( containsCollectionFetches, hasLimit, needsDistinct ) ); + + if ( needsDistinct ) { + final int first = !hasLimit || getQueryOptions().getLimit().getFirstRow() == null + ? getIntegerLiteral( sqmStatement.getOffset(), 0 ) + : getQueryOptions().getLimit().getFirstRow(); + final int max = !hasLimit || getQueryOptions().getLimit().getMaxRows() == null + ? getMaxRows( sqmStatement, list.size() ) + : getQueryOptions().getLimit().getMaxRows(); + if ( first > 0 || max != -1 ) { + final int toIndex; + final int resultSize = list.size(); + if ( max != -1 ) { + toIndex = first + max; + } + else { + toIndex = resultSize; + } + return list.subList( first, toIndex > resultSize ? resultSize : toIndex ); + } + } + return list; + + } + + protected DomainQueryExecutionContext executionContextFordoList(boolean containsCollectionFetches, boolean hasLimit, boolean needsDistinct) { final DomainQueryExecutionContext executionContextToUse; if ( hasLimit && containsCollectionFetches ) { boolean fail = getSessionFactory().getSessionFactoryOptions().isFailOnPaginationOverCollectionFetchEnabled(); @@ -570,30 +597,7 @@ public class QuerySqmImpl executionContextToUse = this; } } - - final List list = resolveSelectQueryPlan().performList( executionContextToUse ); - - if ( needsDistinct ) { - final int first = !hasLimit || getQueryOptions().getLimit().getFirstRow() == null - ? getIntegerLiteral( sqmStatement.getOffset(), 0 ) - : getQueryOptions().getLimit().getFirstRow(); - final int max = !hasLimit || getQueryOptions().getLimit().getMaxRows() == null - ? getMaxRows( sqmStatement, list.size() ) - : getQueryOptions().getLimit().getMaxRows(); - if ( first > 0 || max != -1 ) { - final int toIndex; - final int resultSize = list.size(); - if ( max != -1 ) { - toIndex = first + max; - } - else { - toIndex = resultSize; - } - return list.subList( first, toIndex > resultSize ? resultSize : toIndex ); - } - } - return list; - + return executionContextToUse; } public static QueryOptions uniqueSemanticQueryOptions(QueryOptions originalOptions) {