Search in sources :

Example 1 with SqmOrPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmOrPredicate in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitOrPredicate.

@Override
public Junction visitOrPredicate(SqmOrPredicate predicate) {
    final Junction disjunction = new Junction(Junction.Nature.DISJUNCTION, getBooleanType());
    final Predicate predicate1 = (Predicate) predicate.getLeftHandPredicate().accept(this);
    final Map<SqmPath<?>, Set<String>> conjunctTreatUsages1;
    if (conjunctTreatUsages.isEmpty()) {
        conjunctTreatUsages1 = null;
    } else {
        conjunctTreatUsages1 = new IdentityHashMap<>(conjunctTreatUsages);
        conjunctTreatUsages.clear();
    }
    final Predicate predicate2 = (Predicate) predicate.getRightHandPredicate().accept(this);
    if (conjunctTreatUsages.isEmpty() || conjunctTreatUsages1 == null) {
        disjunction.add(SqlAstTreeHelper.combinePredicates(consumeConjunctTreatTypeRestrictions(conjunctTreatUsages1), predicate1));
        disjunction.add(predicate2);
    } else {
        // If both disjunctions have treat type restrictions build the intersection of the two,
        // so that we can push that up and infer during pruning, which entity subclasses can be omitted
        final Map<SqmPath<?>, Set<String>> conjunctTreatUsages2 = new IdentityHashMap<>(conjunctTreatUsages);
        final Iterator<Map.Entry<SqmPath<?>, Set<String>>> iterator = conjunctTreatUsages.entrySet().iterator();
        while (iterator.hasNext()) {
            final Map.Entry<SqmPath<?>, Set<String>> entry = iterator.next();
            final Set<String> entityNames1 = conjunctTreatUsages1.get(entry.getKey());
            if (entityNames1 == null) {
                iterator.remove();
                continue;
            }
            // Intersect the two sets and transfer the common elements to the intersection
            final Set<String> entityNames2 = entry.getValue();
            final Set<String> intersected = new HashSet<>(entityNames1);
            intersected.retainAll(entityNames2);
            if (intersected.isEmpty()) {
                iterator.remove();
                continue;
            }
            entry.setValue(intersected);
            entityNames1.removeAll(intersected);
            entityNames2.removeAll(intersected);
            if (entityNames1.isEmpty()) {
                conjunctTreatUsages1.remove(entry.getKey());
            }
            if (entityNames2.isEmpty()) {
                conjunctTreatUsages2.remove(entry.getKey());
            }
        }
        disjunction.add(SqlAstTreeHelper.combinePredicates(consumeConjunctTreatTypeRestrictions(conjunctTreatUsages1), predicate1));
        disjunction.add(SqlAstTreeHelper.combinePredicates(consumeConjunctTreatTypeRestrictions(conjunctTreatUsages2), predicate2));
    }
    return disjunction;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) IdentityHashMap(java.util.IdentityHashMap) SelfInterpretingSqmPath(org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) DiscriminatorSqmPath(org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SqmBooleanExpressionPredicate(org.hibernate.query.sqm.tree.predicate.SqmBooleanExpressionPredicate) SelfRenderingPredicate(org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) LikePredicate(org.hibernate.sql.ast.tree.predicate.LikePredicate) BetweenPredicate(org.hibernate.sql.ast.tree.predicate.BetweenPredicate) SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) SqmNegatedPredicate(org.hibernate.query.sqm.tree.predicate.SqmNegatedPredicate) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) SqmAndPredicate(org.hibernate.query.sqm.tree.predicate.SqmAndPredicate) SqmMemberOfPredicate(org.hibernate.query.sqm.tree.predicate.SqmMemberOfPredicate) SqmLikePredicate(org.hibernate.query.sqm.tree.predicate.SqmLikePredicate) SqmExistsPredicate(org.hibernate.query.sqm.tree.predicate.SqmExistsPredicate) SqmOrPredicate(org.hibernate.query.sqm.tree.predicate.SqmOrPredicate) SqmNullnessPredicate(org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmGroupedPredicate(org.hibernate.query.sqm.tree.predicate.SqmGroupedPredicate) SqmInListPredicate(org.hibernate.query.sqm.tree.predicate.SqmInListPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) SqmBetweenPredicate(org.hibernate.query.sqm.tree.predicate.SqmBetweenPredicate) GroupedPredicate(org.hibernate.sql.ast.tree.predicate.GroupedPredicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) SqmInSubQueryPredicate(org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate) SqmEmptinessPredicate(org.hibernate.query.sqm.tree.predicate.SqmEmptinessPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) Junction(org.hibernate.sql.ast.tree.predicate.Junction) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) AbstractMap(java.util.AbstractMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Aggregations

AbstractMap (java.util.AbstractMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 DiscriminatorSqmPath (org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath)1 SelfInterpretingSqmPath (org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath)1 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)1 SqmAndPredicate (org.hibernate.query.sqm.tree.predicate.SqmAndPredicate)1 SqmBetweenPredicate (org.hibernate.query.sqm.tree.predicate.SqmBetweenPredicate)1 SqmBooleanExpressionPredicate (org.hibernate.query.sqm.tree.predicate.SqmBooleanExpressionPredicate)1 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)1 SqmEmptinessPredicate (org.hibernate.query.sqm.tree.predicate.SqmEmptinessPredicate)1 SqmExistsPredicate (org.hibernate.query.sqm.tree.predicate.SqmExistsPredicate)1 SqmGroupedPredicate (org.hibernate.query.sqm.tree.predicate.SqmGroupedPredicate)1 SqmInListPredicate (org.hibernate.query.sqm.tree.predicate.SqmInListPredicate)1 SqmInSubQueryPredicate (org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate)1 SqmLikePredicate (org.hibernate.query.sqm.tree.predicate.SqmLikePredicate)1