-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
OptionalMatchRemover.scala
210 lines (172 loc) · 9.1 KB
/
OptionalMatchRemover.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (c) 2002-2017 "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.v3_4.planner.logical
import org.neo4j.cypher.internal.util.v3_4.Rewritable._
import org.neo4j.cypher.internal.util.v3_4.{InputPosition, Rewriter, topDown}
import org.neo4j.cypher.internal.compiler.v3_4.phases.{CompilerContext, LogicalPlanState}
import org.neo4j.cypher.internal.frontend.v3_4.phases.CompilationPhaseTracer.CompilationPhase
import org.neo4j.cypher.internal.frontend.v3_4.phases.CompilationPhaseTracer.CompilationPhase.LOGICAL_PLANNING
import org.neo4j.cypher.internal.frontend.v3_4.phases.{Condition, Phase}
import org.neo4j.cypher.internal.ir.v3_4._
import org.neo4j.cypher.internal.v3_4.expressions._
import scala.annotation.tailrec
import scala.collection.mutable.ListBuffer
import scala.collection.{TraversableOnce, mutable}
case object OptionalMatchRemover extends PlannerQueryRewriter {
override def description: String = "remove optional match when possible"
override def postConditions: Set[Condition] = Set.empty
override def instance(ignored: CompilerContext): Rewriter = topDown(Rewriter.lift {
case RegularPlannerQuery(graph, proj@AggregatingQueryProjection(distinctExpressions, aggregations, _), tail)
if validAggregations(aggregations) =>
val projectionDeps: Iterable[Variable] = (distinctExpressions.values ++ aggregations.values).flatMap(_.dependencies)
rewrite(projectionDeps, graph, proj, tail)
case RegularPlannerQuery(graph, proj@DistinctQueryProjection(distinctExpressions, _), tail) =>
val projectionDeps: Iterable[Variable] = distinctExpressions.values.flatMap(_.dependencies)
rewrite(projectionDeps, graph, proj, tail)
})
private def rewrite(projectionDeps: Iterable[Variable], graph: QueryGraph, proj: QueryProjection, tail: Option[PlannerQuery]): RegularPlannerQuery = {
val updateDeps = graph.mutatingPatterns.flatMap(_.dependencies)
val dependencies: Set[IdName] = projectionDeps.map(IdName.fromVariable).toSet ++ updateDeps
val optionalMatches = graph.optionalMatches.flatMapWithTail {
(original: QueryGraph, tail: Seq[QueryGraph]) =>
//The dependencies on an optional match are:
val allDeps =
// dependencies from optional matches listed later in the query
tail.flatMap(g => g.argumentIds ++ g.selections.variableDependencies).toSet ++
// any dependencies from the next horizon
dependencies --
// But we don't need to solve variables already present by the non-optional part of the QG
graph.coveredIds
val mustInclude = allDeps -- original.argumentIds
val mustKeep = original.smallestGraphIncluding(mustInclude)
if (mustKeep.isEmpty)
// We did not find anything in this OPTIONAL MATCH. Since there are no variable deps from this clause,
// and it can't change cardinality, it's safe to ignore it
None
else {
val (predicatesForPatterns, remaining) = {
val elementsToKeep1 = original.smallestGraphIncluding(mustInclude ++ original.argumentIds)
partitionPredicates(original.selections.predicates, elementsToKeep1)
}
val elementsToKeep = {
val variablesNeededForPredicates =
remaining.flatMap(expression => expression.dependencies.map(IdName.fromVariable))
original.smallestGraphIncluding(mustInclude ++ original.argumentIds ++ variablesNeededForPredicates)
}
val (patternsToKeep, patternsToFilter) = original.patternRelationships.partition(r => elementsToKeep(r.name))
val patternNodes = original.patternNodes.filter(elementsToKeep.apply)
val patternPredicates = patternsToFilter.map(toAst(elementsToKeep, predicatesForPatterns, _))
val newOptionalGraph = original.
withPatternRelationships(patternsToKeep).
withPatternNodes(patternNodes).
withSelections(Selections.from(remaining) ++ patternPredicates)
Some(newOptionalGraph)
}
}
val matches = graph.withOptionalMatches(optionalMatches)
RegularPlannerQuery(matches, horizon = proj, tail = tail)
}
private object LabelsAndEquality {
def empty = new LabelsAndEquality(Seq.empty, Seq.empty)
}
private case class LabelsAndEquality(labels: Seq[LabelName], equality: Seq[(PropertyKeyName, Expression)])
/**
* This method extracts predicates that need to be part of pattern expressions
*
* @param predicates All the original predicates of the QueryGraph
* @param kept Set of all variables that should not be moved to pattern expressions
* @return Map of label and property equality comparisons to move to pattern expressions,
* and the set of remaining predicates
*/
private def partitionPredicates(predicates: Set[Predicate], kept: Set[IdName]): (Map[IdName, LabelsAndEquality], Set[Expression]) = {
val patternPredicates = mutable.Map.empty[IdName, LabelsAndEquality]
val predicatesToKeep = mutable.Set.empty[Expression]
def addLabel(idName: IdName, labelName: LabelName) = {
val current = patternPredicates.getOrElse(idName, LabelsAndEquality.empty)
patternPredicates += idName -> current.copy(labels = current.labels :+ labelName)
}
def addProperty(idName: IdName, prop: PropertyKeyName, rhs: Expression) = {
val current = patternPredicates.getOrElse(idName, LabelsAndEquality.empty)
patternPredicates += idName -> current.copy(equality = current.equality :+ prop -> rhs)
}
predicates.foreach {
case Predicate(deps, HasLabels(Variable(_), labels)) if deps.size == 1 && !kept(deps.head) =>
assert(labels.size == 1) // We know there is only a single label here because AST rewriting
addLabel(deps.head, labels.head)
case Predicate(deps, Equals(Property(Variable(_), prop), rhs)) if deps.size == 1 && !kept(deps.head) =>
addProperty(deps.head, prop, rhs)
case Predicate(_, expr) =>
predicatesToKeep += expr
}
(patternPredicates.toMap, predicatesToKeep.toSet)
}
private def validAggregations(aggregations: Map[String, Expression]) =
aggregations.isEmpty ||
aggregations.values.forall {
case func: FunctionInvocation => func.distinct
case _ => false
}
private def toAst(elementsToKeep: Set[IdName], predicates: Map[IdName, LabelsAndEquality], pattern: PatternRelationship) = {
val pos = InputPosition.NONE
def createVariable(name: IdName): Option[Variable] =
if (!elementsToKeep(name))
None
else {
Some(Variable(name.name)(pos))
}
def createNode(name: IdName): NodePattern = {
val labelsAndProps = predicates.getOrElse(name, LabelsAndEquality.empty)
val props = if (labelsAndProps.equality.isEmpty) None else Some(MapExpression(labelsAndProps.equality)(pos))
NodePattern(createVariable(name), labels = labelsAndProps.labels, properties = props)(pos)
}
val relName = createVariable(pattern.name)
val leftNode = createNode(pattern.nodes._1)
val rightNode = createNode(pattern.nodes._2)
val relPattern = RelationshipPattern(relName, pattern.types, length = None, properties = None, pattern.dir)(pos)
val chain = RelationshipChain(leftNode, relPattern, rightNode)(pos)
PatternExpression(RelationshipsPattern(chain)(pos))
}
implicit class FlatMapWithTailable(in: IndexedSeq[QueryGraph]) {
def flatMapWithTail(f: (QueryGraph, Seq[QueryGraph]) => TraversableOnce[QueryGraph]): IndexedSeq[QueryGraph] = {
@tailrec
def recurse(that: QueryGraph, rest: Seq[QueryGraph], builder: mutable.Builder[QueryGraph, ListBuffer[QueryGraph]]): Unit = {
builder ++= f(that, rest)
if (rest.nonEmpty)
recurse(rest.head, rest.tail, builder)
}
if (in.isEmpty)
IndexedSeq.empty
else {
val builder = ListBuffer.newBuilder[QueryGraph]
recurse(in.head, in.tail, builder)
builder.result().toIndexedSeq
}
}
}
}
trait PlannerQueryRewriter extends Phase[CompilerContext, LogicalPlanState, LogicalPlanState] {
override def phase: CompilationPhase = LOGICAL_PLANNING
def instance(context: CompilerContext): Rewriter
override def process(from: LogicalPlanState, context: CompilerContext): LogicalPlanState = {
val query: UnionQuery = from.unionQuery
val rewritten = query.endoRewrite(instance(context))
from.copy(maybeUnionQuery = Some(rewritten))
}
}