-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ReturnAcceptanceTest.scala
472 lines (359 loc) · 14.8 KB
/
ReturnAcceptanceTest.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
* 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.internal.cypher.acceptance
import org.neo4j.cypher._
import org.neo4j.cypher.internal.compiler.v3_1.commands.expressions.PathImpl
import org.neo4j.cypher.internal.compiler.v3_1.test_helpers.CustomMatchers
import org.neo4j.graphdb._
class ReturnAcceptanceTest extends ExecutionEngineFunSuite with CustomMatchers with NewPlannerTestSupport {
// TCK'd
test("returning properties of deleted nodes should throw exception") {
createNode("p" -> 0)
val query = "MATCH (n) DELETE n RETURN n.p"
an [EntityNotFoundException] should be thrownBy updateWithBothPlanners(query)
}
// TCK'd
test("returning labels of deleted nodes should throw exception") {
createLabeledNode("A")
val query = "MATCH (n:A) DELETE n RETURN labels(n)"
an [EntityNotFoundException] should be thrownBy updateWithBothPlanners(query)
}
// TCK'd
test("returning properties of deleted relationships should throw exception") {
relate(createNode(), createNode(), "T", Map("p" -> "a property"))
val query = "MATCH ()-[r]->() DELETE r RETURN r.p"
an [EntityNotFoundException] should be thrownBy updateWithBothPlanners(query)
}
// TCK'd
test("returning the type of deleted relationships should throw exception") {
relate(createNode(), createNode(), "T")
val query = "MATCH ()-[r:T]->() DELETE r RETURN type(r)"
an [EntityNotFoundException] should be thrownBy updateWithBothPlanners(query)
}
// TCK'd
test("should accept a valid unicode literal") {
val query = "RETURN '\\uf123' AS a"
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode(query)
result.toList should equal(List(Map("a" -> "\uF123")))
}
// TCK'd
test("limit 0 should return an empty result") {
createNode()
createNode()
createNode()
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("match (n) return n limit 0")
result should be(empty)
}
// TCK'd
test("should not support sorting on things after distinct has removed it") {
createNode("name" -> "A", "age" -> 13)
createNode("name" -> "B", "age" -> 12)
createNode("name" -> "C", "age" -> 11)
intercept[SyntaxException](executeWithAllPlannersAndCompatibilityMode( """
match (a) where id(a) in [1,2,3,1]
return distinct a.name
order by a.age""").toList)
}
// TCK'd
test("should not allow ordering on nodes") {
createNode()
createNode()
intercept[IncomparableValuesException](executeWithAllPlannersAndCompatibilityMode("match (n) where id(n) in [0,1] return n order by n").toList)
}
// TCK'd
test("expose problem with aliasing") {
createNode("nisse")
executeWithAllPlannersAndCompatibilityMode("match (n) return n.name, count(*) as foo order by n.name")
}
// TCK'd
test("distinct on nullable values") {
createNode("name" -> "Florescu")
createNode()
createNode()
val result = executeWithAllPlannersAndCompatibilityMode("match (a) return distinct a.name").toList
result should equal(List(Map("a.name" -> "Florescu"), Map("a.name" -> null)))
}
// TCK'd
test("return all variables") {
val a = createLabeledNode("Start")
val b = createNode()
val r = relate(a, b)
val result = executeWithAllPlannersAndCompatibilityMode("match p=(a:Start)-->(b) return *").toList
val first = result.head
first.keys should equal(Set("a", "b", "p"))
first("p") should equal(PathImpl(a, r, b))
}
// TCK'd
test("issue 508") {
createNode()
val q = "match (n) set n.x=[1,2,3] return length(n.x)"
updateWithBothPlannersAndCompatibilityMode(q).toList should equal(List(Map("length(n.x)" -> 3)))
}
test("long or double") {
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("return 1, 1.5").toList.head
result("1") should haveType[java.lang.Long]
result("1.5") should haveType[java.lang.Double]
}
// TCK'd
test("square function returns decimals") {
val result = executeWithAllPlannersAndCompatibilityMode("return sqrt(12.96)").toList
result should equal(List(Map("sqrt(12.96)" -> 3.6)))
}
// TCK'd
test("maths inside aggregation") {
val andres = createNode("name" -> "Andres")
val michael = createNode("name" -> "Michael")
val peter = createNode("name" -> "Peter")
val bread = createNode("type" -> "Bread")
val veg = createNode("type" -> "Veggies")
val meat = createNode("type" -> "Meat")
relate(andres, bread, "ATE", Map("times" -> 10))
relate(andres, veg, "ATE", Map("times" -> 8))
relate(michael, veg, "ATE", Map("times" -> 4))
relate(michael, bread, "ATE", Map("times" -> 6))
relate(michael, meat, "ATE", Map("times" -> 9))
relate(peter, veg, "ATE", Map("times" -> 7))
relate(peter, bread, "ATE", Map("times" -> 7))
relate(peter, meat, "ATE", Map("times" -> 4))
executeWithAllPlannersAndCompatibilityMode(
"""
match (me)-[r1:ATE]->(food)<-[r2:ATE]-(you)
where id(me) = 1
with me,count(distinct r1) as H1,count(distinct r2) as H2,you
match (me)-[r1:ATE]->(food)<-[r2:ATE]-(you)
return me,you,sum((1-ABS(r1.times/H1-r2.times/H2))*(r1.times+r2.times)/(H1+H2))""").dumpToString()
}
// TCK'd
test("should not be confused by rewriting about what is a relationship and what not") {
val andres = createNode("name" -> "Andres")
val michael = createNode("name" -> "Michael")
val peter = createNode("name" -> "Peter")
val bread = createNode("type" -> "Bread")
val veg = createNode("type" -> "Veggies")
val meat = createNode("type" -> "Meat")
relate(andres, bread, "ATE", Map("times" -> 10))
relate(andres, veg, "ATE", Map("times" -> 8))
relate(michael, veg, "ATE", Map("times" -> 4))
relate(michael, bread, "ATE", Map("times" -> 6))
relate(michael, meat, "ATE", Map("times" -> 9))
relate(peter, veg, "ATE", Map("times" -> 7))
relate(peter, bread, "ATE", Map("times" -> 7))
relate(peter, meat, "ATE", Map("times" -> 4))
executeWithAllPlannersAndCompatibilityMode(
"""
match (me)-[r1]->(you)
with 1 AS x
match (me)-[r1]->(food)<-[r2]-(you)
return r1.times""").dumpToString()
}
test("should return shortest paths if using a ridiculously unhip cypher") {
val a = createNode()
val b = createNode()
val c = createNode()
relate(a, b)
relate(b, c)
val result = eengine.execute("match (a), (c) where id(a) = 0 and id(c) = 2 return shortestPath((a)-[*]->(c))", Map.empty[String, Any], graph.session()).columnAs[Path]("shortestPath((a)-[*]->(c))").toList.head
result.endNode() should equal(c)
result.startNode() should equal(a)
result.length() should equal(2)
}
test("should return shortest path") {
val a = createLabeledNode("Start")
val b = createNode()
val c = createLabeledNode("End")
relate(a, b)
relate(b, c)
val result = executeWithAllPlannersAndCompatibilityMode("match (a:Start), (c:End) return shortestPath((a)-[*]->(c))").columnAs[Path]("shortestPath((a)-[*]->(c))").toList.head
result.endNode() should equal(c)
result.startNode() should equal(a)
result.length() should equal(2)
}
// TCK'd
test("array prop output") {
createNode("foo" -> Array(1, 2, 3))
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("match (n) return n").dumpToString()
result should include ("[1,2,3]")
}
// TCK'd
test("map output") {
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("return {a:1, b:'foo'}").dumpToString()
result should ( include ("""{a -> 1, b -> "foo"}""") or include ("""{b -> "foo", a -> 1}""") )
}
// TCK'd
test("should be able to return predicate result") {
createNode()
val result = executeWithAllPlannersAndCompatibilityMode("match (a) return id(a) = 0, a is null").toList
result should equal(List(Map("id(a) = 0" -> true, "a is null" -> false)))
}
// TCK'd
test("literal_collection") {
val result = executeWithAllPlannersAndCompatibilityMode("return size([[],[]]+[[]]) as l").toList
result should equal(List(Map("l" -> 3)))
}
// TCK'd
test("array property should be accessible as list") {
createNode()
val result = updateWithBothPlannersAndCompatibilityMode("match (n) SET n.array = [1,2,3,4,5] RETURN tail(tail(n.array))").
toList.
head("tail(tail(n.array))").
asInstanceOf[Iterable[_]]
result.toList should equal(List(3, 4, 5))
}
// TCK'd
test("getting top x when we have less than x left") {
(0 to 15).foreach(x => createNode("count" -> x))
val result = executeWithAllPlannersAndCompatibilityMode("MATCH (a) RETURN a.count ORDER BY a.count SKIP 10 LIMIT 10")
result.toList should equal(List(
Map("a.count" -> 10),
Map("a.count" -> 11),
Map("a.count" -> 12),
Map("a.count" -> 13),
Map("a.count" -> 14),
Map("a.count" -> 15)
))
}
// TCK'd
test("substring with default length") {
val result = executeWithAllPlannersAndCompatibilityMode("return substring('0123456789', 1) as s")
result.toList should equal(List(Map("s" -> "123456789")))
}
// TCK'd
test("sort columns do not leak") {
//GIVEN
val result = executeWithAllPlannersAndCompatibilityMode("match (n) return * order by id(n)")
//THEN
result.columns should equal(List("n"))
}
// TCK'd
test("should allow expression alias in order by with distinct") {
createNode()
//WHEN
val result = executeWithAllPlannersAndCompatibilityMode(
"""MATCH (n)
RETURN distinct ID(n) as id
ORDER BY id DESC""")
//THEN DOESN'T THROW EXCEPTION
result.toList should equal(List(Map("id" -> 0)))
}
// TCK'd
test("columns should not change when using order by and distinct") {
val n = createNode()
val result = executeWithAllPlannersAndCompatibilityMode("match (n) return distinct n order by id(n)")
result.toList should equal(List(Map("n" -> n)))
}
// TCK'd
test("should propagate null through math funcs") {
val result = executeWithAllPlannersAndCompatibilityMode("return 1 + (2 - (3 * (4 / (5 ^ (6 % null))))) as A")
result.toList should equal(List(Map("A" -> null)))
}
// TCK'd
test("should be able to index into nested literal lists") {
executeWithAllPlannersAndCompatibilityMode("RETURN [[1]][0][0]").toList
// should not throw an exception
}
// TCK'd
test("should be able to alias expressions") {
createNode("id" -> 42)
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("match (a) return a.id as a, a.id")
result.toList should equal(List(Map("a" -> 42, "a.id" -> 42)))
}
// TCK'd
test("should not get into a never ending loop") {
val n = createNode("id" -> 42)
val result = executeWithAllPlannersAndCompatibilityMode("MATCH (n) RETURN n, count(n) + 3")
result.toList should equal(List(Map("n" -> n, "count(n) + 3" -> 4)))
}
// TCK'd
test("renaming in multiple steps should still work") {
val result = executeWithCostPlannerOnly(
"""CREATE (m)
|WITH {FIRST: id(m)} AS m
|WITH {SECOND: m.FIRST} AS m
|RETURN m.SECOND""".stripMargin)
.columnAs[Any]("m.SECOND")
.toList
result shouldNot contain(null)
}
// TCK'd
test("aggregating by an array property has a correct definition of equality") {
updateWithBothPlannersAndCompatibilityMode(
""" create
| (_0 {a:[1,2,3]}),
| (_1 {a:[1,2,3]})
""".stripMargin
)
val result = executeWithAllPlannersAndCompatibilityMode("MATCH (a) WITH a.a AS a, count(*) AS count RETURN count")
result.toList should equal(List(Map("count" -> 2)))
}
// TCK'd
test("reusing variable names should not be problematic") {
val result = executeWithAllPlannersAndCompatibilityMode(
"""MATCH (person:Person )<- -(message)<-[like]-(liker:Person)
|WITH like.creationDate AS likeTime, person AS person
|ORDER BY likeTime , message.id
|WITH head(collect({ likeTime: likeTime})) AS latestLike, person AS person
|RETURN latestLike.likeTime AS likeTime
|ORDER BY likeTime
| """.stripMargin)
result shouldBe empty
}
// TCK'd
test("compiled runtime should support addition of lists") {
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("RETURN [1,2,3] + [4, 5] AS FOO")
result.toComparableResult should equal(List(Map("FOO" -> List(1, 2, 3, 4, 5))))
}
// TCK'd
test("compiled runtime should support addition of item to a list") {
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("""RETURN [1,2,3] + 4 AS FOO""")
result.toComparableResult should equal(List(Map("FOO" -> List(1, 2, 3, 4))))
}
// TCK'd
test("distinct inside aggregation should work with lists inside maps") {
val list = Array("A", "B")
createNode("array" -> list)
createNode("array" -> list)
val result = executeWithAllPlanners("MATCH (n) RETURN count(distinct {foo: n.array}) as count")
result.toList should equal(List(Map("count" -> 1)))
}
// TCK'd
test("distinct should work with lists inside maps") {
val list = Array("A", "B")
createNode("array" -> list)
createNode("array" -> list)
val result = executeWithAllPlanners("MATCH (n) WITH distinct {foo: n.array} as map RETURN count(*)")
result.toList should equal(List(Map("count(*)" -> 1)))
}
// TCK'd
test("distinct inside aggregation should work with nested lists inside map") {
val list = Array("A", "B")
createNode("array" -> list)
createNode("array" -> list)
val result = executeWithAllPlanners("MATCH (n) RETURN count(distinct {foo: [[n.array, n.array], [n.array, n.array]]}) as count")
result.toList should equal(List(Map("count" -> 1)))
}
// TCK'd
test("distinct inside aggregation should work with nested lists of maps inside map") {
val list = Array("A", "B")
createNode("array" -> list)
createNode("array" -> list)
val result = executeWithAllPlanners("MATCH (n) RETURN count(distinct {foo: [{bar: n.array}, {baz: {apa: n.array}}]}) as count")
result.toList should equal(List(Map("count" -> 1)))
}
}