Skip to content

Commit eb01350

Browse files
authored
ARROW-18131: [R] Correctly handle .data pronoun in group_by() (#14484)
Authored-by: Nic Crane <thisisnic@gmail.com> Signed-off-by: Nic Crane <thisisnic@gmail.com>
1 parent 33f2c0e commit eb01350

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

r/R/dplyr-group-by.R

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,20 @@ group_by.arrow_dplyr_query <- function(.data,
3737
expression_list <- expand_across(.data, quos(...))
3838
new_groups <- ensure_named_exprs(expression_list)
3939

40+
# set up group names and check which are new
41+
gbp <- dplyr::group_by_prepare(.data, !!!expression_list, .add = .add)
42+
existing_groups <- dplyr::group_vars(gbp$data)
43+
new_group_names <- setdiff(gbp$group_names, existing_groups)
44+
45+
names(new_groups) <- new_group_names
46+
4047
if (length(new_groups)) {
4148
# Add them to the data
4249
.data <- dplyr::mutate(.data, !!!new_groups)
4350
}
4451

45-
if (.add) {
46-
gv <- union(dplyr::group_vars(.data), names(new_groups))
47-
} else {
48-
gv <- names(new_groups)
49-
}
50-
51-
.data$group_by_vars <- gv %||% character()
52-
.data$drop_empty_groups <- ifelse(length(gv), .drop, dplyr::group_by_drop_default(.data))
52+
.data$group_by_vars <- gbp$group_names
53+
.data$drop_empty_groups <- ifelse(length(gbp$group_names), .drop, dplyr::group_by_drop_default(.data))
5354
.data
5455
}
5556
group_by.Dataset <- group_by.ArrowTabular <- group_by.RecordBatchReader <- group_by.arrow_dplyr_query

r/tests/testthat/test-dplyr-group-by.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,12 @@ test_that("Can use across() within group_by()", {
305305
tbl
306306
)
307307
})
308+
309+
test_that("ARROW-18131 - correctly handles .data pronoun in group_by()", {
310+
compare_dplyr_binding(
311+
.input %>%
312+
group_by(.data$lgl) %>%
313+
collect(),
314+
tbl
315+
)
316+
})

0 commit comments

Comments
 (0)