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;
}
Aggregations