/
QueryPlanner.scala
122 lines (100 loc) · 5.14 KB
/
QueryPlanner.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Copyright (c) 2002-2015 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.cypher.internal.compiler.v2_2.planner.logical
import org.neo4j.cypher.internal.compiler.v2_2._
import org.neo4j.cypher.internal.compiler.v2_2.planner._
import org.neo4j.cypher.internal.compiler.v2_2.planner.logical.plans.LogicalPlan
import org.neo4j.cypher.internal.compiler.v2_2.planner.logical.plans.rewriter.LogicalPlanRewriter
import org.neo4j.cypher.internal.compiler.v2_2.planner.logical.steps.{aggregation, projection, sortSkipAndLimit, verifyBestPlan}
import org.neo4j.cypher.internal.compiler.v2_2.tracing.rewriters.RewriterStepSequencer
trait QueryPlanner {
def plan(plannerQuery: UnionQuery)(implicit context: LogicalPlanningContext): LogicalPlan
}
class DefaultQueryPlanner(planRewriter: Rewriter,
config: QueryPlannerConfiguration = QueryPlannerConfiguration.default,
expressionRewriterFactory: (LogicalPlanningContext => Rewriter) = ExpressionRewriterFactory)
extends QueryPlanner {
def plan(unionQuery: UnionQuery)(implicit context: LogicalPlanningContext): LogicalPlan = unionQuery match {
case UnionQuery(queries, distinct) =>
val plan = planQuery(queries, distinct)
plan.endoRewrite(planRewriter)
case _ =>
throw new CantHandleQueryException
}
private def planQuery(queries: Seq[PlannerQuery], distinct: Boolean)(implicit context: LogicalPlanningContext) = {
val logicalPlans: Seq[LogicalPlan] = queries.map(p => planSingleQuery(p))
val unionPlan = logicalPlans.reduce[LogicalPlan] {
case (p1, p2) => context.logicalPlanProducer.planUnion(p1, p2)
}
if (distinct)
context.logicalPlanProducer.planDistinct(unionPlan)
else
unionPlan
}
protected def planSingleQuery(query: PlannerQuery)(implicit context: LogicalPlanningContext): LogicalPlan = {
val partPlan = planPart(query, context, None)
val projectedPlan = planEventHorizon(query, partPlan)
val projectedContext = context.recurse(projectedPlan)
val expressionRewriter = expressionRewriterFactory(projectedContext)
val completePlan = projectedPlan.endoRewrite(expressionRewriter)
val finalPlan = planWithTail(completePlan, query.tail)(projectedContext)
verifyBestPlan(finalPlan, query)
}
private def planWithTail(pred: LogicalPlan, remaining: Option[PlannerQuery])(implicit context: LogicalPlanningContext): LogicalPlan =
remaining match {
case Some(query) =>
val lhs = pred
val lhsContext = context.recurse(lhs)
val rhs = planPart(query, lhsContext, Some(context.logicalPlanProducer.planQueryArgumentRow(query.graph)))
val applyPlan = context.logicalPlanProducer.planTailApply(lhs, rhs)
val applyContext = lhsContext.recurse(applyPlan)
val projectedPlan = planEventHorizon(query, applyPlan)(applyContext)
val projectedContext = applyContext.recurse(projectedPlan)
val expressionRewriter = expressionRewriterFactory(projectedContext)
val completePlan = projectedPlan.endoRewrite(expressionRewriter)
// planning nested expressions doesn't change outer cardinality
planWithTail(completePlan, query.tail)(projectedContext)
case None =>
pred
}
private def planPart(query: PlannerQuery, context: LogicalPlanningContext, leafPlan: Option[LogicalPlan]): LogicalPlan = {
val ctx = query.preferredStrictness match {
case Some(mode) if context.input.strictness.exists(mode == _) => context.withStrictness(mode)
case _ => context
}
ctx.strategy.plan(query.graph)(ctx, leafPlan)
}
private def planEventHorizon(query: PlannerQuery, plan: LogicalPlan)(implicit context: LogicalPlanningContext): LogicalPlan = {
val selectedPlan = config.applySelections(plan, query.graph)
val projectedPlan = query.horizon match {
case aggregatingProjection: AggregatingQueryProjection =>
val aggregationPlan = aggregation(selectedPlan, aggregatingProjection)
sortSkipAndLimit(aggregationPlan, query)
case queryProjection: RegularQueryProjection =>
val sortedAndLimited = sortSkipAndLimit(selectedPlan, query)
projection(sortedAndLimited, queryProjection.projections, intermediate = query.tail.isDefined)
case UnwindProjection(identifier, expression) =>
context.logicalPlanProducer.planUnwind(plan, identifier, expression)
case _ =>
throw new CantHandleQueryException
}
projectedPlan
}
}