-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
MatchAcceptanceTest.scala
2175 lines (1693 loc) · 71.3 KB
/
MatchAcceptanceTest.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.graphdb._
import org.neo4j.helpers.collection.Iterators.single
import scala.collection.JavaConverters._
class MatchAcceptanceTest extends ExecutionEngineFunSuite with QueryStatisticsTestSupport with NewPlannerTestSupport {
test("make sure non-existing nodes are not returned") {
executeWithAllPlannersAndCompatibilityMode("match (n) where id(n) = 10 return n") should be(empty)
executeWithAllPlannersAndCompatibilityMode("match ()-[r]->() where id(r) = 10 return r") should be(empty)
}
test("should fail if columnAs refers to unknown column") {
val n1 = createNode()
val n2 = createNode()
val r = relate(n1, n2)
val result = executeWithAllPlannersAndCompatibilityMode("MATCH (n)-[r]->() RETURN n, r")
a[NotFoundException] should be thrownBy result.columnAs("m")
}
test("AND'd predicates that throw exceptions should not matter if other predicates return false") {
val root = createLabeledNode(Map("name" -> "x"), "Root")
val child1 = createLabeledNode(Map("id" -> "text"), "TextNode")
val child2 = createLabeledNode(Map("id" -> 0), "IntNode")
relate(root, child1)
relate(root, child2)
val query = "MATCH (:Root {name:'x'})-->(i:TextNode) WHERE i.id =~ 'te.*' RETURN i"
val result = executeWithAllPlannersAndCompatibilityMode(query)
result.toList should equal(List(Map("i" -> child1)))
}
test("OR'd predicates that throw exceptions should not matter if other predicates return true") {
val root = createLabeledNode(Map("name" -> "x"), "Root")
val child1 = createLabeledNode(Map("id" -> "text"), "TextNode")
val child2 = createLabeledNode(Map("id" -> 0), "IntNode")
relate(root, child1)
relate(root, child2)
val query = "MATCH (:Root {name:'x'})-->(i) WHERE exists(i.id) OR i.id =~ 'te.*' RETURN i"
val result = executeWithAllPlannersAndCompatibilityMode(query)
result.columnAs("i").toSet[Node] should equal(Set(child1, child2))
}
test("exceptions should be thrown if rows are kept through AND'd predicates") {
val root = createLabeledNode(Map("name" -> "x"), "Root")
val child = createLabeledNode(Map("id" -> 0), "Child")
relate(root, child)
val query = "MATCH (:Root {name:'x'})-->(i:Child) WHERE i.id > 'te' RETURN i"
an [IncomparableValuesException] should be thrownBy executeWithAllPlannersAndCompatibilityMode(query)
}
test("exceptions should be thrown if rows are kept through OR'd predicates") {
val root = createLabeledNode(Map("name" -> "x"), "Root")
val child = createLabeledNode(Map("id" -> 0), "Child")
relate(root, child)
val query = "MATCH (:Root {name:'x'})-->(i) WHERE NOT exists(i.id) OR i.id > 'te' RETURN i"
an [IncomparableValuesException] should be thrownBy executeWithAllPlannersAndCompatibilityMode(query)
}
test("combines aggregation and named path") {
val node1 = createNode("num" -> 1)
val node2 = createNode("num" -> 2)
val node3 = createNode("num" -> 3)
val node4 = createNode("num" -> 4)
relate(node1, node2)
relate(node3, node4)
val result = executeWithCostPlannerOnly(
"""MATCH p=()-[*]->()
|WITH count(*) AS count, p AS p
|WITH nodes(p) AS nodes
|RETURN *""".stripMargin)
result.toSet should equal(Set(
Map("nodes" -> Seq(node1, node2)),
Map("nodes" -> Seq(node3, node4))
))
}
test("comparing numbers should work nicely") {
val n1 = createNode(Map("x" -> 50))
val n2 = createNode(Map("x" -> 50l))
val n3 = createNode(Map("x" -> 50f))
val n4 = createNode(Map("x" -> 50d))
val n5 = createNode(Map("x" -> 50.toByte))
val result = executeWithAllPlannersAndCompatibilityMode(
s"match (n) where n.x < 100 return n"
)
result.columnAs[Node]("n").toList should equal(List(n1, n2, n3, n4, n5))
}
test("comparing string and chars should work nicely") {
val n1 = createNode(Map("x" -> "Anders"))
val n2 = createNode(Map("x" -> 'C'))
createNode(Map("x" -> "Zzing"))
createNode(Map("x" -> 'Ä'))
val result = executeWithAllPlannersAndCompatibilityMode(
s"match (n) where n.x < 'Z' AND n.x < 'z' return n"
)
result.columnAs("n").toList should equal(List(n1, n2))
}
test("test zero length var len path in the middle") {
createNodes("A", "B", "C", "D", "E")
relate("A" -> "CONTAINS" -> "B")
relate("B" -> "FRIEND" -> "C")
val result = executeWithAllPlannersAndCompatibilityMode("match (a {name:'A'})-[:CONTAINS*0..1]->(b)-[:FRIEND*0..1]->(c) return a,b,c")
result.toSet should equal(
Set(
Map("a" -> node("A"), "b" -> node("A"), "c" -> node("A")),
Map("a" -> node("A"), "b" -> node("B"), "c" -> node("B")),
Map("a" -> node("A"), "b" -> node("B"), "c" -> node("C")))
)
}
test("simple var length acceptance test") {
createNodes("A", "B", "C", "D")
relate("A" -> "CONTAINS" -> "B")
relate("B" -> "CONTAINS" -> "C")
relate("C" -> "CONTAINS" -> "D")
val result = executeWithAllPlannersAndCompatibilityMode("match (a {name:'A'})-[*]->(x) return x")
result.toSet should equal(
Set(
Map("x" -> node("B")),
Map("x" -> node("C")),
Map("x" -> node("D")))
)
}
test("should return a var length path without minimal length") {
createNodes("A", "B", "C")
val r1 = relate("A" -> "KNOWS" -> "B")
val r2 = relate("B" -> "KNOWS" -> "C")
val result = executeWithAllPlannersAndCompatibilityMode("match p=(n {name:'A'})-[:KNOWS*..2]->(x) return p")
result.columnAs[Path]("p").toList should equal(List(
PathImpl(node("A"), r1, node("B")),
PathImpl(node("A"), r1, node("B"), r2, node("C"))
))
}
test("should return a var length path with unbound max") {
createNodes("A", "B", "C")
val r1 = relate("A" -> "KNOWS" -> "B")
val r2 = relate("B" -> "KNOWS" -> "C")
val result = executeWithAllPlannersAndCompatibilityMode("match p=(n {name:'A'})-[:KNOWS*..]->(x) return p")
result.columnAs[Path]("p").toList should equal(List(
PathImpl(node("A"), r1, node("B")),
PathImpl(node("A"), r1, node("B"), r2, node("C"))
))
}
test("should handle bound nodes not part of the pattern") {
createNodes("A", "B", "C")
relate("A" -> "KNOWS" -> "B")
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("MATCH (a {name:'A'}),(c {name:'C'}) match (a)-->(b) return a,b,c").toSet
result should equal (Set(Map("a" -> node("A"), "b" -> node("B"), "c" -> node("C"))))
}
test("should return shortest path") {
createNodes("A", "B")
val r1 = relate("A" -> "KNOWS" -> "B")
val result = executeWithAllPlannersAndCompatibilityMode("match p = shortestPath((a {name:'A'})-[*..15]-(b {name:'B'})) return p").
toList.head("p").asInstanceOf[Path]
graph.inTx {
val number_of_relationships_in_path = result.length()
number_of_relationships_in_path should equal (1)
result.startNode() should equal (node("A"))
result.endNode() should equal (node("B"))
result.lastRelationship() should equal (r1)
}
}
test("should return shortest path unbound length") {
createNodes("A", "B")
relate("A" -> "KNOWS" -> "B")
//Checking that we don't get an exception
executeWithAllPlannersAndCompatibilityMode("match p = shortestPath((a {name:'A'})-[*]-(b {name:'B'})) return p").toList
}
test("should not traverse same relationship twice in shortest path") {
// given
createNodes("A", "B")
relate("A" -> "KNOWS" -> "B")
// when
val result = executeWithAllPlannersAndCompatibilityMode("MATCH (a{name:'A'}), (b{name:'B'}) MATCH p=allShortestPaths((a)-[:KNOWS|KNOWS*]->(b)) RETURN p").
toList
// then
graph.inTx {
result.size should equal (1)
}
}
test("finds a single path for paths of length one") {
/*
(a)-(b)-c
*/
val nodeA = createLabeledNode("A")
val nodeB = createLabeledNode("B")
val nodeC = createLabeledNode("C")
relate(nodeA, nodeB)
relate(nodeB, nodeC)
val result = executeWithAllPlannersAndCompatibilityMode("match p = shortestpath((a:A)-[r*..1]->(n)) return nodes(p) as nodes").columnAs[List[Node]]("nodes").toSet
result should equal(Set(List(nodeA, nodeB)))
}
test("if asked for also return paths of length 0") {
/*
(a)-(b)-c
*/
val nodeA = createLabeledNode("A")
val nodeB = createLabeledNode("B")
val nodeC = createLabeledNode("C")
relate(nodeA, nodeB)
relate(nodeB, nodeC)
val result = executeWithAllPlannersAndCompatibilityMode("match p = shortestpath((a:A)-[r*0..1]->(n)) return nodes(p) as nodes").columnAs[List[Node]]("nodes").toSet
result should equal(Set(List(nodeA), List(nodeA, nodeB)))
}
test("if asked for also return paths of length 0, even when no max length is speficied") {
/*
(a)-(b)-c
*/
val nodeA = createLabeledNode("A")
val nodeB = createLabeledNode("B")
val nodeC = createLabeledNode("C")
relate(nodeA, nodeB)
relate(nodeB, nodeC)
val result = executeWithAllPlannersAndCompatibilityMode("match p = shortestpath((a:A)-[r*0..]->(n)) return nodes(p) as nodes").columnAs[List[Node]]("nodes").toSet
result should equal(Set(List(nodeA), List(nodeA, nodeB), List(nodeA, nodeB, nodeC)))
}
test("we can ask explicitly for paths of minimal length 1") {
/*
(a)-(b)-c
*/
val nodeA = createLabeledNode("A")
val nodeB = createLabeledNode("B")
val nodeC = createLabeledNode("C")
relate(nodeA, nodeB)
relate(nodeB, nodeC)
val result = executeWithAllPlannersAndCompatibilityMode("match p = shortestpath((a:A)-[r*1..1]->(n)) return nodes(p) as nodes").columnAs[List[Node]]("nodes").toSet
result should equal(Set(List(nodeA, nodeB)))
}
test("finds a single path for non-variable length paths") {
/*
(a)-(b)-c
*/
val nodeA = createLabeledNode("A")
val nodeB = createLabeledNode("B")
val nodeC = createLabeledNode("C")
relate(nodeA, nodeB)
relate(nodeB, nodeC)
val result = executeWithAllPlannersAndCompatibilityMode("match p = shortestpath((a:A)-[r]->(n)) return nodes(p) as nodes").columnAs[List[Node]]("nodes").toSet
result should equal(Set(List(nodeA, nodeB)))
}
test("two bound nodes pointing to one") {
val a = createNode("A")
val b = createNode("B")
val x1 = createNode("x1")
val x2 = createNode("x2")
relate(a, x1, "REL", "AX1")
relate(a, x2, "REL", "AX2")
relate(b, x1, "REL", "BX1")
relate(b, x2, "REL", "BX2")
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode( """
MATCH (a {name:'A'}), (b {name:'B'})
MATCH (a)-[rA]->(x)<-[rB]->(b)
return x""")
result.columnAs("x").toList should equal (List(x2, x1))
}
test("three bound nodes pointing to one") {
val a = createNode("A")
val b = createNode("B")
val c = createNode("C")
val x1 = createNode("x1")
val x2 = createNode("x2")
relate(a, x1, "REL", "AX1")
relate(a, x2, "REL", "AX2")
relate(b, x1, "REL", "BX1")
relate(b, x2, "REL", "BX2")
relate(c, x1, "REL", "CX1")
relate(c, x2, "REL", "CX2")
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode( """
MATCH (a {name:'A'}), (b {name:'B'}), (c {name:'C'})
match (a)-[rA]->(x), (b)-[rB]->(x), (c)-[rC]->(x)
return x""")
result.columnAs("x").toList should equal (List(x2, x1))
}
test("three bound nodes pointing to one with a bunch of extra connections") {
val a = createNode("a")
val b = createNode("b")
val c = createNode("c")
val d = createNode("d")
val e = createNode("e")
val f = createNode("f")
val g = createNode("g")
val h = createNode("h")
val i = createNode("i")
val j = createNode("j")
val k = createNode("k")
relate(a, d)
relate(a, e)
relate(a, f)
relate(a, g)
relate(a, i)
relate(b, d)
relate(b, e)
relate(b, f)
relate(b, h)
relate(b, k)
relate(c, d)
relate(c, e)
relate(c, h)
relate(c, g)
relate(c, j)
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode( """
MATCH (a {name:'a'}), (b {name:'b'}), (c {name:'c'})
match (a)-->(x), (b)-->(x), (c)-->(x)
return x""")
result.columnAs("x").toList should equal (List(e, d))
}
test("should split optional mandatory cleverly") {
val a = createNode("A")
val b = createNode("B")
val c = createNode("C")
relate(a, b, "knows", "rAB")
relate(b, c, "knows", "rBC")
val result = executeWithAllPlannersAndCompatibilityMode( """
match (a {name:'A'})
optional match (a)-[r1:knows]->(friend)-[r2:knows]->(foaf)
return foaf""")
result.toList should equal (List(Map("foaf" -> c)))
}
test("should handle optional paths") {
val a = createNode("A")
val b = createNode("B")
val c = createNode("C")
val r = relate(a, b, "X")
val result = executeWithAllPlannersAndCompatibilityMode( """
match (a {name:'A'}), (x) where x.name in ['B', 'C']
optional match p = (a)-->(x)
return x, p""")
graph.inTx {
assert(Set(
Map("x" -> b, "p" -> PathImpl(a, r, b)),
Map("x" -> c, "p" -> null)
) === result.toSet)
}
}
test("should handle optional paths from graph algo") {
val a = createNode("A")
val b = createNode("B")
val c = createNode("C")
val r = relate(a, b, "X")
val result = executeWithAllPlannersAndCompatibilityMode( """
match (a {name:'A'}), (x) where x.name in ['B', 'C']
optional match p = shortestPath((a)-[*]->(x))
return x, p""").toSet
graph.inTx(assert(Set(
Map("x" -> b, "p" -> PathImpl(a, r, b)),
Map("x" -> c, "p" -> null)
) === result))
}
test("should handle optional paths from a combo") {
val a = createNode("A")
val b = createNode("B")
relate(a, b, "X")
val result = executeWithAllPlannersAndCompatibilityMode( """
match (a {name:'A'})
optional match p = (a)-->(b)-[*]->(c)
return p""")
assert(Set(
Map("p" -> null)
) === result.toSet)
}
test("should handle optional paths from a combo with MATCH") {
val a = createNode("A")
val b = createNode("B")
relate(a, b, "X")
val result = executeWithAllPlannersAndCompatibilityMode( """
match (a {name:'A'})
optional match p = (a)-->(b)-[*]->(c)
return p""")
assert(Set(
Map("p" -> null)
) === result.toSet)
}
test("should handle optional paths from var length path") {
val a = createNode("A")
val b = createNode("B")
val c = createNode("C")
val r = relate(a, b, "X")
val result = executeWithAllPlannersAndCompatibilityMode( """
match (a {name:'A'}), (x) where x.name in ['B', 'C']
optional match p = (a)-[r*]->(x)
return r, x, p""")
assert(Set(
Map("r" -> Seq(r), "x" -> b, "p" -> PathImpl(a, r, b)),
Map("r" -> null, "x" -> c, "p" -> null)
) === result.toSet)
}
test("should return an iterable with all relationships from a var length") {
val a = createNode()
val b = createNode()
val c = createNode()
val r1 = relate(a, b)
val r2 = relate(b, c)
val result = executeWithAllPlannersAndCompatibilityMode( """
match (a) where id(a) = 0
match (a)-[r*2]->(c)
return r""")
result.toList should equal (List(Map("r" -> List(r1, r2))))
}
test("should handle all shortest paths") {
createDiamond()
val result = executeWithAllPlannersAndCompatibilityMode( """
match (a), (d) where id(a) = 0 and id(d) = 3
match p = allShortestPaths( (a)-[*]->(d) )
return p""")
result.toList.size should equal (2)
}
test("should collect leafs") {
val a = createNode()
val b = createNode()
val c = createNode()
val d = createNode()
val rab = relate(a, b)
val rac = relate(a, c)
val rcd = relate(c, d)
val result = executeWithAllPlannersAndCompatibilityMode( """
match p = (root)-[*]->(leaf)
where id(root) = 0 and not((leaf)-->())
return p, leaf""")
assert(Set(
Map("leaf" -> b, "p" -> PathImpl(a, rab, b)),
Map("leaf" -> d, "p" -> PathImpl(a, rac, c, rcd, d))
) === result.toSet)
}
test("should exclude connected nodes") {
val a = createNode("A")
val b = createNode("B")
val c = createNode("B")
relate(a, b)
val result = executeWithAllPlannersAndCompatibilityMode("""
MATCH (a {name:'A'}), (other {name:'B'})
WHERE NOT (a)-->(other)
RETURN other""")
result.toList should equal (List(Map("other" -> c)))
}
test("should not throw exception when stuff is missing") {
val a = createNode()
val b = createNode("Mark")
relate(a, b)
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode( """
MATCH (n)-->(x0)
OPTIONAL MATCH (x0)-->(x1)
WHERE x1.foo = 'bar'
RETURN x0.name""")
result.toList should equal (List(Map("x0.name" -> "Mark")))
}
test("should solve an optional match even when the optional match is highly selective") {
val a = createNode("A")
val b = createNode("B")
val c = createNode("C")
relate(a, b)
relate(a, c)
val result = executeWithAllPlannersAndCompatibilityMode( s"""
MATCH (a)-->(b)
WHERE id(b) = ${b.getId}
OPTIONAL MATCH (a)-->(c)
WHERE id(c) = ${c.getId}
RETURN a.name""")
result.toList should equal (List(Map("a.name" -> "A")))
}
test("should find nodes both directions") {
val n = createNode()
val a = createNode()
relate(a, n, "Admin")
val result = executeWithAllPlannersAndCompatibilityMode( "match (n) -[:Admin]- (b) where id(n) = 0 return id(n), id(b)")
result.toSet should equal (Set(Map("id(n)" -> 0, "id(b)" -> 1)))
}
test("should get all nodes") {
val a = createNode()
val b = createNode()
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("match (n) return n")
result.columnAs[Node]("n").toSet should equal (Set(a, b))
}
test("should allow comparisons of nodes") {
val a = createNode()
val b = createNode()
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("MATCH (a), (b) where a <> b return a,b")
result.toSet should equal (Set(Map("a" -> b, "b" -> a), Map("b" -> b, "a" -> a)))
}
test("should solve selfreferencing pattern") {
val a = createNode()
val b = createNode()
val c = createNode()
relate(a, b)
relate(b, c)
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("match (a)-->(b), (b)-->(b) return b")
result shouldBe 'isEmpty
}
test("should solve self referencing pattern2") {
val a = createNode()
val b = createNode()
val r = relate(a, a)
relate(a, b)
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("match (a)-[r]->(a) return r")
result.toList should equal (List(Map("r" -> r)))
}
test("relationship predicate with multiple rel types") {
val a = createNode()
val b = createNode()
val x = createNode()
relate(a, x, "A")
relate(b, x, "B")
val result = executeWithAllPlannersAndCompatibilityMode("match (a) where (a)-[:A|:B]->() return a").toSet
result should equal (Set(Map("a" -> a), Map("a" -> b)))
}
test("relationship predicate") {
val a = createNode()
val x = createNode()
relate(a, x, "A")
val result = executeWithAllPlannersAndCompatibilityMode("match (a) where (a)-[:A]->() return a").toList
result should equal (List(Map("a" -> a)))
}
test("nullable var length path should work") {
createNode()
val b = createNode()
val result = executeWithAllPlannersAndCompatibilityMode("""
match (a), (b) where id(a) = 0 and id(b) = 1
optional match (a)-[r*]-(b) where r is null and a <> b
return b
""").toList
result should equal (List(Map("b" -> b)))
}
test("listing rel types multiple times should not give multiple returns") {
val a = createNode()
val b = createNode()
relate(a, b, "REL")
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("match (a)-[:REL|:REL]->(b) return b").toList
result should equal (List(Map("b" -> b)))
}
test("different results on ordered aggregation with limit") {
val root = createNode()
val n1 = createNode("x" -> 1)
val n2 = createNode("x" -> 2)
val m1 = createNode()
val m2 = createNode()
relate(root, n1, m1)
relate(root, n2, m2)
val q = "match (a)-->(n)-->(m) where id(a) = 0 return n.x, count(*) order by n.x"
val resultWithoutLimit = executeWithAllPlannersAndCompatibilityMode(q)
val resultWithLimit = executeWithAllPlannersAndCompatibilityMode(q + " limit 1000")
resultWithoutLimit.toList should equal (resultWithLimit.toList)
}
test("should be able to handle single node patterns") {
// given
val n = createNode("foo" -> "bar")
// when
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("match (n) where n.foo = 'bar' return n")
// then
result.toList should equal (List(Map("n" -> n)))
}
test("issue 479") {
createNode()
val q = "match (n) where id(n) = 0 optional match (n)-->(x) where (x)-->() return x"
executeWithAllPlannersAndCompatibilityMode(q).toList should equal (List(Map("x" -> null)))
}
test("issue 479 has relationship to specific node") {
createNode()
val q = "match (n) where id(n) = 0 optional match (n)-[:FRIEND]->(x) where not (n)-[:BLOCK]->(x) return x"
executeWithAllPlannersAndCompatibilityMode(q).toList should equal (List(Map("x" -> null)))
}
test("length on filter") {
val q = "match (n) optional match (n)-[r]->(m) return length(filter(x in collect(r) WHERE x <> null)) as cn"
executeWithAllPlannersAndCompatibilityMode(q).toList should equal (List(Map("cn" -> 0)))
}
test("path Direction Respected") {
val a = createNode()
val b = createNode()
relate(a, b)
val result = executeWithAllPlannersAndCompatibilityMode("match p=(b)<--(a) return p").toList.head("p").asInstanceOf[Path]
result.startNode() should equal (b)
result.endNode() should equal (a)
}
test("shortest Path Direction Respected") {
val a = createNode()
val b = createNode()
relate(a, b)
val result = executeWithAllPlannersAndCompatibilityMode("match (a), (b) where id(a) = 0 and id(b) = 1 match p=shortestPath((b)<-[*]-(a)) return p").toList.head("p").asInstanceOf[Path]
result.startNode() should equal (b)
result.endNode() should equal (a)
}
test("no match in optional match should produce null values") {
val result = executeWithAllPlannersAndCompatibilityMode("OPTIONAL MATCH (n) RETURN n")
result.toList should equal (List(Map("n" -> null)))
}
test("should preserve the original matched values if optional match matches nothing") {
val n = createNode()
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("MATCH (n) OPTIONAL MATCH (n)-[:NOT_EXIST]->(x) RETURN n, x")
result.toList should equal (List(Map("n" -> n, "x" -> null)))
}
test("empty collect should not contain null") {
val n = createNode()
val result = executeWithAllPlannersAndCompatibilityMode("MATCH (n) OPTIONAL MATCH (n)-[:NOT_EXIST]->(x) RETURN n, collect(x)")
result.toList should equal (List(Map("n" -> n, "collect(x)" -> List())))
}
test("can rewrite exists property") {
val a = createNode()
val r1 = createNode("foo" -> "bar")
val r2 = createNode()
val b = createNode()
relate(a, r1)
relate(a, r2)
relate(r1, b)
relate(r2, b)
val result = executeWithAllPlannersAndCompatibilityMode("MATCH (a)-->(r)-->(b) WHERE id(a) = 0 AND exists(r.foo) RETURN r")
result.toList should equal (List(Map("r" -> r1)))
}
test("can handle paths with multiple unnamed nodes") {
createNode()
val result = executeWithAllPlannersAndCompatibilityMode("MATCH (a)<--()<--(b)-->()-->(c) WHERE id(a) = 0 RETURN c")
result shouldBe 'isEmpty
}
test("path expressions should work with on the fly predicates") {
val refNode = createNode()
relate(refNode, createNode("name" -> "Neo"))
val result = executeWithAllPlannersAndCompatibilityMode("MATCH (a)-->(b) WHERE (b)-->() AND id(a) = {self} RETURN b", "self" -> refNode.getId)
result shouldBe 'isEmpty
}
test("should filter nodes by label given in match") {
// given
val a = createNode()
val b1 = createLabeledNode("foo")
val b2 = createNode()
relate(a, b1)
relate(a, b2)
// when
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode(s"MATCH (a)-->(b:foo) RETURN b")
// THEN
result.toList should equal (List(Map("b" -> b1)))
}
test("should match nodes with specified labels on both sides") {
// given
val r = relate(createLabeledNode("A"), createLabeledNode("B"))
relate(createLabeledNode("B"), createLabeledNode("A"))
relate(createLabeledNode("B"), createLabeledNode("B"))
relate(createLabeledNode("A"), createLabeledNode("A"))
// when
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode(s"MATCH (a:A)-[r]->(b:B) RETURN r")
// THEN
result.toSet should equal (Set(Map("r" -> r)))
}
test("should match nodes with many labels specified on it") {
// given
val n = createLabeledNode("A","B","C")
createLabeledNode("A","B")
createLabeledNode("A","C")
createLabeledNode("B","C")
createLabeledNode("A")
createLabeledNode("B")
createLabeledNode("C")
// when
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode(s"MATCH (a:A:B:C) RETURN a")
// THEN
result.toList should equal (List(Map("a" -> n)))
}
test("should be able to tell if a label is on a node or not") {
// given
createNode()
createLabeledNode("Foo")
// when
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode(s"MATCH (n) RETURN (n:Foo)")
// THEN
result.toSet should equal (Set(Map("(n:Foo)" -> true), Map("(n:Foo)" -> false)))
}
test("should use predicates in the correct place") {
// given
val m = executeWithCostPlannerOnly( """create
(advertiser {name:"advertiser1"}),
(thing {name:"Color"}),
(red {name:"red"}),
(p1 {name:"product1"}),
(p2 {name:"product4"}),
(advertiser)-[:adv_has_product]->(p1),
(advertiser)-[:adv_has_product]->(p2),
(thing)-[:aa_has_value]->(red),
(p1) -[:ap_has_value]->(red),
(p2) -[:ap_has_value]->(red)
return advertiser, thing""").toList.head
val advertiser = m("advertiser").asInstanceOf[Node]
val thing = m("thing").asInstanceOf[Node]
// when
val result = executeWithAllPlannersAndCompatibilityMode(
"""MATCH (advertiser) -[:adv_has_product] ->(out) -[:ap_has_value] -> (red) <-[:aa_has_value]- (a)
WHERE id(advertiser) = {1} AND id(a) = {2}
AND red.name = 'red' and out.name = 'product1'
RETURN out.name""", "1" -> advertiser.getId, "2" -> thing.getId)
// then
result.toList should equal (List(Map("out.name" -> "product1")))
}
test("should be able to use index hints") {
// given
val andres = createLabeledNode(Map("name" -> "Andres"), "Person")
val jake = createLabeledNode(Map("name" -> "Jacob"), "Person")
relate(andres, createNode())
relate(jake, createNode())
graph.createIndex("Person", "name")
// when
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("MATCH (n:Person)-->() USING INDEX n:Person(name) WHERE n.name = 'Jacob' RETURN n")
// then
result.toList should equal (List(Map("n" -> jake)))
result.executionPlanDescription.toString should include("IndexSeek")
}
test("should be able to use index hints with STARTS WITH predicates") {
// given
val andres = createLabeledNode(Map("name" -> "Andres"), "Person")
val jake = createLabeledNode(Map("name" -> "Jacob"), "Person")
relate(andres, createNode())
relate(jake, createNode())
graph.createIndex("Person", "name")
// when
val result = executeWithAllPlannersAndCompatibilityMode("MATCH (n:Person)-->() USING INDEX n:Person(name) WHERE n.name STARTS WITH 'Jac' RETURN n")
// then
result.toList should equal (List(Map("n" -> jake)))
result.executionPlanDescription.toString should include("IndexSeek")
}
test("should be able to use index hints with inequality/range predicates") {
// given
val andres = createLabeledNode(Map("name" -> "Andres"), "Person")
val jake = createLabeledNode(Map("name" -> "Jacob"), "Person")
relate(andres, createNode())
relate(jake, createNode())
graph.createIndex("Person", "name")
// when
val result = executeWithAllPlannersAndCompatibilityMode("MATCH (n:Person)-->() USING INDEX n:Person(name) WHERE n.name > 'Jac' RETURN n")
// then
result.toList should equal (List(Map("n" -> jake)))
result.executionPlanDescription.toString should include("IndexSeek")
}
test("should be able to use label as start point") {
// given
val andres = createLabeledNode(Map("name" -> "Andres"), "Person")
val jake = createLabeledNode(Map("name" -> "Jacob"), "Person")
relate(andres, createNode())
relate(jake, createNode())
// when
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("MATCH (n:Person)-->() WHERE n.name = 'Jacob' RETURN n")
// then
result.toList should equal (List(Map("n" -> jake)))
}
test("id in where leads to empty result") {
// when
val result = executeWithAllPlannersAndCompatibilityMode("MATCH (n) WHERE id(n)=1337 RETURN n")
// then DOESN'T THROW EXCEPTION
result shouldBe 'isEmpty
}
test("should handle two unconnected patterns") {
// given a node with two related nodes
val a = createNode()
val b = createNode()
val c = createNode()
relate(a, b)
relate(a, c)
// when asked for a cartesian product of the same match twice
val result = executeWithAllPlannersAndRuntimesAndCompatibilityMode("match (a)-->(b) match (c)-->(d) return a,b,c,d")
// then we should find 2 x 2 = 4 result matches
result.toSet shouldEqual
Set(
Map("a" -> a, "b" -> b, "c" -> a, "d" -> b),
Map("a" -> a, "b" -> b, "c" -> a, "d" -> c),
Map("a" -> a, "b" -> c, "c" -> a, "d" -> b),
Map("a" -> a, "b" -> c, "c" -> a, "d" -> c))
}
test("should be able to set properties with a literal map twice in the same transaction") {
val node = createLabeledNode("FOO")
graph.inTx {
executeWithCostPlannerOnly("MATCH (n:FOO) SET n = { first: 'value' }")
executeWithCostPlannerOnly("MATCH (n:FOO) SET n = { second: 'value' }")
}
graph.inTx {
node.getProperty("first", null) should equal (null)
node.getProperty("second") should equal ("value")
}
}
test("should not fail if asking for a non existent node id with WHERE") {
executeWithAllPlannersAndCompatibilityMode("match (n) where id(n) in [0,1] return n").toList
// should not throw an exception
}