/
AggregateFunctionsTests.scala
241 lines (204 loc) · 6.7 KB
/
AggregateFunctionsTests.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
package frameless
package functions
import frameless.functions.aggregate._
import org.scalacheck.Prop
import org.scalacheck.Prop._
class AggregateFunctionsTests extends TypedDatasetSuite {
def approximatelyEqual[A](a: A, b: A)(implicit numeric: Numeric[A]): Prop = {
val da = numeric.toDouble(a)
val db = numeric.toDouble(b)
val epsilon = 1E-6
// Spark has a weird behaviour concerning expressions that should return Inf
// Most of the time they return NaN instead, for instance stddev of Seq(-7.827553978923477E227, -5.009124275715786E153)
if((da.isNaN || da.isInfinity) && (db.isNaN || db.isInfinity)) proved
else if (
(da - db).abs < epsilon ||
(da - db).abs < da.abs / 100)
proved
else falsified :| s"Expected $a but got $b, which is more than 1% off and greater than epsilon = $epsilon."
}
test("sum") {
def prop[A: TypedEncoder : Numeric : Summable](xs: List[A])(
implicit
eoa: TypedEncoder[Option[A]],
ex1: TypedEncoder[X1[A]]
): Prop = {
val dataset = TypedDataset.create(xs.map(X1(_)))
val A = dataset.col[A]('a)
val datasetSum = dataset.select(sum(A)).collect().run().toList
datasetSum match {
case x :: Nil => approximatelyEqual(x, xs.sum)
case other => falsified
}
}
check(forAll(prop[BigDecimal] _))
check(forAll(prop[Long] _))
check(forAll(prop[Double] _))
// doesn't work yet because resulting type is different
// check(forAll(prop[Int] _)
// check(forAll(prop[Short] _)
// check(forAll(prop[Byte] _)
}
test("avg") {
def prop[A: TypedEncoder : Averageable](xs: List[A])(
implicit
fractional: Fractional[A],
eoa: TypedEncoder[Option[A]],
ex1: TypedEncoder[X1[A]]
): Prop = {
val dataset = TypedDataset.create(xs.map(X1(_)))
val A = dataset.col[A]('a)
val Vector(datasetAvg) = dataset.select(avg(A)).collect().run().toVector
xs match {
case Nil => datasetAvg ?= None
case _ :: _ => datasetAvg match {
case Some(x) => approximatelyEqual(fractional.div(xs.sum, fractional.fromInt(xs.size)), x)
case other => falsified
}
}
}
check(forAll(prop[BigDecimal] _))
check(forAll(prop[Double] _))
}
test("stddev") {
def prop[A: TypedEncoder : Variance : Fractional : Numeric](xs: List[A])(
implicit
eoa: TypedEncoder[Option[A]],
ex1: TypedEncoder[X1[A]]
): Prop = {
val dataset = TypedDataset.create(xs.map(X1(_)))
val A = dataset.col[A]('a)
val Vector(datasetStd) = dataset.select(stddev(A)).collect().run().toVector
val std = sc.parallelize(xs.map(implicitly[Numeric[A]].toDouble)).sampleStdev()
xs match {
case Nil => datasetStd ?= None
case _ :: Nil => datasetStd match {
case Some(x) => if (implicitly[Numeric[A]].toDouble(x).isNaN) proved else falsified
case _ => falsified
}
case _ => datasetStd match {
case Some(x) => approximatelyEqual(std, implicitly[Numeric[A]].toDouble(x))
case _ => falsified
}
}
}
check(forAll(prop[Double] _))
}
test("count") {
def prop[A: TypedEncoder](xs: List[A]): Prop = {
val dataset = TypedDataset.create(xs)
val Vector(datasetCount) = dataset.select(count()).collect().run().toVector
datasetCount ?= xs.size.toLong
}
check(forAll(prop[Int] _))
check(forAll(prop[Byte] _))
}
test("count('a)") {
def prop[A: TypedEncoder](xs: List[A])(implicit ex1: TypedEncoder[X1[A]]): Prop = {
val dataset = TypedDataset.create(xs.map(X1(_)))
val A = dataset.col[A]('a)
val Vector(datasetCount) = dataset.select(count(A)).collect().run().toVector
datasetCount ?= xs.size.toLong
}
check(forAll(prop[Int] _))
check(forAll(prop[Byte] _))
}
test("max") {
def prop[A: TypedEncoder : Ordering](xs: List[A])(
implicit
ex1: TypedEncoder[X1[A]],
eoa: TypedEncoder[Option[A]]
): Prop = {
val dataset = TypedDataset.create(xs.map(X1(_)))
val A = dataset.col[A]('a)
val datasetMax = dataset.select(max(A)).collect().run().toList.head
xs match {
case Nil => datasetMax.isEmpty
case xs => datasetMax match {
case Some(m) => xs.max ?= m
case _ => falsified
}
}
}
check(forAll(prop[Long] _))
check(forAll(prop[Double] _))
check(forAll(prop[Int] _))
check(forAll(prop[Short] _))
check(forAll(prop[Byte] _))
check(forAll(prop[String] _))
}
test("min") {
def prop[A: TypedEncoder : Ordering](xs: List[A])(
implicit
ex1: TypedEncoder[X1[A]],
eoa: TypedEncoder[Option[A]]
): Prop = {
val dataset = TypedDataset.create(xs.map(X1(_)))
val A = dataset.col[A]('a)
val datasetMin = dataset.select(min(A)).collect().run().toList.head
xs match {
case Nil => datasetMin.isEmpty
case xs => datasetMin match {
case Some(m) => xs.min ?= m
case _ => falsified
}
}
}
check(forAll(prop[Long] _))
check(forAll(prop[Double] _))
check(forAll(prop[Int] _))
check(forAll(prop[Short] _))
check(forAll(prop[Byte] _))
check(forAll(prop[String] _))
}
test("first") {
def prop[A: TypedEncoder](xs: List[A])(
implicit
ex1: TypedEncoder[X1[A]],
eoa: TypedEncoder[Option[A]]
): Prop = {
val dataset = TypedDataset.create(xs.map(X1(_)))
val A = dataset.col[A]('a)
val datasetFirst :: Nil = dataset.select(first(A)).collect().run().toList
xs match {
case Nil => datasetFirst.isEmpty
case x::_ => datasetFirst match {
case Some(m) => x ?= m
case _ => falsified
}
}
}
check(forAll(prop[BigDecimal] _))
check(forAll(prop[Long] _))
check(forAll(prop[Double] _))
check(forAll(prop[Int] _))
check(forAll(prop[Short] _))
check(forAll(prop[Byte] _))
check(forAll(prop[String] _))
}
test("last") {
def prop[A: TypedEncoder](xs: List[A])(
implicit
ex1: TypedEncoder[X1[A]],
eoa: TypedEncoder[Option[A]]
): Prop = {
val dataset = TypedDataset.create(xs.map(X1(_)))
val A = dataset.col[A]('a)
val datasetLast :: Nil = dataset.select(last(A)).collect().run().toList
xs match {
case Nil => datasetLast.isEmpty
case xs => datasetLast match {
case Some(m) => xs.last ?= m
case _ => falsified
}
}
}
check(forAll(prop[BigDecimal] _))
check(forAll(prop[Long] _))
check(forAll(prop[Double] _))
check(forAll(prop[Int] _))
check(forAll(prop[Short] _))
check(forAll(prop[Byte] _))
check(forAll(prop[String] _))
}
}