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());
}
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());
}
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());
}
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());
}
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());
}
}
Aggregations