forked from eldersantos/community
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Expression.scala
278 lines (238 loc) · 11 KB
/
Expression.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* Copyright (c) 2002-2012 "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.commands
import java.lang.String
import org.neo4j.cypher._
import internal.symbols._
import org.neo4j.graphdb.{NotFoundException, PropertyContainer}
import collection.Map
abstract class Expression extends (Map[String, Any] => Any) {
protected def compute(v1: Map[String, Any]) : Any
def apply(m: Map[String, Any]) = m.getOrElse(identifier.name, compute(m))
val identifier: Identifier
def declareDependencies(expectedType: AnyType): Seq[Identifier]
def dependencies(expectedType: AnyType): Seq[Identifier] = {
val myType = identifier.typ
if (!expectedType.isAssignableFrom(myType) &&
!myType.isAssignableFrom(expectedType))
throw new SyntaxException("`%s` expected to be a %s but it is a %s".format(identifier.name, expectedType, identifier.typ))
declareDependencies(expectedType)
}
def rewrite(f: Expression => Expression): Expression
def exists(f: Expression => Boolean) = filter(f).nonEmpty
def filter(f: Expression => Boolean): Seq[Expression]
def subExpressions = filter( _ != this)
def containsAggregate = exists(_.isInstanceOf[AggregationExpression])
override def toString() = identifier.name
}
case class CachedExpression(key:String, identifier:Identifier) extends CastableExpression {
override def apply(m: Map[String, Any]) = m(key)
protected def compute(v1: Map[String, Any]) = null
def declareDependencies(extectedType: AnyType) = Seq()
def rewrite(f: (Expression) => Expression) = f(this)
def filter(f: (Expression) => Boolean) = if(f(this)) Seq(this) else Seq()
override def toString() = "Cached(" + super.toString() + ")"
}
case class Null() extends Expression {
protected def compute(v1: Map[String, Any]) = null
val identifier: Identifier = Identifier("null", ScalarType())
def declareDependencies(extectedType: AnyType): Seq[Identifier] = Seq()
def rewrite(f: (Expression) => Expression): Expression = f(this)
def filter(f: (Expression) => Boolean): Seq[Expression] = if(f(this)) Seq(this) else Seq()
}
case class Add(a: Expression, b: Expression) extends Expression {
val identifier = Identifier("%s + %s".format(a.identifier.name, b.identifier.name), myType)
private def myType = if(a.identifier.typ.isAssignableFrom(b.identifier.typ))
a.identifier.typ
else if(b.identifier.typ.isAssignableFrom(a.identifier.typ))
b.identifier.typ
else ScalarType()
def compute(m: Map[String, Any]) = {
val aVal = a(m)
val bVal = b(m)
(aVal, bVal) match {
case (x: Number, y: Number) => x.doubleValue() + y.doubleValue()
case (x: String, y: String) => x + y
case (IsIterable(x), IsIterable(y)) => x ++ y
case (IsIterable(x), y) => x ++ Seq(y)
case (x, IsIterable(y)) => Seq(x) ++ y
case _ => throw new CypherTypeException("Don't know how to add `" + aVal.toString + "` and `" + bVal.toString + "`")
}
}
def declareDependencies(extectedType: AnyType) = a.declareDependencies(extectedType) ++ b.declareDependencies(extectedType)
def rewrite(f: (Expression) => Expression) = f(Add(a.rewrite(f), b.rewrite(f)))
def filter(f: (Expression) => Boolean) = if(f(this))
Seq(this) ++ a.filter(f) ++ b.filter(f)
else
a.filter(f) ++ b.filter(f)
}
case class Subtract(a: Expression, b: Expression) extends Arithmetics(a, b) {
def operand = "-"
def verb = "subtract"
def stringWithString(a: String, b: String) = throwTypeError(a, b)
def numberWithNumber(a: Number, b: Number) = a.doubleValue() - b.doubleValue()
def rewrite(f: (Expression) => Expression) = f(Subtract(a.rewrite(f), b.rewrite(f)))
}
case class Modulo(a: Expression, b: Expression) extends Arithmetics(a, b) {
def operand = "%"
def verb = "modulo"
def stringWithString(a: String, b: String) = throwTypeError(a, b)
def numberWithNumber(a: Number, b: Number) = a.doubleValue() % b.doubleValue()
def rewrite(f: (Expression) => Expression) = f(Modulo(a.rewrite(f), b.rewrite(f)))
}
case class Pow(a: Expression, b: Expression) extends Arithmetics(a, b) {
def operand = "^"
def verb = "power"
def stringWithString(a: String, b: String) = throwTypeError(a, b)
def numberWithNumber(a: Number, b: Number) = math.pow(a.doubleValue(), b.doubleValue())
def rewrite(f: (Expression) => Expression) = f(Pow(a.rewrite(f), b.rewrite(f)))
}
case class Multiply(a: Expression, b: Expression) extends Arithmetics(a, b) {
def operand = "*"
def verb = "multiply"
def stringWithString(a: String, b: String) = throwTypeError(a, b)
def numberWithNumber(a: Number, b: Number) = a.doubleValue() * b.doubleValue()
def rewrite(f: (Expression) => Expression) = f(Multiply(a.rewrite(f), b.rewrite(f)))
}
case class Divide(a: Expression, b: Expression) extends Arithmetics(a, b) {
def operand = "/"
def verb = "divide"
def stringWithString(a: String, b: String) = throwTypeError(a, b)
def numberWithNumber(a: Number, b: Number) = a.doubleValue() / b.doubleValue()
def rewrite(f: (Expression) => Expression) = f(Divide(a.rewrite(f), b.rewrite(f)))
}
abstract class Arithmetics(left: Expression, right: Expression) extends Expression {
val identifier = Identifier("%s %s %s".format(left.identifier.name, operand, right.identifier.name), NumberType())
def operand: String
def throwTypeError(bVal: Any, aVal: Any): Nothing = {
throw new CypherTypeException("Don't know how to " + verb + " `" + name(bVal) + "` with `" + name(aVal) + "`")
}
private def name(x:Any)=x match {
case null => "null"
case _ => x.toString
}
def compute(m: Map[String, Any]) = {
val aVal = left(m)
val bVal = right(m)
(aVal, bVal) match {
case (x: Number, y: Number) => numberWithNumber(x, y)
case (x: String, y: String) => stringWithString(x, y)
case _ => throwTypeError(bVal, aVal)
}
}
def verb: String
def stringWithString(a: String, b: String): String
def numberWithNumber(a: Number, b: Number): Number
def declareDependencies(extectedType: AnyType) = left.declareDependencies(extectedType) ++ right.declareDependencies(extectedType)
def filter(f: (Expression) => Boolean) = if(f(this))
Seq(this) ++ left.filter(f) ++ right.filter(f)
else
left.filter(f) ++ right.filter(f)
}
case class Literal(v: Any) extends Expression {
def compute(m: Map[String, Any]) = v
override def apply(m: Map[String, Any]): Any = compute(m)
val identifier = Identifier(name, AnyType.fromJava(v))
private def name = v match {
case null => "null"
case x => x.toString
}
def declareDependencies(extectedType: AnyType): Seq[Identifier] = Seq()
def rewrite(f: (Expression) => Expression) = f(this)
def filter(f: (Expression) => Boolean) = if(f(this))
Seq(this)
else
Seq()
}
abstract class CastableExpression extends Expression {
override def dependencies(extectedType: AnyType): Seq[Identifier] = declareDependencies(extectedType)
}
case class Nullable(expression: Expression) extends Expression {
val identifier = Identifier(expression.identifier.name + "?", expression.identifier.typ)
def compute(m: Map[String, Any]) = try {
expression.apply(m)
} catch {
case x: EntityNotFoundException => null
}
def declareDependencies(extectedType: AnyType) = expression.dependencies(extectedType)
override def dependencies(extectedType: AnyType) = expression.dependencies(extectedType)
def rewrite(f: (Expression) => Expression) = f(Nullable(expression.rewrite(f)))
def filter(f: (Expression) => Boolean) = if(f(this))
Seq(this) ++ expression.filter(f)
else
expression.filter(f)
}
case class Property(entity: String, property: String) extends CastableExpression {
def compute(m: Map[String, Any]): Any = {
m(entity).asInstanceOf[PropertyContainer] match {
case null => null
case propertyContainer => try {
propertyContainer.getProperty(property)
} catch {
case x: NotFoundException => throw new EntityNotFoundException("The property '%s' does not exist on %s".format(property, propertyContainer), x)
}
}
}
val identifier: Identifier = Identifier(entity + "." + property, AnyType())
def declareDependencies(extectedType: AnyType): Seq[Identifier] = Seq(Identifier(entity, MapType()))
def rewrite(f: (Expression) => Expression) = f(this)
def filter(f: (Expression) => Boolean) = if(f(this))
Seq(this)
else
Seq()
}
case class Entity(entityName: String) extends CastableExpression {
def compute(m: Map[String, Any]): Any = m.getOrElse(entityName, throw new NotFoundException("Failed to find `" + entityName + "`"))
val identifier: Identifier = Identifier(entityName, AnyType())
override def toString(): String = entityName
def declareDependencies(extectedType: AnyType): Seq[Identifier] = Seq(Identifier(entityName, extectedType))
def rewrite(f: (Expression) => Expression) = f(this)
def filter(f: (Expression) => Boolean) = if(f(this))
Seq(this)
else
Seq()
}
case class Collection(expressions:Expression*) extends CastableExpression {
def compute(m: Map[String, Any]): Any = expressions.map(e=>e(m))
val identifier: Identifier = Identifier(name, AnyIterableType())
private def name = expressions.map(_.identifier.name).mkString("[", ", ", "]")
def declareDependencies(extectedType: AnyType): Seq[Identifier] = expressions.flatMap(_.declareDependencies(AnyType()))
def rewrite(f: (Expression) => Expression): Expression = f(Collection(expressions.map(f):_*))
def filter(f: (Expression) => Boolean): Seq[Expression] = if(f(this))
Seq(this) ++ expressions.flatMap(_.filter(f))
else
expressions.flatMap(_.filter(f))
}
case class ParameterExpression(parameterName: String) extends CastableExpression {
def compute(m: Map[String, Any]): Any = m.getOrElse("-=PARAMETER=-" + parameterName + "-=PARAMETER=-", throw new ParameterNotFoundException("Expected a parameter named " + parameterName)) match {
case ParameterValue(x) => x
case _ => throw new ParameterNotFoundException("Expected a parameter named " + parameterName)
}
override def apply(m: Map[String, Any]) = compute(m)
val identifier: Identifier = Identifier(parameterName, AnyType())
override def toString(): String = "{" + parameterName + "}"
def declareDependencies(extectedType: AnyType): Seq[Identifier] = Seq()
def rewrite(f: (Expression) => Expression) = f(this)
def filter(f: (Expression) => Boolean) = if(f(this))
Seq(this)
else
Seq()
}
case class ParameterValue(value: Any)