Search in sources :

Example 6 with RelationalExpression

use of com.apple.foundationdb.record.query.plan.temp.RelationalExpression in project fdb-record-layer by FoundationDB.

the class AdjustMatchRule method onMatch.

@Override
@SuppressWarnings("java:S135")
public void onMatch(@Nonnull PlannerRuleCall call) {
    final PlannerBindings bindings = call.getBindings();
    final PartialMatch incompleteMatch = bindings.get(rootMatcher);
    final ExpressionRef<? extends RelationalExpression> queryReference = incompleteMatch.getQueryRef();
    final MatchCandidate matchCandidate = incompleteMatch.getMatchCandidate();
    final SetMultimap<ExpressionRef<? extends RelationalExpression>, RelationalExpression> refToExpressionMap = matchCandidate.findReferencingExpressions(ImmutableList.of(queryReference));
    for (final Map.Entry<ExpressionRef<? extends RelationalExpression>, RelationalExpression> entry : refToExpressionMap.entries()) {
        final ExpressionRef<? extends RelationalExpression> candidateReference = entry.getKey();
        final RelationalExpression candidateExpression = entry.getValue();
        matchWithCandidate(incompleteMatch, candidateExpression).ifPresent(matchInfo -> call.yieldPartialMatch(incompleteMatch.getBoundAliasMap(), matchCandidate, incompleteMatch.getQueryExpression(), candidateReference, matchInfo));
    }
}
Also used : RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) ExpressionRef(com.apple.foundationdb.record.query.plan.temp.ExpressionRef) PartialMatch(com.apple.foundationdb.record.query.plan.temp.PartialMatch) PlannerBindings(com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings) MatchCandidate(com.apple.foundationdb.record.query.plan.temp.MatchCandidate) Map(java.util.Map)

Example 7 with RelationalExpression

use of com.apple.foundationdb.record.query.plan.temp.RelationalExpression in project fdb-record-layer by FoundationDB.

the class ExpressionMatcherTest method anyRefMatcher.

@Test
public void anyRefMatcher() {
    // create a matcher and expression to match
    BindingMatcher<? extends ExpressionRef<? extends RelationalExpression>> matcher = ReferenceMatchers.anyRef();
    Quantifier.ForEach quantifier = Quantifier.forEach(GroupExpressionRef.of(new RecordQueryScanPlan(ScanComparisons.EMPTY, false)));
    ExpressionRef<RelationalExpression> root = GroupExpressionRef.of(new LogicalFilterExpression(ImmutableList.of(new QueryComponentPredicate(Query.field("test").equalsValue(5))), quantifier));
    // try to match to expression
    Optional<PlannerBindings> newBindings = matcher.bindMatches(PlannerBindings.empty(), root).findFirst();
    // check the the bindings are what we expect, and that none of the existing ones were clobbered
    assertTrue(newBindings.isPresent());
    PlannerBindings allBindings = newBindings.get().mergedWith(getExistingBindings());
    assertExistingBindingsSurvived(allBindings);
    assertTrue(newBindings.get().containsKey(matcher));
    assertTrue(allBindings.containsKey(matcher));
    assertEquals(root, allBindings.get(matcher));
}
Also used : RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) RecordQueryScanPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan) QueryComponentPredicate(com.apple.foundationdb.record.query.predicates.QueryComponentPredicate) LogicalFilterExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalFilterExpression) Quantifier(com.apple.foundationdb.record.query.plan.temp.Quantifier) Test(org.junit.jupiter.api.Test)

Example 8 with RelationalExpression

use of com.apple.foundationdb.record.query.plan.temp.RelationalExpression in project fdb-record-layer by FoundationDB.

the class CombineFilterRuleTest method combineFilter.

