Search in sources :

Example 1 with PlannerBindings

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

the class InComparisonToExplodeRule method onMatch.

@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
    final PlannerBindings bindings = call.getBindings();
    final SelectExpression selectExpression = bindings.get(root);
    // we don't need iteration stability
    final List<? extends ValuePredicate> inPredicatesList = bindings.getAll(inPredicateMatcher);
    if (inPredicatesList.isEmpty()) {
        return;
    }
    final Set<QueryPredicate> inPredicates = Sets.newIdentityHashSet();
    inPredicates.addAll(inPredicatesList);
    final ImmutableList.Builder<Quantifier> transformedQuantifiers = ImmutableList.builder();
    final ImmutableList.Builder<QueryPredicate> transformedPredicates = ImmutableList.builder();
    for (final QueryPredicate predicate : selectExpression.getPredicates()) {
        if (inPredicates.contains(predicate)) {
            final ValuePredicate valuePredicate = (ValuePredicate) predicate;
            final Comparisons.Comparison comparison = valuePredicate.getComparison();
            Verify.verify(comparison.getType() == Comparisons.Type.IN);
            final ExplodeExpression explodeExpression;
            if (comparison instanceof Comparisons.ListComparison) {
                explodeExpression = new ExplodeExpression(new LiteralValue<>(comparison.getComparand()));
            } else if (comparison instanceof Comparisons.ParameterComparison) {
                explodeExpression = new ExplodeExpression(QuantifiedColumnValue.of(CorrelationIdentifier.of(((Comparisons.ParameterComparison) comparison).getParameter()), 0));
            } else {
                throw new RecordCoreException("unknown in comparison " + comparison.getClass().getSimpleName());
            }
            final Quantifier.ForEach newQuantifier = Quantifier.forEach(GroupExpressionRef.of(explodeExpression));
            transformedPredicates.add(new ValuePredicate(((ValuePredicate) predicate).getValue(), new Comparisons.ParameterComparison(Comparisons.Type.EQUALS, Bindings.Internal.CORRELATION.bindingName(newQuantifier.getAlias().toString()), Bindings.Internal.CORRELATION)));
            transformedQuantifiers.add(newQuantifier);
        } else {
            transformedPredicates.add(predicate);
        }
    }
    transformedQuantifiers.addAll(bindings.getAll(innerQuantifierMatcher));
    call.yield(call.ref(new SelectExpression(selectExpression.getResultValues(), transformedQuantifiers.build(), transformedPredicates.build())));
}
Also used : QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) ImmutableList(com.google.common.collect.ImmutableList) LiteralValue(com.apple.foundationdb.record.query.predicates.LiteralValue) SelectExpression(com.apple.foundationdb.record.query.plan.temp.expressions.SelectExpression) ExplodeExpression(com.apple.foundationdb.record.query.plan.temp.expressions.ExplodeExpression) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) ValuePredicate(com.apple.foundationdb.record.query.predicates.ValuePredicate) PlannerBindings(com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings) Quantifier(com.apple.foundationdb.record.query.plan.temp.Quantifier) QuantifierMatchers.forEachQuantifier(com.apple.foundationdb.record.query.plan.temp.matchers.QuantifierMatchers.forEachQuantifier)

Example 2 with PlannerBindings

use of com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings 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 3 with PlannerBindings

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

the class PushInJoinThroughFetchRule method onMatch.

@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
    final PlannerBindings bindings = call.getBindings();
    final RecordQueryInJoinPlan inJoinPlan = bindings.get(getMatcher());
    final RecordQueryFetchFromPartialRecordPlan fetchPlan = bindings.get(fetchPlanMatcher);
    final RecordQueryPlan innerPlan = bindings.get(innerPlanMatcher);
    final RecordQueryPlanWithChild pushedInJoinPlan = inJoinPlan.withChild(innerPlan);
    final var newFetchPlan = new RecordQueryFetchFromPartialRecordPlan(pushedInJoinPlan, fetchPlan.getPushValueFunction());
    call.yield(call.ref(newFetchPlan));
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) RecordQueryFetchFromPartialRecordPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryFetchFromPartialRecordPlan) PlannerBindings(com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings) RecordQueryPlanWithChild(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithChild) RecordQueryInJoinPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryInJoinPlan)

Example 4 with PlannerBindings

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

the class PushInterestingOrderingThroughDistinctRule method onMatch.

@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
    final Optional<Set<RequestedOrdering>> requestedOrderingOptionals = call.getInterestingProperty(OrderingAttribute.ORDERING);
    if (requestedOrderingOptionals.isEmpty()) {
        return;
    }
    final PlannerBindings bindings = call.getBindings();
    final ExpressionRef<? extends RelationalExpression> lowerRef = bindings.get(lowerRefMatcher);
    call.pushRequirement(lowerRef, OrderingAttribute.ORDERING, requestedOrderingOptionals.get());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) PlannerBindings(com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings)

Example 5 with PlannerBindings

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

Aggregations

PlannerBindings (com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings)22 Quantifier (com.apple.foundationdb.record.query.plan.temp.Quantifier)11 ImmutableList (com.google.common.collect.ImmutableList)9 QueryPredicate (com.apple.foundationdb.record.query.predicates.QueryPredicate)8 API (com.apple.foundationdb.annotation.API)7 ExpressionRef (com.apple.foundationdb.record.query.plan.temp.ExpressionRef)7 GroupExpressionRef (com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef)7 PlannerRule (com.apple.foundationdb.record.query.plan.temp.PlannerRule)7 PlannerRuleCall (com.apple.foundationdb.record.query.plan.temp.PlannerRuleCall)7 BindingMatcher (com.apple.foundationdb.record.query.plan.temp.matchers.BindingMatcher)7 Nonnull (javax.annotation.Nonnull)7 List (java.util.List)6 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)5 RelationalExpression (com.apple.foundationdb.record.query.plan.temp.RelationalExpression)5 Set (java.util.Set)5 RecordQueryFetchFromPartialRecordPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryFetchFromPartialRecordPlan)4 CorrelationIdentifier (com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier)4 MultiMatcher.all (com.apple.foundationdb.record.query.plan.temp.matchers.MultiMatcher.all)4 ImmutableSet (com.google.common.collect.ImmutableSet)4 Collection (java.util.Collection)4