-
Notifications
You must be signed in to change notification settings - Fork 139
/
FilterTests.scala
184 lines (148 loc) · 6.43 KB
/
FilterTests.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
package frameless
import org.scalatest.matchers.should.Matchers
import org.scalacheck.Prop
import org.scalacheck.Prop._
final class FilterTests extends TypedDatasetSuite with Matchers {
test("filter('a == lit(b))") {
def prop[A: TypedEncoder](elem: A, data: Vector[X1[A]])(implicit ex1: TypedEncoder[X1[A]]): Prop = {
val dataset = TypedDataset.create(data)
val A = dataset.col('a)
val dataset2 = dataset.filter(A === elem).collect().run().toVector
val data2 = data.filter(_.a == elem)
dataset2 ?= data2
}
check(forAll(prop[Int] _))
check(forAll(prop[String] _))
}
test("filter('a =!= lit(b))") {
def prop[A: TypedEncoder](elem: A, data: Vector[X1[A]])(implicit ex1: TypedEncoder[X1[A]]): Prop = {
val dataset = TypedDataset.create(data)
val A = dataset.col('a)
val dataset2 = dataset.filter(A =!= elem).collect().run().toVector
val data2 = data.filter(_.a != elem)
dataset2 ?= data2
}
check(forAll(prop[Int] _))
check(forAll(prop[String] _))
check(forAll(prop[Char] _))
check(forAll(prop[Boolean] _))
check(forAll(prop[SQLTimestamp] _))
check(forAll(prop[Vector[SQLTimestamp]] _))
}
test("filter('a =!= 'b)") {
def prop[A: TypedEncoder](data: Vector[X2[A, A]]): Prop = {
val dataset = TypedDataset.create(data)
val A = dataset.col('a)
val B = dataset.col('b)
val dataset2 = dataset.filter(A =!= B).collect().run().toVector
val data2 = data.filter(x => x.a != x.b)
dataset2 ?= data2
}
check(forAll(prop[Int] _))
check(forAll(prop[String] _))
check(forAll(prop[Char] _))
check(forAll(prop[Boolean] _))
check(forAll(prop[SQLTimestamp] _))
check(forAll(prop[Vector[SQLTimestamp]] _))
}
test("filter('a =!= 'b") {
def prop[A: TypedEncoder](elem: A, data: Vector[X2[A,A]]): Prop = {
val dataset = TypedDataset.create(data)
val cA = dataset.col('a)
val cB = dataset.col('b)
val dataset2 = dataset.filter(cA =!= cB).collect().run().toVector
val data2 = data.filter(x => x.a != x.b )
(dataset2 ?= data2).&&(dataset.filter(cA =!= cA).count().run() ?= 0)
}
check(forAll(prop[Int] _))
check(forAll(prop[String] _))
check(forAll(prop[Char] _))
check(forAll(prop[SQLTimestamp] _))
check(forAll(prop[Vector[SQLTimestamp]] _))
}
test("filter with arithmetic expressions: addition") {
check(forAll { (data: Vector[X1[Int]]) =>
val ds = TypedDataset.create(data)
val res = ds.filter((ds('a) + 1) === (ds('a) + 1)).collect().run().toVector
res ?= data
})
}
test("filter with values (not columns): addition") {
check(forAll { (data: Vector[X1[Int]], const: Int) =>
val ds = TypedDataset.create(data)
val res = ds.filter(ds('a) > const).collect().run().toVector
res ?= data.filter(_.a > const)
})
}
test("filter with arithmetic expressions: multiplication") {
val t = X1(1) :: X1(2) :: X1(3) :: Nil
val tds: TypedDataset[X1[Int]] = TypedDataset.create(t)
assert(tds.filter(tds('a) * 2 === 2).collect().run().toVector === Vector(X1(1)))
assert(tds.filter(tds('a) * 3 === 3).collect().run().toVector === Vector(X1(1)))
}
test("Option equality/inequality for columns") {
def prop[A <: Option[_] : TypedEncoder](a: A, b: A): Prop = {
val data = X2(a, b) :: X2(a, a) :: Nil
val dataset = TypedDataset.create(data)
val A = dataset.col('a)
val B = dataset.col('b)
(data.filter(x => x.a == x.b).toSet ?= dataset.filter(A === B).collect().run().toSet).
&&(data.filter(x => x.a != x.b).toSet ?= dataset.filter(A =!= B).collect().run().toSet).
&&(data.filter(x => x.a == None).toSet ?= dataset.filter(A.isNone).collect().run().toSet).
&&(data.filter(x => x.a == None).toSet ?= dataset.filter(A.isNotNone === false).collect().run().toSet)
}
check(forAll(prop[Option[Int]] _))
check(forAll(prop[Option[Boolean]] _))
check(forAll(prop[Option[SQLDate]] _))
check(forAll(prop[Option[SQLTimestamp]] _))
check(forAll(prop[Option[X1[String]]] _))
check(forAll(prop[Option[X1[X1[String]]]] _))
check(forAll(prop[Option[X1[X1[Vector[Option[Int]]]]]] _))
}
test("Option equality/inequality for lit") {
def prop[A <: Option[_] : TypedEncoder](a: A, b: A, cLit: A): Prop = {
val data = X2(a, b) :: X2(a, cLit) :: Nil
val dataset = TypedDataset.create(data)
val colA = dataset.col('a)
(data.filter(x => x.a == cLit).toSet ?= dataset.filter(colA === cLit).collect().run().toSet).
&&(data.filter(x => x.a != cLit).toSet ?= dataset.filter(colA =!= cLit).collect().run().toSet).
&&(data.filter(x => x.a == None).toSet ?= dataset.filter(colA.isNone).collect().run().toSet).
&&(data.filter(x => x.a == None).toSet ?= dataset.filter(colA.isNotNone === false).collect().run().toSet)
}
check(forAll(prop[Option[Int]] _))
check(forAll(prop[Option[Boolean]] _))
check(forAll(prop[Option[SQLDate]] _))
check(forAll(prop[Option[SQLTimestamp]] _))
check(forAll(prop[Option[String]] _))
check(forAll(prop[Option[X1[String]]] _))
check(forAll(prop[Option[X1[X1[String]]]] _))
check(forAll(prop[Option[X1[X1[Vector[Option[Int]]]]]] _))
}
test("Option content filter") {
val data = (Option(1L), Option(2L)) :: (Option(0L), Option(1L)) :: (None, None) :: Nil
val ds = TypedDataset.create(data)
val l = functions.lit[Long, (Option[Long], Option[Long])](0L)
val exists = ds('_1).isSome[Long](_ <= l)
val forall = ds('_1).isSomeOrNone[Long](_ <= l)
ds.select(exists).collect().run() shouldEqual Seq(false, true, false)
ds.select(forall).collect().run() shouldEqual Seq(false, true, true)
ds.filter(exists).collect().run() shouldEqual Seq(Option(0L) -> Option(1L))
ds.filter(forall).collect().run() shouldEqual Seq(
Option(0L) -> Option(1L), (None -> None))
}
test("filter with isin values") {
def prop[A: TypedEncoder](data: Vector[X1[A]], values: Vector[A])(implicit a : CatalystIsin[A]): Prop = {
val ds = TypedDataset.create(data)
val res = ds.filter(ds('a).isin(values:_*)).collect().run().toVector
res ?= data.filter(d => values.contains(d.a))
}
check(forAll(prop[BigDecimal] _))
check(forAll(prop[Byte] _))
check(forAll(prop[Double] _))
check(forAll(prop[Float] _))
check(forAll(prop[Int] _))
check(forAll(prop[Long] _))
check(forAll(prop[Short] _))
check(forAll(prop[String] _))
}
}