HHH-15761 Extract creation of execution context in ConcreteSqmSelectQueryPlan

This commit is contained in:
Davide D'Alto 2022-11-25 14:58:00 +00:00 committed by Davide D'Alto
parent 5bdd79baf1
commit c9df6af30a
2 changed files with 62 additions and 50 deletions

View File

@ -113,25 +113,7 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
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<R> implements SelectQueryPlan<R> {
// `#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<R> implements SelectQueryPlan<R> {
}
@SuppressWarnings("unchecked")
private RowTransformer<R> determineRowTransformer(
protected static <T> RowTransformer<T> determineRowTransformer(
SqmSelectStatement<?> sqm,
Class<R> resultType,
Class<T> resultType,
TupleMetadata tupleMetadata,
QueryOptions queryOptions) {
if ( queryOptions.getTupleTransformer() != null ) {
@ -213,7 +221,7 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
}
if ( resultType.isArray() ) {
return (RowTransformer<R>) RowTransformerArrayImpl.instance();
return (RowTransformer<T>) RowTransformerArrayImpl.instance();
}
// NOTE : if we get here :
@ -224,7 +232,7 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
if ( tupleMetadata != null ) {
// resultType is Tuple..
if ( queryOptions.getTupleTransformer() == null ) {
return (RowTransformer<R>) new RowTransformerJpaTupleImpl( tupleMetadata );
return (RowTransformer<T>) new RowTransformerJpaTupleImpl( tupleMetadata );
}
throw new IllegalArgumentException(
@ -243,7 +251,7 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
}
}
private RowTransformer<R> makeRowTransformerTupleTransformerAdapter(
private static <T> RowTransformer<T> makeRowTransformerTupleTransformerAdapter(
SqmSelectStatement<?> sqm,
QueryOptions queryOptions) {
final List<String> aliases = new ArrayList<>();
@ -261,8 +269,8 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
@SuppressWarnings("unchecked")
TupleTransformer<R> tupleTransformer = (TupleTransformer<R>) queryOptions.getTupleTransformer();
return new RowTransformerTupleTransformerAdapter<R>(
TupleTransformer<T> tupleTransformer = (TupleTransformer<T>) queryOptions.getTupleTransformer();
return new RowTransformerTupleTransformerAdapter<T>(
ArrayHelper.toStringArray( aliases ),
tupleTransformer
);

View File

@ -516,6 +516,33 @@ public class QuerySqmImpl<R>
final boolean needsDistinct = containsCollectionFetches
&& ( sqmStatement.usesDistinct() || hasAppliedGraph( getQueryOptions() ) || hasLimit );
final List<R> 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<R>
executionContextToUse = this;
}
}
final List<R> 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) {