-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AggregationAcceptanceTest.scala
329 lines (258 loc) · 11.7 KB
/
AggregationAcceptanceTest.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
/*
* 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.{SyntaxException, NewPlannerTestSupport, ExecutionEngineFunSuite}
class AggregationAcceptanceTest extends ExecutionEngineFunSuite with NewPlannerTestSupport {
test("should handle aggregates inside non aggregate expressions") {
executeWithAllPlannersAndCompatibilityMode(
"MATCH (a { name: 'Andres' })<-[:FATHER]-(child) RETURN {foo:a.name='Andres',kids:collect(child.name)}"
).toList
}
test ("should be able to count nodes") {
val a = createLabeledNode("Start")
val b1 = createNode()
val b2 = createNode()
relate(a, b1, "A")
relate(a, b2, "A")
val result = executeWithAllPlannersAndCompatibilityMode(
s"match (a:Start)-[rel]->(b) return a, count(*)"
)
result.toList should equal(List(Map("a" -> a, "count(*)" -> 2)))
}
test("should sort on aggregated function and normal property") {
createNode(Map("name" -> "andres", "division" -> "Sweden"))
createNode(Map("name" -> "michael", "division" -> "Germany"))
createNode(Map("name" -> "jim", "division" -> "England"))
createNode(Map("name" -> "mattias", "division" -> "Sweden"))
val result = executeWithAllPlannersAndCompatibilityMode(
"""match (n)
|return n.division, count(*)
|order by count(*) DESC, n.division ASC""".stripMargin
)
result.toList should equal(List(
Map("n.division" -> "Sweden", "count(*)" -> 2),
Map("n.division" -> "England", "count(*)" -> 1),
Map("n.division" -> "Germany", "count(*)" -> 1)))
}
test("should aggregate on properties") {
createNode(Map("x" -> 33))
createNode(Map("x" -> 33))
createNode(Map("x" -> 42))
val result = executeWithAllPlannersAndCompatibilityMode("match (n) return n.x, count(*)")
result.toList should equal(List(Map("n.x" -> 42, "count(*)" -> 1), Map("n.x" -> 33, "count(*)" -> 2)))
}
test("should count non null values") {
createNode(Map("y" -> "a", "x" -> 33))
createNode(Map("y" -> "a"))
createNode(Map("y" -> "b", "x" -> 42))
val result = executeWithAllPlannersAndCompatibilityMode("match (n) return n.y, count(n.x)")
result.toSet should equal(Set(Map("n.y" -> "a", "count(n.x)" -> 1), Map("n.y" -> "b", "count(n.x)" -> 1)))
}
test("should sum non null values") {
createNode(Map("y" -> "a", "x" -> 33))
createNode(Map("y" -> "a"))
createNode(Map("y" -> "a", "x" -> 42))
val result = executeWithAllPlannersAndCompatibilityMode("match (n) return n.y, sum(n.x)")
result.toList should contain(Map("n.y" -> "a", "sum(n.x)" -> 75))
}
test("should handle aggregation on functions") {
val a = createLabeledNode("Start")
val b = createNode()
val c = createNode()
relate(a, b)
relate(a, c)
val result = executeWithAllPlannersAndCompatibilityMode(
"""match p = (a:Start)-[*]-> (b)
|return b, avg(length(p))""".stripMargin)
result.toSet should equal(Set(Map("b" -> c, "avg(length(p))" -> 1.0),
Map("b" -> b, "avg(length(p))" -> 1.0)))
}
test("should be able to do distinct on unbound node") {
val result = executeWithAllPlannersAndCompatibilityMode("optional match (a) return count(distinct a)")
result.toList should equal (List(Map("count(distinct a)" -> 0)))
}
test("shouldBeAbleToDoDistinctOnNull") {
createNode()
val result = executeWithAllPlannersAndCompatibilityMode("match (a) return count(distinct a.foo)")
result.toList should equal (List(Map("count(distinct a.foo)" -> 0)))
}
test("should be able to collect distinct nulls") {
val query = "unwind [NULL, NULL] AS x RETURN collect(distinct x) as c"
val result = executeWithAllPlanners(query)
result.toSeq.head shouldBe Map("c" -> List.empty)
}
test("should be able to collect distinct values mixed with nulls") {
val query = "unwind [NULL, 1, NULL] AS x RETURN collect(distinct x) as c"
val result = executeWithAllPlanners(query)
result.toSeq.head shouldBe Map("c" -> List(1))
}
test("should aggregate on array values") {
createNode("color" -> Array("red"))
createNode("color" -> Array("blue"))
createNode("color" -> Array("red"))
val result = executeWithAllPlannersAndCompatibilityMode("match (a) return distinct a.color, count(*)").toList
result.foreach { x =>
val c = x("a.color").asInstanceOf[Array[_]]
c.toList match {
case List("red") => x("count(*)") should equal (2)
case List("blue") => x("count(*)") should equal (1)
case _ => fail("wut?")
}
}
}
test("aggregates in aggregates should fail") {
createNode()
intercept[SyntaxException](executeWithAllPlannersAndCompatibilityMode("match (a) return count(count(*))").toList)
}
test("aggregates should be possible to use with arithmetics") {
createNode()
val result = executeWithAllPlannersAndCompatibilityMode("match (a) return count(*) * 10").toList
result should equal (List(Map("count(*) * 10" -> 10)))
}
test("aggregates should be possible to order by arithmetics") {
createLabeledNode("A")
createLabeledNode("X")
createLabeledNode("X")
val result = executeWithAllPlannersAndCompatibilityMode("match (a:A), (b:X) return count(a) * 10 + count(b) * 5 as X order by X").toList
result should equal (List(Map("X" -> 30)))
}
test("should handle multiple aggregates on the same node") {
//WHEN
val a = createNode()
val result = executeWithAllPlannersAndCompatibilityMode("match (n) return count(n), collect(n)")
//THEN
result.toList should equal (List(Map("count(n)" -> 1, "collect(n)" -> Seq(a))))
}
test("simple counting of nodes works as expected") {
graph.inTx {
(1 to 100).foreach {
x => createNode()
}
}
//WHEN
val result = executeWithAllPlannersAndCompatibilityMode("match (n) return count(*)")
//THEN
result.toList should equal (List(Map("count(*)" -> 100)))
}
test("aggregation around named paths works") {
val a = createNode()
val b = createNode()
val c = createNode()
val d = createNode()
val e = createNode()
val f = createNode()
relate(a, b)
relate(c, d)
relate(d, e)
relate(e, f)
val result = executeWithAllPlannersAndCompatibilityMode(
"""match p = (a)-[*]->(b)
|return collect(nodes(p)) as paths, length(p) as l order by length(p)""".stripMargin)
val expected =
List(Map[String, Any]("l" -> 1, "paths" -> List(List(a, b), List(c, d), List(d, e), List(e, f))),
Map[String, Any]("l" -> 2, "paths" -> List(List(c, d, e), List(d, e, f))),
Map[String, Any]("l" -> 3, "paths" -> List(List(c, d, e, f))))
result.toList should be (expected)
}
test("combine min with aggregation") {
// given
val a = createLabeledNode(Map("name" -> "a"), "T")
val b = createLabeledNode(Map("name" -> "b"), "T")
val c = createLabeledNode(Map("name" -> "c"), "T")
relate(a, b, "R")
relate(a, c, "R")
relate(c, b, "R")
// when
val query =
"""MATCH p=(a:T {name: "a"})-[:R*]-(other: T)
|WHERE other <> a
|WITH a, other, min(length(p)) AS len ORDER BY other.name
|RETURN a.name as name, collect(other.name) AS others, len""".stripMargin
val result = executeWithAllPlannersAndCompatibilityMode(query)
//then
result.toList should equal(Seq(Map("name" -> "a", "others" -> Seq("b", "c"), "len" -> 1 )))
}
test("should handle subexpression in aggregation also occuring as standalone expression with nested aggregation in a literal map") {
// There was a bug in the isolateAggregation AST rewriter that was triggered by this somewhat unusual case
val a = createLabeledNode("A")
val b = createLabeledNode(Map("prop" -> 42), "B")
val query =
"""|MATCH (a:A), (b:B)
|RETURN
|coalesce(a.prop, b.prop) AS foo,
|b.prop AS bar,
|{
| y: count(b)
|} AS baz""".stripMargin
val result = executeWithCostPlannerOnly(query)
result.toList should equal(List(Map("foo" -> 42, "bar" -> 42, "baz" -> Map("y" -> 1))))
}
test("should not project too much when aggregating in a WITH before merge and after a WITH with filter") {
val n = createLabeledNode(Map("prop" -> 42), "A")
val query =
"""|UNWIND [42] as props
|WITH props WHERE props > 32
|WITH distinct props as p
|MERGE (a:A {prop:p})
|RETURN a.prop as prop""".stripMargin
val result = executeWithAllPlannersAndCompatibilityMode(query)
result.toList should equal(List(Map("prop" -> 42)))
}
test("should not overflow when doing summation") {
executeWithAllPlanners("unwind range(1000000,2000000) as i with i limit 3000 return sum(i)").toList should equal(
List(Map("sum(i)" -> 3004498500L)))
}
test("should count correctly in case of loops") {
val node = createNode()
relate(node, node)
val list = executeWithAllPlanners("MATCH ()-[r]-() RETURN id(r) as r").columnAs[Long]("r").toList
val result = executeWithAllPlanners("MATCH ()-[r]-() RETURN count(r) as c").columnAs[Long]("c").next()
list should equal(Seq(0))
result should equal(1)
}
test("should aggregate using as grouping key expressions using variables in scope and nothing else") {
val userId = createLabeledNode(Map("userId" -> 11), "User")
relate(userId, createNode(), "FRIEND", Map("propFive" -> 1))
relate(userId, createNode(), "FRIEND", Map("propFive" -> 3))
relate(createNode(), userId, "FRIEND", Map("propFive" -> 2))
relate(createNode(), userId, "FRIEND", Map("propFive" -> 4))
val query1 = """MATCH (user:User {userId: 11})-[friendship:FRIEND]-()
|WITH user, collect(friendship)[toInt(rand() * count(friendship))] AS selectedFriendship
|RETURN id(selectedFriendship) AS friendshipId, selectedFriendship.propFive AS propertyValue""".stripMargin
val query2 = """MATCH (user:User {userId: 11})-[friendship:FRIEND]-()
|WITH user, collect(friendship) AS friendships
|WITH user, friendships[toInt(rand() * size(friendships))] AS selectedFriendship
|RETURN id(selectedFriendship) AS friendshipId, selectedFriendship.propFive AS propertyValue""".stripMargin
// TODO: this can be executed with the compatibility mode when we'll depend on the 2.3.5 cypher-compiler
val result1 = executeWithCostPlannerOnly(query1).toList
val result2 = executeWithCostPlannerOnly(query2).toList
result1.size should equal(result2.size)
}
test("max() should aggregate strings") {
val query = "UNWIND ['a', 'b', 'B', null, 'abc', 'abc1'] AS i RETURN max(i)"
val result = executeWithAllPlanners(query)
result.toList should equal(List(Map("max(i)" -> "b")))
}
test("min() should aggregate strings") {
val query = "UNWIND ['a', 'b', 'B', null, 'abc', 'abc1'] AS i RETURN min(i)"
val result = executeWithAllPlanners(query)
result.toList should equal(List(Map("min(i)" -> "B")))
}
}