-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Sargable.scala
247 lines (195 loc) · 8.23 KB
/
Sargable.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (c) 2002-2016 "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_1.planner.logical.plans
import org.neo4j.cypher.internal.compiler.v3_1._
import org.neo4j.cypher.internal.compiler.v3_1.ast.convert.commands.ExpressionConverters._
import org.neo4j.cypher.internal.compiler.v3_1.ast.{InequalitySeekRangeWrapper, PrefixSeekRangeWrapper}
import org.neo4j.cypher.internal.compiler.v3_1.commands.{ManyQueryExpression, QueryExpression, RangeQueryExpression, SingleQueryExpression}
import org.neo4j.cypher.internal.compiler.v3_1.helpers._
import org.neo4j.cypher.internal.compiler.v3_1.pipes.{ManySeekArgs, SeekArgs, SingleSeekArg}
import org.neo4j.cypher.internal.frontend.v3_1.ast._
import org.neo4j.cypher.internal.frontend.v3_1.{ExclusiveBound, InclusiveBound}
object WithSeekableArgs {
def unapply(v: Any) = v match {
case In(lhs, rhs) => Some(lhs -> ManySeekableArgs(rhs))
case Equals(lhs, rhs) => Some(lhs -> SingleSeekableArg(rhs))
case _ => None
}
}
object AsIdSeekable {
def unapply(v: Any) = v match {
case WithSeekableArgs(func@UserFunctionInvocation(_, _, _, IndexedSeq(ident: Variable)), rhs)
if func.function == functions.Id && !rhs.dependencies(ident) =>
Some(IdSeekable(func, ident, rhs))
case _ =>
None
}
}
object AsPropertySeekable {
def unapply(v: Any) = v match {
case WithSeekableArgs(prop@Property(ident: Variable, propertyKey), rhs)
if !rhs.dependencies(ident) =>
Some(PropertySeekable(prop, ident, rhs))
case _ =>
None
}
}
object AsPropertyScannable {
def unapply(v: Any): Option[Scannable[Expression]] = v match {
case func@UserFunctionInvocation(_, _, _, IndexedSeq(property@Property(ident: Variable, _)))
if func.function == functions.Exists =>
Some(ExplicitlyPropertyScannable(func, ident, property))
case expr: Equals =>
partialPropertyPredicate(expr, expr.lhs)
case expr: InequalityExpression =>
partialPropertyPredicate(expr, expr.lhs)
case startsWith: StartsWith =>
partialPropertyPredicate(startsWith, startsWith.lhs)
case regex: RegexMatch =>
partialPropertyPredicate(regex, regex.lhs)
case expr: NotEquals =>
partialPropertyPredicate(expr, expr.lhs)
case _ =>
None
}
private def partialPropertyPredicate[P <: Expression](predicate: P, lhs: Expression) = lhs match {
case property@Property(ident: Variable, _) =>
PartialPredicate.ifNotEqual(
UserFunctionInvocation(FunctionName(functions.Exists.name)(predicate.position), property)(predicate.position),
predicate
).map(ImplicitlyPropertyScannable(_, ident, property))
case _ =>
None
}
}
object AsStringRangeSeekable {
def unapply(v: Any): Option[PrefixRangeSeekable] = v match {
case startsWith@StartsWith(Property(ident: Variable, propertyKey), lit@StringLiteral(prefix)) if prefix.nonEmpty =>
Some(PrefixRangeSeekable(PrefixRange(lit), startsWith, ident, propertyKey))
case startsWith@StartsWith(Property(ident: Variable, propertyKey), rhs) =>
Some(PrefixRangeSeekable(PrefixRange(rhs), startsWith, ident, propertyKey))
case _ =>
None
}
}
object AsValueRangeSeekable {
def unapply(v: Any): Option[InequalityRangeSeekable] = v match {
case inequalities@AndedPropertyInequalities(ident, prop, innerInequalities)
if innerInequalities.forall( _.rhs.dependencies.isEmpty ) =>
Some(InequalityRangeSeekable(ident, prop.propertyKey, inequalities))
case _ =>
None
}
}
sealed trait Sargable[+T <: Expression] {
def expr: T
def ident: Variable
def name = ident.name
}
sealed trait Seekable[T <: Expression] extends Sargable[T] {
def dependencies: Set[Variable]
}
sealed trait EqualitySeekable[T <: Expression] extends Seekable[T] {
def args: SeekableArgs
}
case class IdSeekable(expr: UserFunctionInvocation, ident: Variable, args: SeekableArgs)
extends EqualitySeekable[UserFunctionInvocation] {
def dependencies = args.dependencies
}
case class PropertySeekable(expr: Property, ident: Variable, args: SeekableArgs)
extends EqualitySeekable[Property] {
def propertyKey = expr.propertyKey
def dependencies = args.dependencies
}
sealed trait RangeSeekable[T <: Expression, V] extends Seekable[T] {
def range: SeekRange[V]
}
case class PrefixRangeSeekable(override val range: PrefixRange[Expression], expr: StartsWith, ident: Variable, propertyKey: PropertyKeyName)
extends RangeSeekable[StartsWith, Expression] {
def dependencies = Set.empty
def asQueryExpression: QueryExpression[Expression] =
RangeQueryExpression(PrefixSeekRangeWrapper(range)(expr.rhs.position))
}
case class InequalityRangeSeekable(ident: Variable, propertyKeyName: PropertyKeyName, expr: AndedPropertyInequalities)
extends RangeSeekable[AndedPropertyInequalities, Expression] {
def dependencies = expr.inequalities.map(_.dependencies).toSet.flatten
def range: InequalitySeekRange[Expression] =
InequalitySeekRange.fromPartitionedBounds(expr.inequalities.partition {
case GreaterThan(_, value) => Left(ExclusiveBound(value))
case GreaterThanOrEqual(_, value) => Left(InclusiveBound(value))
case LessThan(_, value) => Right(ExclusiveBound(value))
case LessThanOrEqual(_, value) => Right(InclusiveBound(value))
})
def hasEquality: Boolean = expr.inequalities.map(_.includeEquality).reduceLeft(_ || _)
def asQueryExpression: QueryExpression[Expression] =
RangeQueryExpression(InequalitySeekRangeWrapper(range)(ident.position))
}
sealed trait Scannable[+T <: Expression] extends Sargable[T] {
def ident: Variable
def property: Property
def propertyKey = property.propertyKey
}
case class ExplicitlyPropertyScannable(expr: UserFunctionInvocation, ident: Variable, property: Property)
extends Scannable[UserFunctionInvocation]
case class ImplicitlyPropertyScannable[+T <: Expression](expr: PartialPredicate[T], ident: Variable, property: Property)
extends Scannable[PartialPredicate[T]]
sealed trait SeekableArgs {
def expr: Expression
def sizeHint: Option[Int]
def dependencies: Set[Variable] = expr.dependencies
def mapValues(f: Expression => Expression): SeekableArgs
def asQueryExpression: QueryExpression[Expression]
def asCommandSeekArgs: SeekArgs
}
case class SingleSeekableArg(expr: Expression) extends SeekableArgs {
def sizeHint = Some(1)
override def mapValues(f: Expression => Expression) = copy(f(expr))
def asQueryExpression: SingleQueryExpression[Expression] = SingleQueryExpression(expr)
def asCommandSeekArgs: SeekArgs = SingleSeekArg(toCommandExpression(expr))
}
case class ManySeekableArgs(expr: Expression) extends SeekableArgs {
val sizeHint = expr match {
case coll: Collection => Some(coll.expressions.size)
case _ => None
}
override def mapValues(f: Expression => Expression) = expr match {
case coll: Collection => copy(expr = coll.map(f))
case _ => copy(expr = f(expr))
}
def asQueryExpression: QueryExpression[Expression] = expr match {
case coll: Collection =>
ZeroOneOrMany(coll.expressions) match {
case One(value) => SingleQueryExpression(value)
case _ => ManyQueryExpression(coll)
}
case _ =>
ManyQueryExpression(expr)
}
def asCommandSeekArgs: SeekArgs = expr match {
case coll: Collection =>
ZeroOneOrMany(coll.expressions) match {
case Zero => SeekArgs.empty
case One(value) => SingleSeekArg(toCommandExpression(value))
case Many(values) => ManySeekArgs(toCommandExpression(coll))
}
case _ =>
ManySeekArgs(toCommandExpression(expr))
}
}