-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CartesianProductsOrValueJoinsTest.scala
144 lines (127 loc) · 5.44 KB
/
CartesianProductsOrValueJoinsTest.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
/*
* Copyright (c) 2002-2017 "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_3.planner.logical.idp
import org.neo4j.cypher.internal.compiler.v3_3.planner._
import org.neo4j.cypher.internal.compiler.v3_3.planner.logical.plans._
import org.neo4j.cypher.internal.frontend.v3_3.ast.{AstConstructionTestSupport, Equals}
import org.neo4j.cypher.internal.frontend.v3_3.test_helpers.CypherFunSuite
import org.neo4j.cypher.internal.ir.v3_3._
class CartesianProductsOrValueJoinsTest
extends CypherFunSuite with LogicalPlanningTestSupport2 with AstConstructionTestSupport {
val planA = allNodesScan("a")
val planB = allNodesScan("b")
val planC = allNodesScan("c")
private def allNodesScan(n: String): LogicalPlan = {
val solved = CardinalityEstimation.lift(RegularPlannerQuery(queryGraph = QueryGraph(patternNodes = Set(IdName(n)))), Cardinality(0))
AllNodesScan(n, Set.empty)(solved)
}
test("should plan cartesian product between 2 pattern nodes") {
testThis(
graph = QueryGraph(patternNodes = Set("a", "b")),
input = Set(
PlannedComponent(QueryGraph(patternNodes = Set("a")), planA),
PlannedComponent(QueryGraph(patternNodes = Set("b")), planB)),
expectedPlan = CartesianProduct(
planA,
planB
)(solved)
)
}
test("should plan cartesian product between 3 pattern nodes") {
testThis(
graph = QueryGraph(patternNodes = Set("a", "b", "c")),
input = Set(
PlannedComponent(QueryGraph(patternNodes = Set("a")), planA),
PlannedComponent(QueryGraph(patternNodes = Set("b")), planB),
PlannedComponent(QueryGraph(patternNodes = Set("c")), planC)),
expectedPlan = CartesianProduct(
planC,
CartesianProduct(
planB,
planA
)(solved)
)(solved))
}
test("should plan cartesian product between lots of pattern nodes") {
val components = ('a' to 'z') map { x =>
PlannedComponent(QueryGraph(patternNodes = Set(x.toString)), allNodesScan(x.toString))
}
val includedPlans = components.map(_.plan).toSet
testThis(
graph = QueryGraph(patternNodes = Set("a", "b", "c")),
input = components.toSet,
assertion = (x: LogicalPlan) => {
val leaves = x.leaves
leaves.toSet should equal(includedPlans)
leaves.size should equal(components.size)
}
)
}
test("should plan computeHash join between 2 pattern nodes") {
val equality = Equals(prop("a", "id"), prop("b", "id"))(pos)
testThis(
graph = QueryGraph(
patternNodes = Set("a", "b"),
selections = Selections.from(equality)),
input = Set(
PlannedComponent(QueryGraph(patternNodes = Set("a")), planA),
PlannedComponent(QueryGraph(patternNodes = Set("b")), planB)),
expectedPlan = ValueHashJoin(planA, planB, equality)(solved))
}
test("should plan computeHash joins between 3 pattern nodes") {
val eq1 = Equals(prop("b", "id"), prop("a", "id"))(pos)
val eq2 = Equals(prop("b", "id"), prop("c", "id"))(pos)
val eq3 = Equals(prop("a", "id"), prop("c", "id"))(pos)
testThis(
graph = QueryGraph(
patternNodes = Set("a", "b", "c"),
selections = Selections.from(Seq(eq1, eq2, eq3))),
input = Set(
PlannedComponent(QueryGraph(patternNodes = Set("a")), planA),
PlannedComponent(QueryGraph(patternNodes = Set("b")), planB),
PlannedComponent(QueryGraph(patternNodes = Set("c")), planC)),
expectedPlan =
Selection(Seq(eq3),
ValueHashJoin(planA,
ValueHashJoin(planB, planC, eq2)(solved), eq1.switchSides)(solved))(solved))
}
private def testThis(graph: QueryGraph, input: Set[PlannedComponent], assertion: LogicalPlan => Unit): Unit = {
new given {
qg = graph
cardinality = mapCardinality {
case RegularPlannerQuery(queryGraph, _, _) if queryGraph.patternNodes == Set(IdName("a")) => 1000.0
case RegularPlannerQuery(queryGraph, _, _) if queryGraph.patternNodes == Set(IdName("b")) => 2000.0
case RegularPlannerQuery(queryGraph, _, _) if queryGraph.patternNodes == Set(IdName("c")) => 3000.0
case _ => 100.0
}
}.withLogicalPlanningContext { (cfg, ctx) =>
implicit val x = ctx
implicit val kit = ctx.config.toKit()
var plans: Set[PlannedComponent] = input
while (plans.size > 1) {
plans = cartesianProductsOrValueJoins(plans, cfg.qg)(ctx, kit, SingleComponentPlanner(mock[IDPQueryGraphSolverMonitor]))
}
val result = plans.head.plan
assertion(result)
}
}
private def testThis(graph: QueryGraph, input: Set[PlannedComponent], expectedPlan: LogicalPlan): Unit =
testThis(graph, input, (result: LogicalPlan) => result should equal(expectedPlan))
}