forked from hail-is/hail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exists.scala
124 lines (111 loc) · 3.23 KB
/
Exists.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
package is.hail.expr.ir
import is.hail.expr._
import is.hail.utils.{BoxedArrayBuilder, _}
import scala.collection.mutable._
//
// Search an IR tree for the first node satisfying some condition
//
object Exists {
def inIR(node: IR, visitor: IR => Boolean): Boolean = {
if (visitor(node))
true
else
node.children.exists {
case child: TableAggregate => visitor(child)
case child: MatrixAggregate => visitor(child)
case child: IR => inIR(child, visitor)
case _ => false
}
}
def apply(node: BaseIR, visitor: BaseIR => Boolean): Boolean = {
if (visitor(node))
true
else
node.children.exists(Exists(_, visitor))
}
}
object Forall {
def apply(node: IR, visitor: IR => Boolean): Boolean =
!Exists.inIR(node, n => !visitor(n))
}
object IsScanResult {
def apply(root: IR): Boolean = root match {
case _: ApplyScanOp => true
case AggFold(_, _, _, _, _, isScan) => isScan
case AggFilter(_, _, isScan) => isScan
case AggExplode(_, _, _, isScan) => isScan
case AggGroupBy(_, _, isScan) => isScan
case AggArrayPerElement(_, _, _, _, _, isScan) => isScan
case _ => false
}
}
object IsAggResult {
def apply(root: IR): Boolean = root match {
case _: ApplyAggOp => true
case _: AggFold => true
case AggFold(_, _, _, _, _, isScan) => !isScan
case AggFilter(_, _, isScan) => !isScan
case AggExplode(_, _, _, isScan) => !isScan
case AggGroupBy(_, _, isScan) => !isScan
case AggArrayPerElement(_, _, _, _, _, isScan) => !isScan
case _ => false
}
}
object ContainsAgg {
def apply(root: IR): Boolean = IsAggResult(root) || (root match {
case l: AggLet => !l.isScan
case _: TableAggregate => false
case _: MatrixAggregate => false
case _: StreamAgg => false
case _ => root.children.exists {
case child: IR => ContainsAgg(child)
case _ => false
}
})
}
object ContainsAggIntermediate {
def apply(root: IR): Boolean = (root match {
case _: ResultOp => true
case _: SeqOp => true
case _: InitOp => true
case _: CombOp => true
case _: DeserializeAggs => true
case _: SerializeAggs => true
case _: AggStateValue => true
case _: CombOpValue => true
case _: InitFromSerializedValue => true
case _ => false
}) || root.children.exists {
case child: IR => ContainsAggIntermediate(child)
case _ => false
}
}
object AggIsCommutative {
def apply(op: AggOp): Boolean = op match {
case Take() | Collect() | PrevNonnull() | TakeBy(_) | ReservoirSample() => false
case _ => true
}
}
object ContainsNonCommutativeAgg {
def apply(root: IR): Boolean = root match {
case ApplyAggOp(_, _, sig) => !AggIsCommutative(sig.op)
case _: TableAggregate => false
case _: MatrixAggregate => false
case _ => root.children.exists {
case child: IR => ContainsNonCommutativeAgg(child)
case _ => false
}
}
}
object ContainsScan {
def apply(root: IR): Boolean = IsScanResult(root) || (root match {
case l: AggLet => l.isScan
case _: TableAggregate => false
case _: MatrixAggregate => false
case _: StreamAggScan => false
case _ => root.children.exists {
case child: IR => ContainsScan(child)
case _ => false
}
})
}