Skip to content

Commit c16a8b2

Browse files
authored
GH-40640: [R] to_arrow() loses group_by() (#49713)
### Rationale for this change Roundtrip to duckdb loses grouping ### What changes are included in this PR? Reapply grouping ### Are these changes tested? Yup ### Are there any user-facing changes? Nah ### AI usage Done with Codex, but I went through it myself and am happy with it. * GitHub Issue: #40640 Authored-by: Nic Crane <thisisnic@gmail.com> Signed-off-by: Nic Crane <thisisnic@gmail.com>
1 parent 66306ae commit c16a8b2

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

r/R/duckdb.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,17 @@ to_arrow <- function(.data) {
183183
)
184184
}
185185

186+
groups <- dplyr::groups(.data)
187+
186188
# Run the query
187189
res <- DBI::dbSendQuery(dbplyr::remote_con(.data), dbplyr::remote_query(.data), arrow = TRUE)
188190

189191
reader <- duckdb::duckdb_fetch_record_batch(res)
190-
MakeSafeRecordBatchReader(reader)
192+
out <- MakeSafeRecordBatchReader(reader)
193+
194+
if (length(groups)) {
195+
out <- dplyr::group_by(out, !!!groups)
196+
}
197+
198+
out
191199
}

r/tests/testthat/test-duckdb.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ test_that("to_arrow roundtrip, with dataset (without wrapping)", {
190190
expect_r6_class(out, "RecordBatchReader")
191191
})
192192

193+
test_that("to_arrow preserves grouping from duckdb tables", {
194+
ds <- InMemoryDataset$create(example_data)
195+
196+
out <- ds |>
197+
to_duckdb() |>
198+
group_by(lgl) |>
199+
to_arrow()
200+
201+
expect_s3_class(out, "arrow_dplyr_query")
202+
expect_equal(dplyr::group_vars(out), "lgl")
203+
})
204+
193205
# The next set of tests use an already-extant connection to test features of
194206
# persistence and querying against the table without using the `tbl` itself, so
195207
# we need to create a connection separate from the ephemeral one that is made

0 commit comments

Comments
 (0)