-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
test-dplyr-join.R
441 lines (390 loc) · 10.2 KB
/
test-dplyr-join.R
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# 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.
library(dplyr, warn.conflicts = FALSE)
left <- example_data
left$some_grouping <- rep(c(1, 2), 5)
to_join <- tibble::tibble(
some_grouping = c(1, 2, 3),
capital_letters = c("A", "B", "C"),
another_column = TRUE
)
test_that("left_join with automatic grouping", {
expect_identical(
as_record_batch(left) %>%
left_join(to_join) %>%
collect(),
left %>%
left_join(to_join, by = "some_grouping") %>%
collect()
)
})
test_that("left_join `by` args", {
compare_dplyr_binding(
.input %>%
left_join(to_join, by = "some_grouping") %>%
collect(),
left
)
compare_dplyr_binding(
.input %>%
left_join(
to_join %>%
rename(the_grouping = some_grouping),
by = c(some_grouping = "the_grouping")
) %>%
collect(),
left
)
compare_dplyr_binding(
.input %>%
rename(the_grouping = some_grouping) %>%
left_join(
to_join,
by = c(the_grouping = "some_grouping")
) %>%
collect(),
left
)
})
test_that("left_join with join_by", {
# only run this test in newer versions of dplyr that include `join_by()`
skip_if_not(packageVersion("dplyr") >= "1.0.99.9000")
compare_dplyr_binding(
.input %>%
left_join(to_join, join_by(some_grouping)) %>%
collect(),
left
)
compare_dplyr_binding(
.input %>%
left_join(
to_join %>%
rename(the_grouping = some_grouping),
join_by(some_grouping == the_grouping)
) %>%
collect(),
left
)
compare_dplyr_binding(
.input %>%
rename(the_grouping = some_grouping) %>%
left_join(
to_join,
join_by(the_grouping == some_grouping)
) %>%
collect(),
left
)
})
test_that("join two tables", {
expect_identical(
arrow_table(left) %>%
left_join(arrow_table(to_join), by = "some_grouping") %>%
collect(),
left %>%
left_join(to_join, by = "some_grouping") %>%
collect()
)
})
test_that("Error handling", {
expect_error(
arrow_table(left) %>%
left_join(to_join, by = "not_a_col") %>%
collect(),
"Join columns must be present in data"
)
# we print both message_x and message_y with an unnamed `by` vector
expect_snapshot(
left_join(
arrow_table(example_data),
arrow_table(example_data),
by = "made_up_colname"
),
error = TRUE
)
# we only print message_y as `int` is a column of x
expect_snapshot(
left_join(
arrow_table(example_data),
arrow_table(example_data),
by = c("int" = "made_up_colname")
),
error = TRUE
)
# we only print message_x as `int` is a column of y
expect_snapshot(
left_join(
arrow_table(example_data),
arrow_table(example_data),
by = c("made_up_colname" = "int")
),
error = TRUE
)
# we print both message_x and message_y
expect_snapshot(
left_join(
arrow_table(example_data),
arrow_table(example_data),
by = c("made_up_colname1", "made_up_colname2")
),
error = TRUE
)
expect_snapshot(
left_join(
arrow_table(example_data),
arrow_table(example_data),
by = c("made_up_colname1" = "made_up_colname2")
),
error = TRUE
)
})
test_that("Error handling for unsupported expressions in join_by", {
# only run this test in newer versions of dplyr that include `join_by()`
skip_if_not(packageVersion("dplyr") >= "1.0.99.9000")
expect_error(
arrow_table(left) %>%
left_join(to_join, join_by(some_grouping >= some_grouping)),
"not supported"
)
expect_error(
arrow_table(left) %>%
left_join(to_join, join_by(closest(some_grouping >= some_grouping))),
"not supported"
)
})
# TODO: test duplicate col names
# TODO: casting: int and float columns?
test_that("right_join", {
compare_dplyr_binding(
.input %>%
right_join(to_join, by = "some_grouping", keep = TRUE) %>%
collect(),
left
)
compare_dplyr_binding(
.input %>%
right_join(to_join, by = "some_grouping", keep = FALSE) %>%
collect(),
left
)
})
test_that("inner_join", {
compare_dplyr_binding(
.input %>%
inner_join(to_join, by = "some_grouping", keep = TRUE) %>%
collect(),
left
)
compare_dplyr_binding(
.input %>%
inner_join(to_join, by = "some_grouping", keep = FALSE) %>%
collect(),
left
)
})
test_that("full_join", {
compare_dplyr_binding(
.input %>%
full_join(to_join, by = "some_grouping", keep = TRUE) %>%
collect(),
left
)
compare_dplyr_binding(
.input %>%
full_join(to_join, by = "some_grouping", keep = FALSE) %>%
collect(),
left
)
})
test_that("semi_join", {
compare_dplyr_binding(
.input %>%
semi_join(to_join, by = "some_grouping") %>%
collect(),
left
)
})
test_that("anti_join", {
compare_dplyr_binding(
.input %>%
# Factor levels when there are no rows in the data don't match
# TODO: use better anti_join test data
select(-fct) %>%
anti_join(to_join, by = "some_grouping") %>%
collect(),
left
)
})
test_that("arrow dplyr query correctly mutates then joins", {
left <- Table$create(
one = c("a", "b"),
two = 1:2
)
right <- Table$create(
three = TRUE,
dos = 2L
)
expect_equal(
left %>%
rename(dos = two) %>%
# Use the ASCII version so we don't need utf8proc for this test
mutate(one = arrow_ascii_upper(one)) %>%
left_join(
right %>%
mutate(three = !three)
) %>%
arrange(dos) %>%
collect(),
tibble(
one = c("A", "B"),
dos = 1:2,
three = c(NA, FALSE)
)
)
})
test_that("arrow dplyr query correctly filters then joins", {
left <- Table$create(
one = c("a", "b", "c"),
two = 1:3
)
right <- Table$create(
three = c(FALSE, TRUE, NA),
dos = c(2L, 3L, 4L)
)
expect_equal(
left %>%
rename(dos = two) %>%
filter(one %in% letters[1:2]) %>%
left_join(
right %>%
filter(!is.na(three))
) %>%
arrange(dos) %>%
collect(),
tibble(
one = c("a", "b"),
dos = 1:2,
three = c(NA, FALSE)
)
)
})
test_that("suffix", {
left_suf <- Table$create(
key = c(1, 2),
left_unique = c(2.1, 3.1),
shared = c(10.1, 10.3)
)
right_suf <- Table$create(
key = c(1, 2, 3, 10, 20),
right_unique = c(1.1, 1.2, 3.1, 4.1, 4.3),
shared = c(20.1, 30, 40, 50, 60)
)
join_op <- inner_join(left_suf, right_suf, by = "key", suffix = c("_left", "_right"))
output <- collect(join_op)
res_col_names <- names(output)
expected_col_names <- c("key", "left_unique", "shared_left", "right_unique", "shared_right")
expect_equal(expected_col_names, res_col_names)
})
test_that("suffix and implicit schema", {
left_suf <- Table$create(
key = c(1, 2),
left_unique = c(2.1, 3.1),
shared = c(10.1, 10.3)
)
right_suf <- Table$create(
key = c(1, 2, 3, 10, 20),
right_unique = c(1.1, 1.2, 3.1, 4.1, 4.3),
shared = c(20.1, 30, 40, 50, 60)
)
join_op <- inner_join(left_suf, right_suf, by = "key", suffix = c("_left", "_right"))
output <- collect(join_op)
impl_schema <- implicit_schema(join_op)
expect_equal(names(output), names(implicit_schema(join_op)))
})
test_that("summarize and join", {
left_suf <- Table$create(
key = c(1, 2, 1, 2),
left_unique = c(2.1, 3.1, 4.1, 6.1),
shared = c(10.1, 10.3, 10.2, 10.4)
)
right_suf <- Table$create(
key = c(1, 2, 3, 10, 20),
right_unique = c(1.1, 1.2, 3.1, 4.1, 4.3),
shared = c(20.1, 30, 40, 50, 60)
)
joined <- left_suf %>%
group_by(key) %>%
summarize(left_unique = mean(left_unique), shared = mean(shared)) %>%
inner_join(right_suf, by = "key", suffix = c("_left", "_right"))
output <- collect(joined)
res_col_names <- names(output)
expected_col_names <- c("key", "left_unique", "shared_left", "right_unique", "shared_right")
expect_equal(expected_col_names, res_col_names)
})
test_that("arrow dplyr query can join two datasets", {
# ARROW-14908 and ARROW-15718
skip_if_not_available("dataset")
# By default, snappy encoding will be used, and
# Snappy has a UBSan issue: https://github.com/google/snappy/pull/148
skip_on_linux_devel()
dir_out <- tempdir()
quakes %>%
select(stations, lat, long) %>%
group_by(stations) %>%
write_dataset(file.path(dir_out, "ds1"))
quakes %>%
select(stations, mag, depth) %>%
group_by(stations) %>%
write_dataset(file.path(dir_out, "ds2"))
withr::with_options(
list(arrow.use_threads = FALSE),
{
res <- open_dataset(file.path(dir_out, "ds1")) %>%
left_join(open_dataset(file.path(dir_out, "ds2")), by = "stations") %>%
collect() # We should not segfault here.
expect_equal(nrow(res), 21872)
}
)
})
test_that("full joins handle keep", {
full_data_df <- tibble::tibble(
x = rep(c("a", "b"), each = 5),
y = rep(1:5, 2),
z = rep("zzz", 10),
index = 1:10
)
small_dataset_df <- tibble::tibble(
value = c(0.1, 0.2, 0.3, 0.4, 0.5),
x = c(rep("a", 3), rep("b", 2)),
y = 1:5,
z = 6:10
)
compare_dplyr_binding(
.input %>%
full_join(full_data_df, by = c("y", "x"), keep = TRUE) %>%
arrange(index) %>%
collect(),
small_dataset_df
)
compare_dplyr_binding(
.input %>%
full_join(full_data_df, by = c("y", "x"), keep = FALSE) %>%
arrange(index) %>%
collect(),
small_dataset_df
)
})