/
DataFrameWindowFramesSuite.scala
419 lines (359 loc) · 15.6 KB
/
DataFrameWindowFramesSuite.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.sql
import java.sql.{Date, Timestamp}
import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.functions._
import org.apache.spark.sql.test.SharedSQLContext
import org.apache.spark.unsafe.types.CalendarInterval
/**
* Window frame testing for DataFrame API.
*/
class DataFrameWindowFramesSuite extends QueryTest with SharedSQLContext {
import testImplicits._
test("lead/lag with empty data frame") {
val df = Seq.empty[(Int, String)].toDF("key", "value")
val window = Window.partitionBy($"key").orderBy($"value")
checkAnswer(
df.select(
lead("value", 1).over(window),
lag("value", 1).over(window)),
Nil)
}
test("lead/lag with positive offset") {
val df = Seq((1, "1"), (2, "2"), (1, "3"), (2, "4")).toDF("key", "value")
val window = Window.partitionBy($"key").orderBy($"value")
checkAnswer(
df.select(
$"key",
lead("value", 1).over(window),
lag("value", 1).over(window)),
Row(1, "3", null) :: Row(1, null, "1") :: Row(2, "4", null) :: Row(2, null, "2") :: Nil)
}
test("reverse lead/lag with positive offset") {
val df = Seq((1, "1"), (2, "2"), (1, "3"), (2, "4")).toDF("key", "value")
val window = Window.partitionBy($"key").orderBy($"value".desc)
checkAnswer(
df.select(
$"key",
lead("value", 1).over(window),
lag("value", 1).over(window)),
Row(1, "1", null) :: Row(1, null, "3") :: Row(2, "2", null) :: Row(2, null, "4") :: Nil)
}
test("lead/lag with negative offset") {
val df = Seq((1, "1"), (2, "2"), (1, "3"), (2, "4")).toDF("key", "value")
val window = Window.partitionBy($"key").orderBy($"value")
checkAnswer(
df.select(
$"key",
lead("value", -1).over(window),
lag("value", -1).over(window)),
Row(1, null, "3") :: Row(1, "1", null) :: Row(2, null, "4") :: Row(2, "2", null) :: Nil)
}
test("reverse lead/lag with negative offset") {
val df = Seq((1, "1"), (2, "2"), (1, "3"), (2, "4")).toDF("key", "value")
val window = Window.partitionBy($"key").orderBy($"value".desc)
checkAnswer(
df.select(
$"key",
lead("value", -1).over(window),
lag("value", -1).over(window)),
Row(1, null, "1") :: Row(1, "3", null) :: Row(2, null, "2") :: Row(2, "4", null) :: Nil)
}
test("lead/lag with default value") {
val default = "n/a"
val df = Seq((1, "1"), (2, "2"), (1, "3"), (2, "4"), (2, "5")).toDF("key", "value")
val window = Window.partitionBy($"key").orderBy($"value")
checkAnswer(
df.select(
$"key",
lead("value", 2, default).over(window),
lag("value", 2, default).over(window),
lead("value", -2, default).over(window),
lag("value", -2, default).over(window)),
Row(1, default, default, default, default) :: Row(1, default, default, default, default) ::
Row(2, "5", default, default, "5") :: Row(2, default, "2", "2", default) ::
Row(2, default, default, default, default) :: Nil)
}
test("rows/range between with empty data frame") {
val df = Seq.empty[(String, Int)].toDF("key", "value")
val window = Window.partitionBy($"key").orderBy($"value")
checkAnswer(
df.select(
'key,
first("value").over(
window.rowsBetween(Window.unboundedPreceding, Window.unboundedFollowing)),
first("value").over(
window.rangeBetween(Window.unboundedPreceding, Window.unboundedFollowing))),
Nil)
}
test("rows between should accept int/long values as boundary") {
val df = Seq((1L, "1"), (1L, "1"), (2147483650L, "1"), (3L, "2"), (2L, "1"), (2147483650L, "2"))
.toDF("key", "value")
checkAnswer(
df.select(
$"key",
count("key").over(
Window.partitionBy($"value").orderBy($"key").rowsBetween(0, 2147483647))),
Seq(Row(1, 3), Row(1, 4), Row(2, 2), Row(3, 2), Row(2147483650L, 1), Row(2147483650L, 1))
)
val e = intercept[AnalysisException](
df.select(
$"key",
count("key").over(
Window.partitionBy($"value").orderBy($"key").rowsBetween(0, 2147483648L))))
assert(e.message.contains("Boundary end is not a valid integer: 2147483648"))
}
test("range between should accept at most one ORDER BY expression when unbounded") {
val df = Seq((1, 1)).toDF("key", "value")
val window = Window.orderBy($"key", $"value")
checkAnswer(
df.select(
$"key",
min("key").over(
window.rangeBetween(Window.unboundedPreceding, Window.unboundedFollowing))),
Seq(Row(1, 1))
)
val e1 = intercept[AnalysisException](
df.select(
min("key").over(window.rangeBetween(Window.unboundedPreceding, 1))))
assert(e1.message.contains("A range window frame with value boundaries cannot be used in a " +
"window specification with multiple order by expressions"))
val e2 = intercept[AnalysisException](
df.select(
min("key").over(window.rangeBetween(-1, Window.unboundedFollowing))))
assert(e2.message.contains("A range window frame with value boundaries cannot be used in a " +
"window specification with multiple order by expressions"))
val e3 = intercept[AnalysisException](
df.select(
min("key").over(window.rangeBetween(-1, 1))))
assert(e3.message.contains("A range window frame with value boundaries cannot be used in a " +
"window specification with multiple order by expressions"))
}
test("range between should accept numeric values only when bounded") {
val df = Seq("non_numeric").toDF("value")
val window = Window.orderBy($"value")
checkAnswer(
df.select(
$"value",
min("value").over(
window.rangeBetween(Window.unboundedPreceding, Window.unboundedFollowing))),
Row("non_numeric", "non_numeric") :: Nil)
val e1 = intercept[AnalysisException](
df.select(
min("value").over(window.rangeBetween(Window.unboundedPreceding, 1))))
assert(e1.message.contains("The data type of the upper bound 'string' " +
"does not match the expected data type"))
val e2 = intercept[AnalysisException](
df.select(
min("value").over(window.rangeBetween(-1, Window.unboundedFollowing))))
assert(e2.message.contains("The data type of the lower bound 'string' " +
"does not match the expected data type"))
val e3 = intercept[AnalysisException](
df.select(
min("value").over(window.rangeBetween(-1, 1))))
assert(e3.message.contains("The data type of the lower bound 'string' " +
"does not match the expected data type"))
}
test("range between should accept int/long values as boundary") {
val df = Seq((1L, "1"), (1L, "1"), (2147483650L, "1"), (3L, "2"), (2L, "1"), (2147483650L, "2"))
.toDF("key", "value")
checkAnswer(
df.select(
$"key",
count("key").over(
Window.partitionBy($"value").orderBy($"key").rangeBetween(0, 2147483648L))),
Seq(Row(1, 3), Row(1, 3), Row(2, 2), Row(3, 2), Row(2147483650L, 1), Row(2147483650L, 1))
)
checkAnswer(
df.select(
$"key",
count("key").over(
Window.partitionBy($"value").orderBy($"key").rangeBetween(-2147483649L, 0))),
Seq(Row(1, 2), Row(1, 2), Row(2, 3), Row(2147483650L, 2), Row(2147483650L, 4), Row(3, 1))
)
def dt(date: String): Date = Date.valueOf(date)
val df2 = Seq((dt("2017-08-01"), "1"), (dt("2017-08-01"), "1"), (dt("2020-12-31"), "1"),
(dt("2017-08-03"), "2"), (dt("2017-08-02"), "1"), (dt("2020-12-31"), "2"))
.toDF("key", "value")
val window = Window.partitionBy($"value").orderBy($"key").rangeBetween(lit(0), lit(2))
checkAnswer(
df2.select(
$"key",
count("key").over(window)),
Seq(Row(dt("2017-08-01"), 3), Row(dt("2017-08-01"), 3), Row(dt("2020-12-31"), 1),
Row(dt("2017-08-03"), 1), Row(dt("2017-08-02"), 1), Row(dt("2020-12-31"), 1))
)
}
test("range between should accept double values as boundary") {
val df = Seq((1.0D, "1"), (1.0D, "1"), (100.001D, "1"), (3.3D, "2"), (2.02D, "1"),
(100.001D, "2")).toDF("key", "value")
val window = Window.partitionBy($"value").orderBy($"key").rangeBetween(currentRow, lit(2.5D))
checkAnswer(
df.select(
$"key",
count("key").over(window)),
Seq(Row(1.0, 3), Row(1.0, 3), Row(100.001, 1), Row(3.3, 1), Row(2.02, 1), Row(100.001, 1))
)
}
test("range between should accept interval values as boundary") {
def ts(timestamp: Long): Timestamp = new Timestamp(timestamp * 1000)
val df = Seq((ts(1501545600), "1"), (ts(1501545600), "1"), (ts(1609372800), "1"),
(ts(1503000000), "2"), (ts(1502000000), "1"), (ts(1609372800), "2"))
.toDF("key", "value")
val window = Window.partitionBy($"value").orderBy($"key")
.rangeBetween(currentRow, lit(CalendarInterval.fromString("interval 23 days 4 hours")))
checkAnswer(
df.select(
$"key",
count("key").over(window)),
Seq(Row(ts(1501545600), 3), Row(ts(1501545600), 3), Row(ts(1609372800), 1),
Row(ts(1503000000), 1), Row(ts(1502000000), 1), Row(ts(1609372800), 1))
)
}
test("unbounded rows/range between with aggregation") {
val df = Seq(("one", 1), ("two", 2), ("one", 3), ("two", 4)).toDF("key", "value")
val window = Window.partitionBy($"key").orderBy($"value")
checkAnswer(
df.select(
'key,
sum("value").over(window.
rowsBetween(Window.unboundedPreceding, Window.unboundedFollowing)),
sum("value").over(window.
rangeBetween(Window.unboundedPreceding, Window.unboundedFollowing))),
Row("one", 4, 4) :: Row("one", 4, 4) :: Row("two", 6, 6) :: Row("two", 6, 6) :: Nil)
}
test("unbounded preceding/following rows between with aggregation") {
val df = Seq((1, "1"), (2, "2"), (2, "3"), (1, "3"), (3, "2"), (4, "3")).toDF("key", "value")
val window = Window.partitionBy($"value").orderBy($"key")
checkAnswer(
df.select(
$"key",
last("key").over(
window.rowsBetween(Window.currentRow, Window.unboundedFollowing)),
last("key").over(
window.rowsBetween(Window.unboundedPreceding, Window.currentRow))),
Row(1, 1, 1) :: Row(2, 3, 2) :: Row(3, 3, 3) :: Row(1, 4, 1) :: Row(2, 4, 2) ::
Row(4, 4, 4) :: Nil)
}
test("reverse unbounded preceding/following rows between with aggregation") {
val df = Seq((1, "1"), (2, "2"), (2, "3"), (1, "3"), (3, "2"), (4, "3")).toDF("key", "value")
val window = Window.partitionBy($"value").orderBy($"key".desc)
checkAnswer(
df.select(
$"key",
last("key").over(
window.rowsBetween(Window.currentRow, Window.unboundedFollowing)),
last("key").over(
window.rowsBetween(Window.unboundedPreceding, Window.currentRow))),
Row(1, 1, 1) :: Row(3, 2, 3) :: Row(2, 2, 2) :: Row(4, 1, 4) :: Row(2, 1, 2) ::
Row(1, 1, 1) :: Nil)
}
test("unbounded preceding/following range between with aggregation") {
val df = Seq((5, "1"), (5, "2"), (4, "2"), (6, "2"), (3, "1"), (2, "2")).toDF("key", "value")
val window = Window.partitionBy("value").orderBy("key")
checkAnswer(
df.select(
$"key",
avg("key").over(window.rangeBetween(Window.unboundedPreceding, 1))
.as("avg_key1"),
avg("key").over(window.rangeBetween(Window.currentRow, Window.unboundedFollowing))
.as("avg_key2")),
Row(3, 3.0d, 4.0d) :: Row(5, 4.0d, 5.0d) :: Row(2, 2.0d, 17.0d / 4.0d) ::
Row(4, 11.0d / 3.0d, 5.0d) :: Row(5, 17.0d / 4.0d, 11.0d / 2.0d) ::
Row(6, 17.0d / 4.0d, 6.0d) :: Nil)
}
// This is here to illustrate the fact that reverse order also reverses offsets.
test("reverse preceding/following range between with aggregation") {
val df = Seq(1, 2, 4, 3, 2, 1).toDF("value")
val window = Window.orderBy($"value".desc)
checkAnswer(
df.select(
$"value",
sum($"value").over(window.rangeBetween(Window.unboundedPreceding, 1)),
sum($"value").over(window.rangeBetween(1, Window.unboundedFollowing))),
Row(1, 13, null) :: Row(2, 13, 2) :: Row(4, 7, 9) :: Row(3, 11, 6) ::
Row(2, 13, 2) :: Row(1, 13, null) :: Nil)
}
test("sliding rows between with aggregation") {
val df = Seq((1, "1"), (2, "1"), (2, "2"), (1, "1"), (2, "2")).toDF("key", "value")
val window = Window.partitionBy($"value").orderBy($"key").rowsBetween(-1, 2)
checkAnswer(
df.select(
$"key",
avg("key").over(window)),
Row(1, 4.0d / 3.0d) :: Row(1, 4.0d / 3.0d) :: Row(2, 3.0d / 2.0d) :: Row(2, 2.0d) ::
Row(2, 2.0d) :: Nil)
}
test("reverse sliding rows between with aggregation") {
val df = Seq((1, "1"), (2, "1"), (2, "2"), (1, "1"), (2, "2")).toDF("key", "value")
val window = Window.partitionBy($"value").orderBy($"key".desc).rowsBetween(-1, 2)
checkAnswer(
df.select(
$"key",
avg("key").over(window)),
Row(1, 1.0d) :: Row(1, 4.0d / 3.0d) :: Row(2, 4.0d / 3.0d) :: Row(2, 2.0d) ::
Row(2, 2.0d) :: Nil)
}
test("sliding range between with aggregation") {
val df = Seq((1, "1"), (1, "1"), (3, "1"), (2, "2"), (2, "1"), (2, "2")).toDF("key", "value")
val window = Window.partitionBy($"value").orderBy($"key").rangeBetween(-1, 1)
checkAnswer(
df.select(
$"key",
avg("key").over(window)),
Row(1, 4.0d / 3.0d) :: Row(1, 4.0d / 3.0d) :: Row(2, 7.0d / 4.0d) :: Row(3, 5.0d / 2.0d) ::
Row(2, 2.0d) :: Row(2, 2.0d) :: Nil)
}
test("reverse sliding range between with aggregation") {
val df = Seq(
(1, "Thin", "Cell Phone", 6000),
(2, "Normal", "Tablet", 1500),
(3, "Mini", "Tablet", 5500),
(4, "Ultra thin", "Cell Phone", 5500),
(5, "Very thin", "Cell Phone", 6000),
(6, "Big", "Tablet", 2500),
(7, "Bendable", "Cell Phone", 3000),
(8, "Foldable", "Cell Phone", 3000),
(9, "Pro", "Tablet", 4500),
(10, "Pro2", "Tablet", 6500)).
toDF("id", "product", "category", "revenue")
val window = Window.partitionBy($"category").orderBy($"revenue".desc).
rangeBetween(-2000L, 1000L)
checkAnswer(
df.select(
$"id",
avg($"revenue").over(window).cast("int")),
Row(1, 5833) :: Row(2, 2000) :: Row(3, 5500) ::
Row(4, 5833) :: Row(5, 5833) :: Row(6, 2833) ::
Row(7, 3000) :: Row(8, 3000) :: Row(9, 5500) ::
Row(10, 6000) :: Nil)
}
test("SPARK-24033: Analysis Failure of OffsetWindowFunction") {
val ds = Seq((1, 1), (1, 2), (1, 3), (2, 1), (2, 2)).toDF("n", "i")
val res =
Row(1, 1, null) :: Row (1, 2, 1) :: Row(1, 3, 2) :: Row(2, 1, null) :: Row(2, 2, 1) :: Nil
checkAnswer(
ds.withColumn("m",
lead("i", -1).over(Window.partitionBy("n").orderBy("i").rowsBetween(-1, -1))),
res)
checkAnswer(
ds.withColumn("m",
lag("i", 1).over(Window.partitionBy("n").orderBy("i").rowsBetween(-1, -1))),
res)
}
}