Search in sources :

Example 6 with QueryPredicate

use of com.apple.foundationdb.record.query.predicates.QueryPredicate in project fdb-record-layer by FoundationDB.

the class PushDistinctBelowFilterRule method onMatch.

@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
    final ExpressionRef<? extends RelationalExpression> inner = call.get(innerRefMatcher);
    final Quantifier.Physical qun = call.get(innerQuantifierMatcher);
    final RecordQueryPredicatesFilterPlan filterPlan = call.get(filterPlanMatcher);
    final RecordQueryUnorderedPrimaryKeyDistinctPlan newDistinctPlan = new RecordQueryUnorderedPrimaryKeyDistinctPlan(Quantifier.physical(inner));
    final Quantifier.Physical newQun = Quantifier.physical(call.ref(newDistinctPlan));
    final List<QueryPredicate> rebasedPredicates = filterPlan.getPredicates().stream().map(queryPredicate -> queryPredicate.rebase(Quantifiers.translate(qun, newQun))).collect(ImmutableList.toImmutableList());
    call.yield(call.ref(new RecordQueryPredicatesFilterPlan(newQun, rebasedPredicates)));
}
Also used : PlannerRuleCall(com.apple.foundationdb.record.query.plan.temp.PlannerRuleCall) PlannerRule(com.apple.foundationdb.record.query.plan.temp.PlannerRule) QuantifierMatchers.physicalQuantifier(com.apple.foundationdb.record.query.plan.temp.matchers.QuantifierMatchers.physicalQuantifier) Quantifier(com.apple.foundationdb.record.query.plan.temp.Quantifier) QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) Quantifiers(com.apple.foundationdb.record.query.plan.temp.Quantifiers) RecordQueryPredicatesFilterPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPredicatesFilterPlan) QuantifierMatchers.physicalQuantifierOverRef(com.apple.foundationdb.record.query.plan.temp.matchers.QuantifierMatchers.physicalQuantifierOverRef) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) List(java.util.List) BindingMatcher(com.apple.foundationdb.record.query.plan.temp.matchers.BindingMatcher) ImmutableList(com.google.common.collect.ImmutableList) RecordQueryUnorderedPrimaryKeyDistinctPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryUnorderedPrimaryKeyDistinctPlan) ReferenceMatchers.anyRefOverOnlyPlans(com.apple.foundationdb.record.query.plan.temp.matchers.ReferenceMatchers.anyRefOverOnlyPlans) ExpressionRef(com.apple.foundationdb.record.query.plan.temp.ExpressionRef) ListMatcher.exactly(com.apple.foundationdb.record.query.plan.temp.matchers.ListMatcher.exactly) RecordQueryPlanMatchers(com.apple.foundationdb.record.query.plan.temp.matchers.RecordQueryPlanMatchers) Nonnull(javax.annotation.Nonnull) QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) RecordQueryPredicatesFilterPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPredicatesFilterPlan) QuantifierMatchers.physicalQuantifier(com.apple.foundationdb.record.query.plan.temp.matchers.QuantifierMatchers.physicalQuantifier) Quantifier(com.apple.foundationdb.record.query.plan.temp.Quantifier) RecordQueryUnorderedPrimaryKeyDistinctPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryUnorderedPrimaryKeyDistinctPlan)

Example 7 with QueryPredicate

use of com.apple.foundationdb.record.query.predicates.QueryPredicate in project fdb-record-layer by FoundationDB.

the class ImplementFilterRule method onMatch.

@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
    final PlannerBindings bindings = call.getBindings();
    final Collection<? extends RecordQueryPlan> innerPlans = bindings.get(innerPlansMatcher);
    final Quantifier.ForEach innerQuantifier = bindings.get(innerQuantifierMatcher);
    final List<? extends QueryPredicate> queryPredicates = bindings.getAll(filterMatcher);
    final GroupExpressionRef<? extends RecordQueryPlan> referenceOverPlans = GroupExpressionRef.from(innerPlans);
    if (queryPredicates.stream().allMatch(QueryPredicate::isTautology)) {
        call.yield(referenceOverPlans);
    } else {
        call.yield(GroupExpressionRef.of(new RecordQueryPredicatesFilterPlan(Quantifier.physicalBuilder().morphFrom(innerQuantifier).build(referenceOverPlans), queryPredicates)));
    }
}
Also used : QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) RecordQueryPredicatesFilterPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPredicatesFilterPlan) PlannerBindings(com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings) Quantifier(com.apple.foundationdb.record.query.plan.temp.Quantifier)

Example 8 with QueryPredicate

use of com.apple.foundationdb.record.query.predicates.QueryPredicate in project fdb-record-layer by FoundationDB.

the class AbstractDataAccessRule method maximumCoverageMatches.

