Search in sources :

Example 1 with LogicalUnionExpression

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

the class OrToLogicalUnionRule method onMatch.

@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
    final PlannerBindings bindings = call.getBindings();
    final SelectExpression selectExpression = bindings.get(root);
    final List<? extends Value> resultValues = selectExpression.getResultValues();
    final List<? extends Quantifier> quantifiers = bindings.getAll(qunMatcher);
    final List<? extends QueryPredicate> orTermPredicates = bindings.getAll(orTermPredicateMatcher);
    final List<ExpressionRef<RelationalExpression>> relationalExpressionRefs = new ArrayList<>(orTermPredicates.size());
    for (final QueryPredicate orTermPredicate : orTermPredicates) {
        relationalExpressionRefs.add(call.ref(new SelectExpression(resultValues, quantifiers, ImmutableList.of(orTermPredicate))));
    }
    call.yield(GroupExpressionRef.of(new LogicalUnionExpression(Quantifiers.forEachQuantifiers(relationalExpressionRefs))));
}
Also used : GroupExpressionRef(com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef) ExpressionRef(com.apple.foundationdb.record.query.plan.temp.ExpressionRef) QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) PlannerBindings(com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings) ArrayList(java.util.ArrayList) SelectExpression(com.apple.foundationdb.record.query.plan.temp.expressions.SelectExpression) LogicalUnionExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalUnionExpression)

Example 2 with LogicalUnionExpression

use of com.apple.foundationdb.record.query.plan.temp.expressions.LogicalUnionExpression 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)

Aggregations

LogicalUnionExpression (com.apple.foundationdb.record.query.plan.temp.expressions.LogicalUnionExpression)2 QueryPredicate (com.apple.foundationdb.record.query.predicates.QueryPredicate)2 IndexScanParameters (com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters)1 QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)1 RecordQueryIndexPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan)1 RecordQueryScanPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan)1 ExpressionRef (com.apple.foundationdb.record.query.plan.temp.ExpressionRef)1 GroupExpressionRef (com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef)1 Quantifier (com.apple.foundationdb.record.query.plan.temp.Quantifier)1 RelationalExpression (com.apple.foundationdb.record.query.plan.temp.RelationalExpression)1 LogicalFilterExpression (com.apple.foundationdb.record.query.plan.temp.expressions.LogicalFilterExpression)1 SelectExpression (com.apple.foundationdb.record.query.plan.temp.expressions.SelectExpression)1 PlannerBindings (com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1