Search in sources :

Example 1 with Debugger

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

the class PlannerGraphProperty method evaluateAtRef.

@Nonnull
@Override
public PlannerGraph evaluateAtRef(@Nonnull final ExpressionRef<? extends RelationalExpression> ref, @Nonnull List<PlannerGraph> memberResults) {
    if (memberResults.isEmpty()) {
        // should not happen -- but we don't want to bail
        return PlannerGraph.builder(new PlannerGraph.ExpressionRefHeadNode(ref)).build();
    }
    if (removePlansIfPossible()) {
        final List<PlannerGraph> filteredMemberResults = memberResults.stream().filter(graph -> graph.getRoot() instanceof PlannerGraph.WithExpression).filter(graph -> {
            final RelationalExpression expression = ((PlannerGraph.WithExpression) graph.getRoot()).getExpression();
            return !(expression instanceof RecordQueryPlan);
        }).collect(Collectors.toList());
        // if we filtered down to empty it is better to just show the physical plan, otherwise try to avoid it
        if (!filteredMemberResults.isEmpty()) {
            memberResults = filteredMemberResults;
        }
    } else if (removeLogicalExpressions()) {
        final List<PlannerGraph> filteredMemberResults = memberResults.stream().filter(graph -> graph.getRoot() instanceof PlannerGraph.WithExpression).filter(graph -> ((PlannerGraph.WithExpression) graph.getRoot()).getExpression() instanceof RecordQueryPlan).collect(Collectors.toList());
        // if we filtered down to empty it is better to just show the physical plan, otherwise try to avoid it
        if (!filteredMemberResults.isEmpty()) {
            memberResults = filteredMemberResults;
        }
    }
    if (renderSingleGroups() || memberResults.size() > 1) {
        final Node head = new PlannerGraph.ExpressionRefHeadNode(ref);
        final PlannerGraph.InternalPlannerGraphBuilder plannerGraphBuilder = PlannerGraph.builder(head);
        final List<PlannerGraph> memberGraphs = memberResults.stream().map(childGraph -> {
            final Node root = childGraph.getRoot();
            final Optional<String> debugNameOptional = Debugger.mapDebugger(debugger -> {
                if (root instanceof PlannerGraph.WithExpression) {
                    final PlannerGraph.WithExpression withExpression = (PlannerGraph.WithExpression) root;
                    @Nullable final RelationalExpression expression = withExpression.getExpression();
                    return expression == null ? null : debugger.nameForObject(expression);
                }
                return null;
            });
            final Node member = debugNameOptional.map(PlannerGraph.ExpressionRefMemberNode::new).orElse(new PlannerGraph.ExpressionRefMemberNode());
            return PlannerGraph.builder(member).addGraph(childGraph).addEdge(root, member, new PlannerGraph.GroupExpressionRefEdge()).build();
        }).collect(Collectors.toList());
        memberGraphs.forEach(memberGraph -> {
            plannerGraphBuilder.addGraph(memberGraph);
            plannerGraphBuilder.addEdge(memberGraph.getRoot(), head, new PlannerGraph.GroupExpressionRefInternalEdge());
        });
        return plannerGraphBuilder.build();
    } else {
        // !renderSingleGroups && memberResults.size() == 1
        return Iterables.getOnlyElement(memberResults);
    }
}
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) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) Optional(java.util.Optional) Node(com.apple.foundationdb.record.query.plan.temp.explain.PlannerGraph.Node) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Nonnull(javax.annotation.Nonnull)

Aggregations

RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)1 ExpressionRef (com.apple.foundationdb.record.query.plan.temp.ExpressionRef)1 ExpressionRefTraversal (com.apple.foundationdb.record.query.plan.temp.ExpressionRefTraversal)1 GroupExpressionRef (com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef)1 MatchCandidate (com.apple.foundationdb.record.query.plan.temp.MatchCandidate)1 PartialMatch (com.apple.foundationdb.record.query.plan.temp.PartialMatch)1 PlannerProperty (com.apple.foundationdb.record.query.plan.temp.PlannerProperty)1 RelationalExpression (com.apple.foundationdb.record.query.plan.temp.RelationalExpression)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 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 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1