Search in sources :

Example 16 with RelationalExpression

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

the class ExpressionMatcherTest method treeDescentWithMixedBindings.

@Test
public void treeDescentWithMixedBindings() {
    // build a relatively complicated matcher
    BindingMatcher<? extends ExpressionRef<? extends RelationalExpression>> filterLeafMatcher = ReferenceMatchers.anyRef();
    BindingMatcher<QueryPredicate> predicateMatcher = QueryPredicateMatchers.anyPredicate();
    final BindingMatcher<LogicalFilterExpression> filterPlanMatcher = RelationalExpressionMatchers.logicalFilterExpression(MultiMatcher.AllMatcher.all(predicateMatcher), AnyMatcher.any(QuantifierMatchers.forEachQuantifierOverRef(filterLeafMatcher)));
    BindingMatcher<RecordQueryScanPlan> scanMatcher = RecordQueryPlanMatchers.scanPlan();
    BindingMatcher<LogicalUnionExpression> matcher = RelationalExpressionMatchers.logicalUnionExpression(ListMatcher.exactly(QuantifierMatchers.forEachQuantifier(filterPlanMatcher), QuantifierMatchers.forEachQuantifier(scanMatcher)));
    // build a relatively complicated expression
    QueryComponent andBranch1 = Query.field("field1").greaterThan(6);
    QueryComponent andBranch2 = Query.field("field2").equalsParameter("param");
    IndexScanParameters fullValueScan = IndexScanComparisons.byValue();
    final Quantifier.ForEach quantifier = Quantifier.forEach(GroupExpressionRef.of(new RecordQueryIndexPlan("an_index", fullValueScan, true)));
    LogicalFilterExpression filterPlan = new LogicalFilterExpression(Query.and(andBranch1, andBranch2).expand(quantifier.getAlias()).getPredicates(), quantifier);
    RecordQueryScanPlan scanPlan = new RecordQueryScanPlan(ScanComparisons.EMPTY, true);
    RelationalExpression root = new LogicalUnionExpression(Quantifiers.forEachQuantifiers(ImmutableList.of(GroupExpressionRef.of(filterPlan), GroupExpressionRef.of(scanPlan))));
    assertTrue(filterPlanMatcher.bindMatches(PlannerBindings.empty(), filterPlan).findFirst().isPresent());
    // try to bind
    Optional<PlannerBindings> possibleBindings = matcher.bindMatches(PlannerBindings.empty(), root).findFirst();
    // check that all the bindings match what we expect
    assertTrue(possibleBindings.isPresent());
    PlannerBindings bindings = possibleBindings.get().mergedWith(getExistingBindings());
    assertEquals(root, bindings.get(matcher));
    assertEquals(filterPlan, bindings.get(filterPlanMatcher));
    assertEquals(scanPlan, bindings.get(scanMatcher));
    assertEquals(filterPlan.getPredicates(), bindings.getAll(predicateMatcher));
    // dereference
    assertEquals(filterPlan.getInner().getRangesOver().get(), bindings.get(filterLeafMatcher).get());
}
Also used : QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) IndexScanParameters(com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) RecordQueryScanPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) LogicalUnionExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalUnionExpression) 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 17 with RelationalExpression

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

the class ExpressionMatcherTest method nestedTypeMatchers.

@Test
public void nestedTypeMatchers() {
    BindingMatcher<RecordQueryIndexPlan> childMatcher1 = RecordQueryPlanMatchers.indexPlan();
    BindingMatcher<RecordQueryScanPlan> childMatcher2 = RecordQueryPlanMatchers.scanPlan();
    BindingMatcher<RecordQueryUnionPlan> parentMatcher = RecordQueryPlanMatchers.union(ListMatcher.exactly(QuantifierMatchers.physicalQuantifier(childMatcher1), QuantifierMatchers.physicalQuantifier(childMatcher2)));
    IndexScanParameters fullValueScan = IndexScanComparisons.byValue();
    RecordQueryIndexPlan child1 = new RecordQueryIndexPlan("an_index", fullValueScan, true);
    RecordQueryScanPlan child2 = new RecordQueryScanPlan(ScanComparisons.EMPTY, true);
    // check matches if the children are in the right order
    RelationalExpression root = // union with arbitrary comparison key
    RecordQueryUnionPlan.from(child1, child2, EmptyKeyExpression.EMPTY, false);
    Optional<PlannerBindings> possibleBindings = parentMatcher.bindMatches(PlannerBindings.empty(), root).findFirst();
    assertTrue(possibleBindings.isPresent());
    PlannerBindings allBindings = possibleBindings.get().mergedWith(getExistingBindings());
    assertExistingBindingsSurvived(allBindings);
    assertEquals(root, allBindings.get(parentMatcher));
    assertEquals(child1, allBindings.get(childMatcher1));
    assertEquals(child2, allBindings.get(childMatcher2));
    // check that we fail to match if the children are in the wrong order
    root = // union with arbitrary comparison key
    RecordQueryUnionPlan.from(child2, child1, EmptyKeyExpression.EMPTY, false);
    assertFalse(parentMatcher.bindMatches(PlannerBindings.empty(), root).findFirst().isPresent());
}
Also used : IndexScanParameters(com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) RecordQueryUnionPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryUnionPlan) RecordQueryScanPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) Test(org.junit.jupiter.api.Test)

