Search in sources :

Example 1 with ExpressionRefTraversal

use of com.apple.foundationdb.record.query.plan.temp.ExpressionRefTraversal 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);
}
Also used : ExpressionRefTraversal(com.apple.foundationdb.record.query.plan.temp.ExpressionRefTraversal) PartialMatchEdge(com.apple.foundationdb.record.query.plan.temp.explain.PlannerGraph.PartialMatchEdge) Iterables(com.google.common.collect.Iterables) GroupExpressionRef(com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) ComponentIdProvider(com.apple.foundationdb.record.query.plan.temp.explain.GraphExporter.ComponentIdProvider) Function(java.util.function.Function) PlannerProperty(com.apple.foundationdb.record.query.plan.temp.PlannerProperty) PartialMatch(com.apple.foundationdb.record.query.plan.temp.PartialMatch) ImmutableList(com.google.common.collect.ImmutableList) CharStreams(com.google.common.io.CharStreams) Map(java.util.Map) ImmutableNetwork(com.google.common.graph.ImmutableNetwork) MatchCandidate(com.apple.foundationdb.record.query.plan.temp.MatchCandidate) URI(java.net.URI) ExpressionRef(com.apple.foundationdb.record.query.plan.temp.ExpressionRef) Network(com.google.common.graph.Network) Edge(com.apple.foundationdb.record.query.plan.temp.explain.PlannerGraph.Edge) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) PrintWriter(java.io.PrintWriter) Desktop(java.awt.Desktop) Verify(com.google.common.base.Verify) ImmutableSet(com.google.common.collect.ImmutableSet) Cluster(com.apple.foundationdb.record.query.plan.temp.explain.GraphExporter.Cluster) ImmutableMap(com.google.common.collect.ImmutableMap) Node(com.apple.foundationdb.record.query.plan.temp.explain.PlannerGraph.Node) StringWriter(java.io.StringWriter) Collection(java.util.Collection) Debugger(com.apple.foundationdb.record.query.plan.temp.debug.Debugger) Throwables(com.google.common.base.Throwables) Set(java.util.Set) Maps(com.google.common.collect.Maps) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) Objects(java.util.Objects) List(java.util.List) Writer(java.io.Writer) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) InputStream(java.io.InputStream) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) PartialMatchEdge(com.apple.foundationdb.record.query.plan.temp.explain.PlannerGraph.PartialMatchEdge) ExpressionRefTraversal(com.apple.foundationdb.record.query.plan.temp.ExpressionRefTraversal) Node(com.apple.foundationdb.record.query.plan.temp.explain.PlannerGraph.Node) GroupExpressionRef(com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef) ExpressionRef(com.apple.foundationdb.record.query.plan.temp.ExpressionRef) PartialMatch(com.apple.foundationdb.record.query.plan.temp.PartialMatch) MatchCandidate(com.apple.foundationdb.record.query.plan.temp.MatchCandidate) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Example 2 with ExpressionRefTraversal

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

the class MatchLeafRule method onMatch.

@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
    final PlanContext context = call.getContext();
    final RelationalExpression expression = call.get(root);
    // iterate through all candidates known to the context
    for (final MatchCandidate matchCandidate : context.getMatchCandidates()) {
        final ExpressionRefTraversal traversal = matchCandidate.getTraversal();
        final Set<ExpressionRef<? extends RelationalExpression>> leafRefs = traversal.getLeafReferences();
        // iterate through all leaf references in all
        for (final ExpressionRef<? extends RelationalExpression> leafRef : leafRefs) {
            for (final RelationalExpression leafMember : leafRef.getMembers()) {
                // expressions.
                if (leafMember.getQuantifiers().isEmpty()) {
                    final Iterable<BoundMatch<MatchInfo>> boundMatchInfos = matchWithCandidate(expression, leafMember);
                    // yield any match to the planner
                    boundMatchInfos.forEach(boundMatchInfo -> call.yieldPartialMatch(boundMatchInfo.getAliasMap(), matchCandidate, expression, leafRef, boundMatchInfo.getMatchResult()));
                }
            }
        }
    }
}
Also used : RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) ExpressionRefTraversal(com.apple.foundationdb.record.query.plan.temp.ExpressionRefTraversal) ExpressionRef(com.apple.foundationdb.record.query.plan.temp.ExpressionRef) PlanContext(com.apple.foundationdb.record.query.plan.temp.PlanContext) BoundMatch(com.apple.foundationdb.record.query.plan.temp.matching.BoundMatch) MatchCandidate(com.apple.foundationdb.record.query.plan.temp.MatchCandidate)

Aggregations

ExpressionRef (com.apple.foundationdb.record.query.plan.temp.ExpressionRef)2 ExpressionRefTraversal (com.apple.foundationdb.record.query.plan.temp.ExpressionRefTraversal)2 MatchCandidate (com.apple.foundationdb.record.query.plan.temp.MatchCandidate)2 RelationalExpression (com.apple.foundationdb.record.query.plan.temp.RelationalExpression)2 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)1 GroupExpressionRef (com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef)1 PartialMatch (com.apple.foundationdb.record.query.plan.temp.PartialMatch)1 PlanContext (com.apple.foundationdb.record.query.plan.temp.PlanContext)1 PlannerProperty (com.apple.foundationdb.record.query.plan.temp.PlannerProperty)1 Debugger (com.apple.foundationdb.record.query.plan.temp.debug.Debugger)1 Cluster (com.apple.foundationdb.record.query.plan.temp.explain.GraphExporter.Cluster)1 ComponentIdProvider (com.apple.foundationdb.record.query.plan.temp.explain.GraphExporter.ComponentIdProvider)1 Edge (com.apple.foundationdb.record.query.plan.temp.explain.PlannerGraph.Edge)1 Node (com.apple.foundationdb.record.query.plan.temp.explain.PlannerGraph.Node)1 PartialMatchEdge (com.apple.foundationdb.record.query.plan.temp.explain.PlannerGraph.PartialMatchEdge)1 BoundMatch (com.apple.foundationdb.record.query.plan.temp.matching.BoundMatch)1 Preconditions (com.google.common.base.Preconditions)1 Throwables (com.google.common.base.Throwables)1 Verify (com.google.common.base.Verify)1 ImmutableList (com.google.common.collect.ImmutableList)1