@Test
public void combineFilter() {
    for (RecordQueryPlan basePlan : basePlans) {
        QueryComponent filter1 = Query.field("testField").equalsValue(5);
        QueryComponent filter2 = Query.field("testField2").equalsValue(10);
        GroupExpressionRef<RelationalExpression> root = GroupExpressionRef.of(buildLogicalFilter(filter1, buildLogicalFilter(filter2, basePlan)));
        TestRuleExecution execution = TestRuleExecution.applyRule(blankContext, rule, root);
        assertTrue(execution.isRuleMatched());
        assertTrue(execution.getResult().containsInMemo(buildLogicalFilter(Query.and(filter1, filter2), basePlan)));
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) Test(org.junit.jupiter.api.Test)

Example 9 with RelationalExpression

use of com.apple.foundationdb.record.query.plan.temp.RelationalExpression in project fdb-record-layer by FoundationDB.

the class CombineFilterRuleTest method doesNotCoalesce.

@Test
public void doesNotCoalesce() {
    for (RecordQueryPlan basePlan : basePlans) {
        QueryComponent filter1 = Query.field("testField").equalsValue(5);
        GroupExpressionRef<RelationalExpression> root = GroupExpressionRef.of(buildLogicalFilter(filter1, buildLogicalFilter(filter1, basePlan)));
        TestRuleExecution execution = TestRuleExecution.applyRule(blankContext, rule, root);
        assertTrue(execution.isRuleMatched());
        // this rule should not try to coalesce the two filters
        assertTrue(root.containsInMemo(buildLogicalFilter(Query.and(filter1, filter1), basePlan)));
        assertFalse(root.containsInMemo(buildLogicalFilter(filter1, basePlan)));
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) Test(org.junit.jupiter.api.Test)

Example 10 with RelationalExpression

use of com.apple.foundationdb.record.query.plan.temp.RelationalExpression in project fdb-record-layer by FoundationDB.

the class TestRuleExecution method applyRule.

public static TestRuleExecution applyRule(@Nonnull PlanContext context, @Nonnull PlannerRule<? extends RelationalExpression> rule, @Nonnull GroupExpressionRef<RelationalExpression> group) {
    boolean ruleMatched = false;
    for (RelationalExpression expression : group.getMembers()) {
        final Iterator<CascadesRuleCall> ruleCalls = rule.getMatcher().bindMatches(PlannerBindings.empty(), expression).map(bindings -> new CascadesRuleCall(context, rule, group, Quantifiers.AliasResolver.withRoot(group), bindings)).iterator();
        while (ruleCalls.hasNext()) {
            ruleCalls.next().run();
            ruleMatched = true;
        }
    }
    return new TestRuleExecution(ruleMatched, group);
}
Also used : RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) Iterator(java.util.Iterator) PlannerRule(com.apple.foundationdb.record.query.plan.temp.PlannerRule) PlanContext(com.apple.foundationdb.record.query.plan.temp.PlanContext) PlannerBindings(com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings) Quantifiers(com.apple.foundationdb.record.query.plan.temp.Quantifiers) GroupExpressionRef(com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) CascadesRuleCall(com.apple.foundationdb.record.query.plan.temp.CascadesRuleCall) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) CascadesRuleCall(com.apple.foundationdb.record.query.plan.temp.CascadesRuleCall)

Aggregations

RelationalExpression (com.apple.foundationdb.record.query.plan.temp.RelationalExpression)26 ExpressionRef (com.apple.foundationdb.record.query.plan.temp.ExpressionRef)12 Quantifier (com.apple.foundationdb.record.query.plan.temp.Quantifier)11 Nonnull (javax.annotation.Nonnull)10 ImmutableList (com.google.common.collect.ImmutableList)9 List (java.util.List)9 Test (org.junit.jupiter.api.Test)9 PartialMatch (com.apple.foundationdb.record.query.plan.temp.PartialMatch)8 API (com.apple.foundationdb.annotation.API)7 GroupExpressionRef (com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef)7 MatchCandidate (com.apple.foundationdb.record.query.plan.temp.MatchCandidate)7 Collection (java.util.Collection)7 Map (java.util.Map)7 Nullable (javax.annotation.Nullable)7 RecordQueryScanPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan)6 PlannerBindings (com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings)6 QueryPredicate (com.apple.foundationdb.record.query.predicates.QueryPredicate)6 ImmutableSet (com.google.common.collect.ImmutableSet)6 Optional (java.util.Optional)6 Set (java.util.Set)6