-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memo.scala
53 lines (48 loc) · 1.79 KB
/
Memo.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
package core.planner.volcano.memo
import core.planner.volcano.logicalplan.LogicalPlan
import utils.generator.{Generator, LongGenerator}
import scala.collection.mutable
class Memo(
groupIdGenerator: Generator[Long] = new LongGenerator,
groupExpressionIdGenerator: Generator[Long] = new LongGenerator
) {
val groups: mutable.HashMap[Long, Group] = mutable.HashMap[Long, Group]()
val parents: mutable.HashMap[Long, Group] = mutable.HashMap[Long, Group]() // lookup group from group expression ID
val groupExpressions: mutable.HashMap[LogicalPlan, GroupExpression] = mutable.HashMap[LogicalPlan, GroupExpression]()
def getOrCreateGroupExpression(plan: LogicalPlan): GroupExpression = {
val children = plan.children()
val childGroups = children.map(child => getOrCreateGroup(child))
groupExpressions.get(plan) match {
case Some(found) => found
case None =>
val id = groupExpressionIdGenerator.generate()
val children = mutable.MutableList() ++ childGroups
val expression = GroupExpression(
id = id,
plan = plan,
children = children
)
groupExpressions += plan -> expression
expression
}
}
def getOrCreateGroup(plan: LogicalPlan): Group = {
val exprGroup = getOrCreateGroupExpression(plan)
val group = parents.get(exprGroup.id) match {
case Some(group) =>
group.equivalents += exprGroup
group
case None =>
val id = groupIdGenerator.generate()
val equivalents = mutable.HashSet() + exprGroup
val group = Group(
id = id,
equivalents = equivalents
)
groups.put(id, group)
group
}
parents += exprGroup.id -> group
group
}
}