Example 18 with RelationalExpression

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

the class ExpressionMatcherTest method matchChildOrder.

@Test
public void matchChildOrder() {
    BindingMatcher<RecordQueryUnionPlan> parentMatcher = RecordQueryPlanMatchers.union(ListMatcher.exactly(QuantifierMatchers.physicalQuantifier(RecordQueryPlanMatchers.indexPlan()), QuantifierMatchers.physicalQuantifier(RecordQueryPlanMatchers.scanPlan())));
    IndexScanParameters fullValueScan = IndexScanComparisons.byValue();
    RecordQueryIndexPlan child1 = new RecordQueryIndexPlan("an_index", fullValueScan, true);
    RecordQueryScanPlan child2 = new RecordQueryScanPlan(ScanComparisons.EMPTY, true);
    RelationalExpression root = // union with arbitrary comparison key
    RecordQueryUnionPlan.from(child1, child2, EmptyKeyExpression.EMPTY, false);
    assertTrue(parentMatcher.bindMatches(PlannerBindings.empty(), root).findFirst().isPresent());
    root = // union with arbitrary comparison key
    RecordQueryUnionPlan.from(child2, child1, EmptyKeyExpression.EMPTY, false);
    assertFalse(parentMatcher.bindMatches(PlannerBindings.empty(), root).findFirst().isPresent());
}
Also used : IndexScanParameters(com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) RecordQueryUnionPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryUnionPlan) RecordQueryScanPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) Test(org.junit.jupiter.api.Test)

Example 19 with RelationalExpression

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

the class ExpressionMatcherTest method matchChildrenAsReferences.

@Test
public void matchChildrenAsReferences() {
    BindingMatcher<? extends ExpressionRef<? extends RelationalExpression>> childMatcher1 = ReferenceMatchers.anyRef();
    BindingMatcher<? extends ExpressionRef<? extends RelationalExpression>> childMatcher2 = ReferenceMatchers.anyRef();
    BindingMatcher<RecordQueryUnionPlan> matcher = RecordQueryPlanMatchers.union(ListMatcher.exactly(QuantifierMatchers.physicalQuantifierOverRef(childMatcher1), QuantifierMatchers.physicalQuantifierOverRef(childMatcher2)));
    IndexScanParameters fullValueScan = IndexScanComparisons.byValue();
    RecordQueryIndexPlan child1 = new RecordQueryIndexPlan("an_index", fullValueScan, true);
    RecordQueryScanPlan child2 = new RecordQueryScanPlan(ScanComparisons.EMPTY, true);
    RelationalExpression root = // union with arbitrary comparison key
    RecordQueryUnionPlan.from(child1, child2, EmptyKeyExpression.EMPTY, false);
    Optional<PlannerBindings> possibleBindings = matcher.bindMatches(PlannerBindings.empty(), root).findFirst();
    assertTrue(possibleBindings.isPresent());
    PlannerBindings newBindings = possibleBindings.get().mergedWith(getExistingBindings());
    assertExistingBindingsSurvived(newBindings);
    // check that root matches
    assertEquals(root, newBindings.get(matcher));
    // check that children are behind references
    assertEquals(child1, newBindings.get(childMatcher1).get());
    assertEquals(child2, newBindings.get(childMatcher2).get());
}
Also used : IndexScanParameters(com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) RecordQueryUnionPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryUnionPlan) RecordQueryScanPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) Test(org.junit.jupiter.api.Test)

Example 20 with RelationalExpression

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

the class CombineFilterRuleTest method doesNotMatchSingleFilter.

@Test
public void doesNotMatchSingleFilter() {
    for (RecordQueryPlan basePlan : basePlans) {
        QueryComponent filter1 = Query.field("testField").equalsValue(5);
        GroupExpressionRef<RelationalExpression> root = GroupExpressionRef.of(buildLogicalFilter(filter1, basePlan));
        TestRuleExecution execution = TestRuleExecution.applyRule(blankContext, rule, root);
        assertFalse(execution.isRuleMatched());
    }
}
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)

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