Skip to content

Commit

Permalink
HHH-13077 - Optimize query plan call count
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmihalcea authored and sebersole committed Mar 3, 2021
1 parent 2d5d606 commit ed3bbf1
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,11 @@ public QueryImplementor getNamedQuery(String name) {
throw getExceptionConverter().convert( new IllegalArgumentException( "No query defined for that name [" + name + "]" ) );
}

protected QueryImplementor createQuery(NamedQueryDefinition queryDefinition) {
protected QueryImpl createQuery(NamedQueryDefinition queryDefinition) {
String queryString = queryDefinition.getQueryString();
final QueryImpl query = new QueryImpl(
this,
getQueryPlan( queryString, false ).getParameterMetadata(),
getQueryPlan( queryString, false ),
queryString
);
applyQuerySettingsAndHints( query );
Expand Down Expand Up @@ -723,15 +723,15 @@ protected void initQueryFromNamedDefinition(Query query, NamedQueryDefinition nq
}

@Override
public QueryImplementor createQuery(String queryString) {
public QueryImpl createQuery(String queryString) {
checkOpen();
pulseTransactionCoordinator();
delayedAfterCompletion();

try {
final QueryImpl query = new QueryImpl(
this,
getQueryPlan( queryString, false ).getParameterMetadata(),
getQueryPlan( queryString, false ),
queryString
);
applyQuerySettingsAndHints( query );
Expand Down Expand Up @@ -831,7 +831,7 @@ public <T> QueryImplementor<T> createQuery(String queryString, Class<T> resultCl

try {
// do the translation
final QueryImplementor<T> query = createQuery( queryString );
final QueryImpl<T> query = createQuery( queryString );
resultClassChecking( resultClass, query );
return query;
}
Expand All @@ -841,13 +841,10 @@ public <T> QueryImplementor<T> createQuery(String queryString, Class<T> resultCl
}

@SuppressWarnings({"unchecked", "WeakerAccess", "StatementWithEmptyBody"})
protected void resultClassChecking(Class resultClass, org.hibernate.Query hqlQuery) {
protected void resultClassChecking(Class resultClass, QueryImpl hqlQuery) {
// make sure the query is a select -> HHH-7192
final HQLQueryPlan queryPlan = getFactory().getQueryPlanCache().getHQLQueryPlan(
hqlQuery.getQueryString(),
false,
getLoadQueryInfluencers().getEnabledFilters()
);
HQLQueryPlan queryPlan = hqlQuery.getQueryPlan();

if ( queryPlan.getTranslators()[0].isManipulationStatement() ) {
throw new IllegalArgumentException( "Update/delete queries cannot be typed" );
}
Expand Down Expand Up @@ -923,7 +920,7 @@ protected <T> QueryImplementor<T> buildQueryFromName(String name, Class<T> resu

@SuppressWarnings({"WeakerAccess", "unchecked"})
protected <T> QueryImplementor<T> createQuery(NamedQueryDefinition namedQueryDefinition, Class<T> resultType) {
final QueryImplementor query = createQuery( namedQueryDefinition );
final QueryImpl query = createQuery( namedQueryDefinition );
if ( resultType != null ) {
resultClassChecking( resultType, query );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,7 @@ public Iterator iterate(String query, QueryParameters queryParameters) throws Hi
queryParameters.validateParameters();

HQLQueryPlan plan = queryParameters.getQueryPlan();
if ( plan == null ) {
if ( plan == null || !plan.isShallow() ) {
plan = getQueryPlan( query, true );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
* @author Steve Ebersole
* @author Gavin King
*/
@SuppressWarnings("UnusedDeclaration")
@SuppressWarnings("UnusedDeclaratiqon")
public interface Query<R> extends TypedQuery<R>, org.hibernate.Query<R>, CommonQueryContract {
/**
* Get the QueryProducer this Query originates from.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1434,8 +1434,8 @@ protected QueryParameters makeQueryParametersForExecution(String hql) {
);
}

QueryParameters queryParameters = new QueryParameters(
getQueryParameterBindings(),
QueryParameters queryParameters = new QueryParameters(
getQueryParameterBindings(),
getLockOptions(),
queryOptions,
true,
Expand All @@ -1450,13 +1450,24 @@ protected QueryParameters makeQueryParametersForExecution(String hql) {
optionalId,
resultTransformer
);
queryParameters.setQueryPlan( entityGraphHintedQueryPlan );

appendQueryPlanToQueryParameters( hql, queryParameters, entityGraphHintedQueryPlan );

if ( passDistinctThrough != null ) {
queryParameters.setPassDistinctThrough( passDistinctThrough );
}
return queryParameters;
}

protected void appendQueryPlanToQueryParameters(
String hql,
QueryParameters queryParameters,
HQLQueryPlan queryPlan) {
if ( queryPlan != null ) {
queryParameters.setQueryPlan( queryPlan );
}
}

public QueryParameters getQueryParameters() {
final String expandedQuery = getQueryParameterBindings().expandListValuedParameters( getQueryString(), getProducer() );
return makeQueryParametersForExecution( expandedQuery );
Expand Down Expand Up @@ -1732,7 +1743,7 @@ public Type determineProperBooleanType(int position, Object value, Type defaultT
: defaultType;
}

private boolean isSelect() {
protected boolean isSelect() {
return getProducer().getFactory().getQueryPlanCache()
.getHQLQueryPlan( getQueryString(), false, Collections.<String, Filter>emptyMap() )
.isSelect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
*/
package org.hibernate.query.internal;

import org.hibernate.engine.query.spi.HQLQueryPlan;
import org.hibernate.engine.query.spi.ReturnMetadata;
import org.hibernate.engine.spi.QueryParameters;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.query.ParameterMetadata;
import org.hibernate.query.Query;
import org.hibernate.query.spi.QueryParameterBindings;
import org.hibernate.type.Type;
Expand All @@ -18,16 +20,19 @@
public class QueryImpl<R> extends AbstractProducedQuery<R> implements Query<R> {
private final String queryString;

private final HQLQueryPlan hqlQueryPlan;

private final QueryParameterBindingsImpl queryParameterBindings;

public QueryImpl(
SharedSessionContractImplementor producer,
ParameterMetadata parameterMetadata,
HQLQueryPlan hqlQueryPlan,
String queryString) {
super( producer, parameterMetadata );
super( producer, hqlQueryPlan.getParameterMetadata() );
this.hqlQueryPlan = hqlQueryPlan;
this.queryString = queryString;
this.queryParameterBindings = QueryParameterBindingsImpl.from(
parameterMetadata,
hqlQueryPlan.getParameterMetadata(),
producer.getFactory(),
producer.isQueryParametersValidationEnabled()
);
Expand All @@ -43,19 +48,25 @@ public String getQueryString() {
return queryString;
}

public HQLQueryPlan getQueryPlan() {
return hqlQueryPlan;
}

@Override
protected boolean isNativeQuery() {
return false;
}

@Override
public Type[] getReturnTypes() {
return getProducer().getFactory().getReturnTypes( queryString );
final ReturnMetadata metadata = hqlQueryPlan.getReturnMetadata();
return metadata == null ? null : metadata.getReturnTypes();
}

@Override
public String[] getReturnAliases() {
return getProducer().getFactory().getReturnAliases( queryString );
final ReturnMetadata metadata = hqlQueryPlan.getReturnMetadata();
return metadata == null ? null : metadata.getReturnAliases();
}

@Override
Expand All @@ -67,4 +78,22 @@ public Query setEntity(int position, Object val) {
public Query setEntity(String name, Object val) {
return setParameter( name, val, getProducer().getFactory().getTypeHelper().entity( resolveEntityName( val ) ) );
}

@Override
protected boolean isSelect() {
return hqlQueryPlan.isSelect();
}

@Override
protected void appendQueryPlanToQueryParameters(
String hql,
QueryParameters queryParameters,
HQLQueryPlan queryPlan) {
if ( queryPlan != null ) {
queryParameters.setQueryPlan( queryPlan );
}
else if ( hql.equals( getQueryString() ) ) {
queryParameters.setQueryPlan( getQueryPlan() );
}
}
}
Loading

0 comments on commit ed3bbf1

Please sign in to comment.