use of com.apple.foundationdb.record.query.plan.temp.ExpressionRef in project fdb-record-layer by FoundationDB.
the class OrderingProperty method evaluateAtRef.
@Nonnull
@Override
public Optional<Ordering> evaluateAtRef(@Nonnull ExpressionRef<? extends RelationalExpression> ref, @Nonnull List<Optional<Ordering>> orderingOptionals) {
final Optional<SetMultimap<KeyExpression, Comparisons.Comparison>> commonEqualityBoundKeysMapOptional = Ordering.combineEqualityBoundKeys(orderingOptionals, Ordering::intersectEqualityBoundKeys);
if (commonEqualityBoundKeysMapOptional.isEmpty()) {
return Optional.empty();
}
final var commonEqualityBoundKeysMap = commonEqualityBoundKeysMapOptional.get();
final Optional<List<KeyPart>> commonOrderingKeysOptional = Ordering.commonOrderingKeys(orderingOptionals, RequestedOrdering.preserve());
if (commonOrderingKeysOptional.isEmpty()) {
return Optional.empty();
}
final var commonOrderingKeys = commonOrderingKeysOptional.get().stream().filter(keyPart -> !commonEqualityBoundKeysMap.containsKey(keyPart.getNormalizedKeyExpression())).collect(ImmutableList.toImmutableList());
final var allAreDistinct = orderingOptionals.stream().allMatch(orderingOptional -> orderingOptional.map(Ordering::isDistinct).orElse(false));
return Optional.of(new Ordering(commonEqualityBoundKeysMap, commonOrderingKeys, allAreDistinct));
}
use of com.apple.foundationdb.record.query.plan.temp.ExpressionRef in project fdb-record-layer by FoundationDB.
the class PlannerGraphProperty method show.
/**
* Show the planner expression that and all the match candidates rendered in your default browser. This also
* shows {@link PartialMatch}es between references if they exist.
* @param renderSingleGroups iff true group references with just one member are not rendered
* @param queryPlanRootReference the planner expression to be rendered.
* @param matchCandidates a set of candidates for matching which should also be shown
* @return the word "done" (IntelliJ really likes a return of String).
*/
@Nonnull
public static String show(final boolean renderSingleGroups, @Nonnull final GroupExpressionRef<? extends RelationalExpression> queryPlanRootReference, @Nonnull final Set<MatchCandidate> matchCandidates) {
final PlannerGraph queryPlannerGraph = Objects.requireNonNull(queryPlanRootReference.acceptPropertyVisitor(forInternalShow(renderSingleGroups, true)));
final PlannerGraph.InternalPlannerGraphBuilder graphBuilder = queryPlannerGraph.derived();
final Map<MatchCandidate, PlannerGraph> matchCandidateMap = matchCandidates.stream().collect(ImmutableMap.toImmutableMap(Function.identity(), matchCandidate -> Objects.requireNonNull(matchCandidate.getTraversal().getRootReference().acceptPropertyVisitor(forInternalShow(renderSingleGroups)))));
matchCandidateMap.forEach((matchCandidate, matchCandidateGraph) -> graphBuilder.addGraph(matchCandidateGraph));
final ExpressionRefTraversal queryGraphTraversal = ExpressionRefTraversal.withRoot(queryPlanRootReference);
final Set<ExpressionRef<? extends RelationalExpression>> queryGraphRefs = queryGraphTraversal.getRefs();
queryGraphRefs.forEach(queryGraphRef -> {
for (final MatchCandidate matchCandidate : Sets.intersection(matchCandidates, queryGraphRef.getMatchCandidates())) {
final Set<PartialMatch> partialMatchesForCandidate = queryGraphRef.getPartialMatchesForCandidate(matchCandidate);
final PlannerGraph matchCandidatePlannerGraph = Objects.requireNonNull(matchCandidateMap.get(matchCandidate));
final Node queryRefNode = Objects.requireNonNull(queryPlannerGraph.getNodeForIdentity(queryGraphRef));
for (final PartialMatch partialMatchForCandidate : partialMatchesForCandidate) {
@Nullable final Node matchCandidateNode = matchCandidatePlannerGraph.getNodeForIdentity(partialMatchForCandidate.getCandidateRef());
// should always be true but we don't want to bail out for corrupt graphs
if (matchCandidateNode != null) {
graphBuilder.addEdge(queryRefNode, matchCandidateNode, new PartialMatchEdge());
}
}
}
});
final String dotString = exportToDot(graphBuilder.build(), queryPlannerGraph.getNetwork().nodes(), nestedClusterProvider -> matchCandidateMap.entrySet().stream().map(entry -> new NamedCluster(entry.getKey().getName(), entry.getValue().getNetwork().nodes(), nestedClusterProvider)).collect(Collectors.toList()));
return show(dotString);
}
use of com.apple.foundationdb.record.query.plan.temp.ExpressionRef 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))));
}
use of com.apple.foundationdb.record.query.plan.temp.ExpressionRef in project fdb-record-layer by FoundationDB.
the class PushDistinctBelowFilterRule method onMatch.
@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
final ExpressionRef<? extends RelationalExpression> inner = call.get(innerRefMatcher);
final Quantifier.Physical qun = call.get(innerQuantifierMatcher);
final RecordQueryPredicatesFilterPlan filterPlan = call.get(filterPlanMatcher);
final RecordQueryUnorderedPrimaryKeyDistinctPlan newDistinctPlan = new RecordQueryUnorderedPrimaryKeyDistinctPlan(Quantifier.physical(inner));
final Quantifier.Physical newQun = Quantifier.physical(call.ref(newDistinctPlan));
final List<QueryPredicate> rebasedPredicates = filterPlan.getPredicates().stream().map(queryPredicate -> queryPredicate.rebase(Quantifiers.translate(qun, newQun))).collect(ImmutableList.toImmutableList());
call.yield(call.ref(new RecordQueryPredicatesFilterPlan(newQun, rebasedPredicates)));
}
use of com.apple.foundationdb.record.query.plan.temp.ExpressionRef 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));
}
}
Aggregations