/**
 * Private helper method to eliminate {@link PartialMatch}es whose coverage is entirely contained in other matches
 * (among the matches given).
 * @param matches candidate matches
 * @param interestedOrderings a set of interesting orderings
 * @return a list of {@link PartialMatch}es that are the maximum coverage matches among the matches handed in
 */
@Nonnull
@SuppressWarnings({ "java:S1905", "java:S135" })
private static List<PartialMatch> maximumCoverageMatches(@Nonnull final Collection<PartialMatch> matches, @Nonnull final Set<RequestedOrdering> interestedOrderings) {
    final ImmutableList<Pair<PartialMatch, Map<QueryPredicate, BoundKeyPart>>> boundKeyPartMapsForMatches = matches.stream().filter(partialMatch -> !satisfiedOrderings(partialMatch, interestedOrderings).isEmpty()).map(partialMatch -> Pair.of(partialMatch, computeBoundKeyPartMap(partialMatch))).sorted(Comparator.comparing((Function<Pair<PartialMatch, Map<QueryPredicate, BoundKeyPart>>, Integer>) p -> p.getValue().size()).reversed()).collect(ImmutableList.toImmutableList());
    final ImmutableList.Builder<PartialMatch> maximumCoverageMatchesBuilder = ImmutableList.builder();
    for (int i = 0; i < boundKeyPartMapsForMatches.size(); i++) {
        final PartialMatch outerMatch = boundKeyPartMapsForMatches.get(i).getKey();
        final Map<QueryPredicate, BoundKeyPart> outer = boundKeyPartMapsForMatches.get(i).getValue();
        boolean foundContainingInner = false;
        for (int j = 0; j < boundKeyPartMapsForMatches.size(); j++) {
            final Map<QueryPredicate, BoundKeyPart> inner = boundKeyPartMapsForMatches.get(j).getValue();
            // check if outer is completely contained in inner
            if (outer.size() >= inner.size()) {
                break;
            }
            if (i != j) {
                final boolean allContained = outer.entrySet().stream().allMatch(outerEntry -> inner.containsKey(outerEntry.getKey()));
                if (allContained) {
                    foundContainingInner = true;
                    break;
                }
            }
        }
        if (!foundContainingInner) {
            // 
            // no other partial match completely contained this one
            // 
            maximumCoverageMatchesBuilder.add(outerMatch);
        }
    }
    return maximumCoverageMatchesBuilder.build();
}
Also used : PlannerRuleCall(com.apple.foundationdb.record.query.plan.temp.PlannerRuleCall) OrderingAttribute(com.apple.foundationdb.record.query.plan.temp.OrderingAttribute) CascadesPlanner(com.apple.foundationdb.record.query.plan.temp.CascadesPlanner) LinkedIdentitySet(com.apple.foundationdb.record.query.plan.temp.LinkedIdentitySet) GroupExpressionRef(com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef) PartialMatch(com.apple.foundationdb.record.query.plan.temp.PartialMatch) Pair(org.apache.commons.lang3.tuple.Pair) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) ComparisonRange(com.apple.foundationdb.record.query.plan.temp.ComparisonRange) Map(java.util.Map) IndexScanExpression(com.apple.foundationdb.record.query.plan.temp.expressions.IndexScanExpression) MatchCandidate(com.apple.foundationdb.record.query.plan.temp.MatchCandidate) PrimaryScanExpression(com.apple.foundationdb.record.query.plan.temp.expressions.PrimaryScanExpression) RequestedOrdering(com.apple.foundationdb.record.query.plan.temp.RequestedOrdering) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) MatchPartition(com.apple.foundationdb.record.query.plan.temp.MatchPartition) Set(java.util.Set) PlannerBindings(com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings) ReferencedFieldsAttribute(com.apple.foundationdb.record.query.plan.temp.ReferencedFieldsAttribute) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) BoundKeyPart(com.apple.foundationdb.record.query.plan.temp.BoundKeyPart) List(java.util.List) Stream(java.util.stream.Stream) CorrelationIdentifier(com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier) MatchInfo(com.apple.foundationdb.record.query.plan.temp.MatchInfo) Optional(java.util.Optional) API(com.apple.foundationdb.annotation.API) IntStream(java.util.stream.IntStream) Iterables(com.google.common.collect.Iterables) PlannerRule(com.apple.foundationdb.record.query.plan.temp.PlannerRule) Quantifier(com.apple.foundationdb.record.query.plan.temp.Quantifier) Ordering(com.apple.foundationdb.record.query.plan.temp.Ordering) Function(java.util.function.Function) Key(com.apple.foundationdb.record.metadata.Key) LinkedHashMap(java.util.LinkedHashMap) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Compensation(com.apple.foundationdb.record.query.plan.temp.Compensation) StreamSupport(java.util.stream.StreamSupport) ExpressionRef(com.apple.foundationdb.record.query.plan.temp.ExpressionRef) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ChooseK(com.apple.foundationdb.record.query.combinatorics.ChooseK) LogicalIntersectionExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalIntersectionExpression) Iterator(java.util.Iterator) LogicalDistinctExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalDistinctExpression) PartialOrder(com.apple.foundationdb.record.query.combinatorics.PartialOrder) QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) KeyPart(com.apple.foundationdb.record.query.plan.temp.KeyPart) Maps(com.google.common.collect.Maps) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) BindingMatcher(com.apple.foundationdb.record.query.plan.temp.matchers.BindingMatcher) PrimaryScanMatchCandidate(com.apple.foundationdb.record.query.plan.temp.PrimaryScanMatchCandidate) Comparator(java.util.Comparator) ValueIndexScanMatchCandidate(com.apple.foundationdb.record.query.plan.temp.ValueIndexScanMatchCandidate) QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) ImmutableList(com.google.common.collect.ImmutableList) BoundKeyPart(com.apple.foundationdb.record.query.plan.temp.BoundKeyPart) Function(java.util.function.Function) PartialMatch(com.apple.foundationdb.record.query.plan.temp.PartialMatch) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) Pair(org.apache.commons.lang3.tuple.Pair) Nonnull(javax.annotation.Nonnull)

