use of com.apple.foundationdb.record.query.expressions.OrComponent in project fdb-record-layer by FoundationDB.
the class RecordQueryPlanner method planOr.
@Nullable
private ScoredPlan planOr(@Nonnull PlanContext planContext, @Nonnull OrComponent filter) {
if (filter.getChildren().isEmpty()) {
return null;
}
List<ScoredPlan> subplans = new ArrayList<>(filter.getChildren().size());
boolean allHaveOrderingKey = true;
RecordQueryPlan commonFilteredBasePlan = null;
boolean allHaveSameBasePlan = true;
for (QueryComponent subfilter : filter.getChildren()) {
ScoredPlan subplan = planFilter(planContext, subfilter, true);
if (subplan == null) {
return null;
}
if (subplan.planOrderingKey == null) {
allHaveOrderingKey = false;
}
RecordQueryPlan filteredBasePlan;
if (subplan.plan instanceof RecordQueryFilterPlan) {
filteredBasePlan = ((RecordQueryFilterPlan) subplan.plan).getInnerPlan();
} else {
filteredBasePlan = null;
}
if (subplans.isEmpty()) {
commonFilteredBasePlan = filteredBasePlan;
allHaveSameBasePlan = filteredBasePlan != null;
} else if (allHaveSameBasePlan && !Objects.equals(filteredBasePlan, commonFilteredBasePlan)) {
allHaveSameBasePlan = false;
}
subplans.add(subplan);
}
// out there over the equivalent OR(EQUALS) filters.
if (allHaveSameBasePlan) {
final RecordQueryPlan combinedOrFilter = new RecordQueryFilterPlan(commonFilteredBasePlan, new OrComponent(subplans.stream().map(subplan -> ((RecordQueryFilterPlan) subplan.plan).getConjunctedFilter()).collect(Collectors.toList())));
ScoredPlan firstSubPlan = subplans.get(0);
return new ScoredPlan(combinedOrFilter, Collections.emptyList(), Collections.emptyList(), firstSubPlan.score, firstSubPlan.createsDuplicates, firstSubPlan.includedRankComparisons);
}
// neither removes duplicates nor requires the children be in order.
if (allHaveOrderingKey) {
final ScoredPlan orderedUnionPlan = planOrderedUnion(planContext, subplans);
if (orderedUnionPlan != null) {
return orderedUnionPlan;
}
}
final ScoredPlan unorderedUnionPlan = planUnorderedUnion(planContext, subplans);
if (unorderedUnionPlan != null) {
return planRemoveDuplicates(planContext, unorderedUnionPlan);
}
return null;
}
Aggregations