Skip to content

Commit

Permalink
HHH-14977 - @where is broken in 6.0
Browse files Browse the repository at this point in the history
Next iteration where `@Where` fragments generate AST one or more `WhereFilterPredicate` instances.

At the moment, `@Filter` fragments are collected together using the existing String-manipulation style and still collected into a single `FilterPredicate`.  Next step is to make that more AST-centric and hopefully get rid of the String-manip-based methods
  • Loading branch information
sebersole committed Dec 15, 2021
1 parent c5c2434 commit 579b3f0
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

import org.hibernate.Filter;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.persister.collection.AbstractCollectionPersister;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.Joinable;
import org.hibernate.sql.Template;
Expand Down Expand Up @@ -163,6 +161,33 @@ public static FilterPredicate createFilterPredicate(
return createFilterPredicate( loadQueryInfluencers, joinable, rootTableGroup, true );
}

public static FilterPredicate createFilterPredicate(
Map<String,Filter> enabledFilters,
Joinable joinable,
TableGroup rootTableGroup) {
return createFilterPredicate( enabledFilters, joinable, rootTableGroup, true );
}

public static FilterPredicate createFilterPredicate(
Map<String,Filter> enabledFilters,
Joinable joinable,
TableGroup rootTableGroup,
boolean useIdentificationVariable) {
final String filterFragment = joinable.filterFragment(
rootTableGroup,
enabledFilters,
Collections.emptySet(),
useIdentificationVariable
);
if ( StringHelper.isNotEmpty( filterFragment ) ) {
return doCreateFilterPredicate( filterFragment, enabledFilters );
}
else {
return null;
}

}

public static FilterPredicate createFilterPredicate(
LoadQueryInfluencers loadQueryInfluencers,
Joinable joinable,
Expand Down Expand Up @@ -193,7 +218,7 @@ public static FilterPredicate createManyToManyFilterPredicate(LoadQueryInfluence
}
}

private static FilterPredicate doCreateFilterPredicate(String filterFragment, Map<String, Filter> enabledFilters) {
public static FilterPredicate doCreateFilterPredicate(String filterFragment, Map<String, Filter> enabledFilters) {
final Matcher matcher = FILTER_PARAMETER_PATTERN.matcher( filterFragment );
final StringBuilder sb = new StringBuilder();
int pos = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -21,16 +20,15 @@
import org.hibernate.collection.spi.BagSemantics;
import org.hibernate.engine.FetchStyle;
import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.spi.SubselectFetch;
import org.hibernate.engine.profile.FetchProfile;
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.CascadingAction;
import org.hibernate.engine.spi.EffectiveEntityGraph;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SubselectFetch;
import org.hibernate.graph.GraphSemantic;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.internal.FilterHelper;
import org.hibernate.loader.ast.spi.Loadable;
import org.hibernate.loader.ast.spi.Loader;
import org.hibernate.metamodel.mapping.AttributeMapping;
Expand Down Expand Up @@ -70,7 +68,6 @@
import org.hibernate.sql.ast.tree.predicate.ComparisonPredicate;
import org.hibernate.sql.ast.tree.predicate.InListPredicate;
import org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate;
import org.hibernate.sql.ast.tree.predicate.Predicate;
import org.hibernate.sql.ast.tree.select.QueryPart;
import org.hibernate.sql.ast.tree.select.QuerySpec;
import org.hibernate.sql.ast.tree.select.SelectStatement;
Expand Down Expand Up @@ -480,7 +477,7 @@ private SelectStatement generateSelect() {
applyOrdering( rootTableGroup, pluralAttributeMapping );
}
else if ( loadable instanceof Joinable ) {
applyFiltering( rootQuerySpec, rootTableGroup, (Joinable) loadable );
applyFiltering( rootQuerySpec, rootTableGroup, (Joinable) loadable, sqlAstCreationState.getFromClauseAccess() );
}

if ( orderByFragments != null ) {
Expand Down Expand Up @@ -589,57 +586,30 @@ private void applyFiltering(
PluralAttributeMapping pluralAttributeMapping,
FromClauseAccess fromClauseAccess) {
final CollectionPersister collectionPersister = pluralAttributeMapping.getCollectionDescriptor();
final Joinable joinable = collectionPersister.getCollectionType()
.getAssociatedJoinable( creationContext.getSessionFactory() );
final Predicate filterPredicate = FilterHelper.createFilterPredicate(
loadQueryInfluencers,
joinable,
tableGroup
final Joinable joinable = collectionPersister.getCollectionType().getAssociatedJoinable( creationContext.getSessionFactory() );
joinable.applyRestrictions(
querySpec,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
fromClauseAccess
);
if ( filterPredicate != null ) {
querySpec.applyPredicate( filterPredicate );
}
if ( collectionPersister.isManyToMany() ) {
assert joinable instanceof CollectionPersister;
final Predicate manyToManyFilterPredicate = FilterHelper.createManyToManyFilterPredicate(
loadQueryInfluencers,
(CollectionPersister) joinable,
tableGroup
);
if ( manyToManyFilterPredicate != null ) {
final NavigablePath parentNavigablePath = tableGroup.getNavigablePath().getParent();
if ( parentNavigablePath == null ) {
querySpec.applyPredicate( manyToManyFilterPredicate );
}
else {
final TableGroup parentTableGroup = fromClauseAccess.getTableGroup( parentNavigablePath );
TableGroupJoin pluralTableGroupJoin = null;
for ( TableGroupJoin nestedTableGroupJoin : parentTableGroup.getTableGroupJoins() ) {
if ( nestedTableGroupJoin.getNavigablePath() == tableGroup.getNavigablePath() ) {
pluralTableGroupJoin = nestedTableGroupJoin;
break;
}
}

assert pluralTableGroupJoin != null;
pluralTableGroupJoin.applyPredicate( manyToManyFilterPredicate );
}
}
}
}

private void applyFiltering(
QuerySpec querySpec,
TableGroup tableGroup,
Joinable joinable) {
final Predicate filterPredicate = FilterHelper.createFilterPredicate(
loadQueryInfluencers,
joinable,
tableGroup
Joinable joinable,
FromClauseAccess fromClauseAccess) {
joinable.applyRestrictions(
querySpec,
tableGroup,
true,
loadQueryInfluencers.getEnabledFilters(),
null,
fromClauseAccess
);
if ( filterPredicate != null ) {
querySpec.applyPredicate( filterPredicate );
}
}

private void applyOrdering(TableGroup tableGroup, PluralAttributeMapping pluralAttributeMapping) {
Expand Down

0 comments on commit 579b3f0

Please sign in to comment.