Example 9 with QueryPredicate

use of com.apple.foundationdb.record.query.predicates.QueryPredicate in project fdb-record-layer by FoundationDB.

the class BooleanPredicateNormalizerTest method assertExpectedNormalization.

protected static void assertExpectedNormalization(@Nonnull final BooleanPredicateNormalizer normalizer, @Nonnull final QueryPredicate expected, @Nonnull final QueryPredicate given) {
    final QueryPredicate normalized = normalizer.normalize(given).orElse(given);
    assertFilterEquals(expected, Objects.requireNonNull(normalized));
    assertEquals(normalized, normalizer.normalize(normalized).orElse(normalized), "Normalized form should be stable");
}
Also used : QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate)

Example 10 with QueryPredicate

use of com.apple.foundationdb.record.query.predicates.QueryPredicate in project fdb-record-layer by FoundationDB.

the class BooleanPredicateNormalizerTest method bigCnfThatWouldOverflow.

@Test
void bigCnfThatWouldOverflow() {
    // A CNF who's DNF size doesn't fit in an int.
    final List<QueryPredicate> conjuncts = new ArrayList<>();
    for (int i = 0; i < 32; i++) {
        final List<QueryPredicate> disjuncts = new ArrayList<>();
        for (int j = 0; j < 2; j++) {
            disjuncts.add(new ValuePredicate(F, new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, i * 100 + j)));
        }
        conjuncts.add(or(disjuncts));
    }
    final QueryPredicate cnf = and(conjuncts);
    final BooleanPredicateNormalizer normalizer = BooleanPredicateNormalizer.getDefaultInstanceForDnf();
    assertThrows(ArithmeticException.class, () -> normalizer.getNormalizedSize(cnf));
    assertThrows(BooleanPredicateNormalizer.NormalFormTooLargeException.class, () -> normalizer.normalize(cnf));
    assertEquals(cnf, normalizer.normalizeIfPossible(cnf).orElse(cnf));
}
Also used : QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) ValuePredicate(com.apple.foundationdb.record.query.predicates.ValuePredicate) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

QueryPredicate (com.apple.foundationdb.record.query.predicates.QueryPredicate)22 Quantifier (com.apple.foundationdb.record.query.plan.temp.Quantifier)14 ImmutableList (com.google.common.collect.ImmutableList)14 Nonnull (javax.annotation.Nonnull)13 List (java.util.List)12 API (com.apple.foundationdb.annotation.API)10 RelationalExpression (com.apple.foundationdb.record.query.plan.temp.RelationalExpression)10 PlannerBindings (com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings)9 CorrelationIdentifier (com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier)8 ImmutableSet (com.google.common.collect.ImmutableSet)7 Collection (java.util.Collection)7 Objects (java.util.Objects)7 Optional (java.util.Optional)7 Collectors (java.util.stream.Collectors)7 ComparisonRange (com.apple.foundationdb.record.query.plan.temp.ComparisonRange)6 ExpressionRef (com.apple.foundationdb.record.query.plan.temp.ExpressionRef)6 GroupExpressionRef (com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef)6 MatchInfo (com.apple.foundationdb.record.query.plan.temp.MatchInfo)6 PartialMatch (com.apple.foundationdb.record.query.plan.temp.PartialMatch)6 Value (com.apple.foundationdb.record.query.predicates.Value)6