-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
test-dplyr-mutate.R
395 lines (360 loc) · 9.41 KB
/
test-dplyr-mutate.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
# 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)
library(stringr)
tbl <- example_data
# Add some better string data
tbl$verses <- verses[[1]]
# c(" a ", " b ", " c ", ...) increasing padding
# nchar = 3 5 7 9 11 13 15 17 19 21
tbl$padded_strings <- stringr::str_pad(letters[1:10], width = 2*(1:10)+1, side = "both")
test_that("mutate() is lazy", {
expect_is(
tbl %>% record_batch() %>% mutate(int = int + 6L),
"arrow_dplyr_query"
)
})
test_that("basic mutate", {
expect_dplyr_equal(
input %>%
select(int, chr) %>%
filter(int > 5) %>%
mutate(int = int + 6L) %>%
collect(),
tbl
)
})
test_that("mutate() with NULL inputs", {
expect_dplyr_equal(
input %>%
mutate(int = NULL) %>%
collect(),
tbl
)
})
test_that("empty mutate()", {
expect_dplyr_equal(
input %>%
mutate() %>%
collect(),
tbl
)
})
test_that("transmute", {
expect_dplyr_equal(
input %>%
select(int, chr) %>%
filter(int > 5) %>%
transmute(int = int + 6L) %>%
collect(),
tbl
)
})
test_that("transmute() with NULL inputs", {
expect_dplyr_equal(
input %>%
transmute(int = NULL) %>%
collect(),
tbl
)
})
test_that("empty transmute()", {
expect_dplyr_equal(
input %>%
transmute() %>%
collect(),
tbl
)
})
test_that("mutate and refer to previous mutants", {
expect_dplyr_equal(
input %>%
select(int, padded_strings) %>%
mutate(
line_lengths = nchar(padded_strings),
longer = line_lengths * 10
) %>%
filter(line_lengths > 15) %>%
collect(),
tbl
)
})
test_that("mutate with .data pronoun", {
expect_dplyr_equal(
input %>%
select(int, padded_strings) %>%
mutate(
line_lengths = nchar(padded_strings),
longer = .data$line_lengths * 10
) %>%
filter(line_lengths > 15) %>%
collect(),
tbl
)
})
test_that("mutate with unnamed expressions", {
expect_dplyr_equal(
input %>%
select(int, padded_strings) %>%
mutate(
int, # bare column name
nchar(padded_strings) # expression
) %>%
filter(int > 5) %>%
collect(),
tbl
)
})
test_that("mutate with reassigning same name", {
expect_dplyr_equal(
input %>%
transmute(
new = lgl,
new = chr
) %>%
collect(),
tbl
)
})
test_that("mutate with single value for recycling", {
skip("Not implemented (ARROW-11705")
expect_dplyr_equal(
input %>%
select(int, padded_strings) %>%
mutate(
dr_bronner = 1 # ALL ONE!
) %>%
collect(),
tbl
)
})
test_that("dplyr::mutate's examples", {
# Newly created variables are available immediately
expect_dplyr_equal(
input %>%
select(name, mass) %>%
mutate(
mass2 = mass * 2,
mass2_squared = mass2 * mass2
) %>%
collect(),
starwars # this is a test tibble that ships with dplyr
)
# As well as adding new variables, you can use mutate() to
# remove variables and modify existing variables.
expect_dplyr_equal(
input %>%
select(name, height, mass, homeworld) %>%
mutate(
mass = NULL,
height = height * 0.0328084 # convert to feet
) %>%
collect(),
starwars
)
# Examples we don't support should succeed
# but warn that they're pulling data into R to do so
# across + autosplicing: ARROW-11699
expect_warning(
expect_dplyr_equal(
input %>%
select(name, homeworld, species) %>%
mutate(across(!name, as.factor)) %>%
collect(),
starwars
),
"Expression across.*not supported in Arrow"
)
# group_by then mutate
expect_warning(
expect_dplyr_equal(
input %>%
select(name, mass, homeworld) %>%
group_by(homeworld) %>%
mutate(rank = min_rank(desc(mass))) %>%
collect(),
starwars
),
"not supported in Arrow"
)
# `.before` and `.after` experimental args: ARROW-11701
df <- tibble(x = 1, y = 2)
expect_dplyr_equal(
input %>% mutate(z = x + y) %>% collect(),
df
)
#> # A tibble: 1 x 3
#> x y z
#> <dbl> <dbl> <dbl>
#> 1 1 2 3
expect_warning(
expect_dplyr_equal(
input %>% mutate(z = x + y, .before = 1) %>% collect(),
df
),
"not supported in Arrow"
)
#> # A tibble: 1 x 3
#> z x y
#> <dbl> <dbl> <dbl>
#> 1 3 1 2
expect_warning(
expect_dplyr_equal(
input %>% mutate(z = x + y, .after = x) %>% collect(),
df
),
"not supported in Arrow"
)
#> # A tibble: 1 x 3
#> x z y
#> <dbl> <dbl> <dbl>
#> 1 1 3 2
# By default, mutate() keeps all columns from the input data.
# Experimental: You can override with `.keep`
df <- tibble(x = 1, y = 2, a = "a", b = "b")
expect_dplyr_equal(
input %>% mutate(z = x + y, .keep = "all") %>% collect(), # the default
df
)
#> # A tibble: 1 x 5
#> x y a b z
#> <dbl> <dbl> <chr> <chr> <dbl>
#> 1 1 2 a b 3
expect_dplyr_equal(
input %>% mutate(z = x + y, .keep = "used") %>% collect(),
df
)
#> # A tibble: 1 x 3
#> x y z
#> <dbl> <dbl> <dbl>
#> 1 1 2 3
expect_dplyr_equal(
input %>% mutate(z = x + y, .keep = "unused") %>% collect(),
df
)
#> # A tibble: 1 x 3
#> a b z
#> <chr> <chr> <dbl>
#> 1 a b 3
expect_dplyr_equal(
input %>% mutate(z = x + y, .keep = "none") %>% collect(), # same as transmute()
df
)
#> # A tibble: 1 x 1
#> z
#> <dbl>
#> 1 3
# Grouping ----------------------------------------
# The mutate operation may yield different results on grouped
# tibbles because the expressions are computed within groups.
# The following normalises `mass` by the global average:
# TODO(ARROW-11702)
expect_warning(
expect_dplyr_equal(
input %>%
select(name, mass, species) %>%
mutate(mass_norm = mass / mean(mass, na.rm = TRUE)) %>%
collect(),
starwars
),
"not supported in Arrow"
)
})
test_that("handle bad expressions", {
# TODO: search for functions other than mean() (see above test)
# that need to be forced to fail because they error ambiguously
with_language("fr", {
# expect_warning(., NA) because the usual behavior when it hits a filter
# that it can't evaluate is to raise a warning, collect() to R, and retry
# the filter. But we want this to error the first time because it's
# a user error, not solvable by retrying in R
expect_warning(
expect_error(
Table$create(tbl) %>% mutate(newvar = NOTAVAR + 2),
"objet 'NOTAVAR' introuvable"
),
NA
)
})
})
test_that("print a mutated table", {
expect_output(
Table$create(tbl) %>%
select(int) %>%
mutate(twice = int * 2) %>%
print(),
'Table (query)
int: int32
twice: expr
See $.data for the source Arrow object',
fixed = TRUE)
# Handling non-expressions/edge cases
expect_output(
Table$create(tbl) %>%
select(int) %>%
mutate(again = 1:10) %>%
print(),
'Table (query)
int: int32
again: expr
See $.data for the source Arrow object',
fixed = TRUE)
})
test_that("mutate and write_dataset", {
skip_if_not_available("dataset")
# See related test in test-dataset.R
skip_on_os("windows") # https://issues.apache.org/jira/browse/ARROW-9651
first_date <- lubridate::ymd_hms("2015-04-29 03:12:39")
df1 <- tibble(
int = 1:10,
dbl = as.numeric(1:10),
lgl = rep(c(TRUE, FALSE, NA, TRUE, FALSE), 2),
chr = letters[1:10],
fct = factor(LETTERS[1:10]),
ts = first_date + lubridate::days(1:10)
)
second_date <- lubridate::ymd_hms("2017-03-09 07:01:02")
df2 <- tibble(
int = 101:110,
dbl = c(as.numeric(51:59), NaN),
lgl = rep(c(TRUE, FALSE, NA, TRUE, FALSE), 2),
chr = letters[10:1],
fct = factor(LETTERS[10:1]),
ts = second_date + lubridate::days(10:1)
)
dst_dir <- tempfile()
stacked <- record_batch(rbind(df1, df2))
stacked %>%
mutate(twice = int * 2) %>%
group_by(int) %>%
write_dataset(dst_dir, format = "feather")
expect_true(dir.exists(dst_dir))
expect_identical(dir(dst_dir), sort(paste("int", c(1:10, 101:110), sep = "=")))
new_ds <- open_dataset(dst_dir, format = "feather")
expect_equivalent(
new_ds %>%
select(string = chr, integer = int, twice) %>%
filter(integer > 6 & integer < 11) %>%
collect() %>%
summarize(mean = mean(integer)),
df1 %>%
select(string = chr, integer = int) %>%
mutate(twice = integer * 2) %>%
filter(integer > 6) %>%
summarize(mean = mean(integer))
)
})