This is an automated email from the ASF dual-hosted git repository.
thisisnic pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new 817e3f68ea GH-40303: [R]: Unnamed columns cause issues when used in
dplyr queries (#51313)
817e3f68ea is described below
commit 817e3f68eaaabfff21744b3929116eed27244ff0
Author: Nic Crane <[email protected]>
AuthorDate: Wed Sep 16 15:50:45 2026 -0500
GH-40303: [R]: Unnamed columns cause issues when used in dplyr queries
(#51313)
### Rationale for this change
Errors with unnamed columns make it hard to tell why we have the error
### What changes are included in this PR?
Explicitly error with useful message
### Are these changes tested?
Yeah
### Are there any user-facing changes?
Sure
* GitHub Issue: #40303
Lead-authored-by: Nic Crane <[email protected]>
Co-authored-by: Copilot Autofix powered by AI
<[email protected]>
Signed-off-by: Nic Crane <[email protected]>
---
r/R/dplyr-eval.R | 9 +++++++++
r/tests/testthat/test-dplyr-eval.R | 26 ++++++++++++++++++++++++++
2 files changed, 35 insertions(+)
diff --git a/r/R/dplyr-eval.R b/r/R/dplyr-eval.R
index 1282f17187..5ed95c7646 100644
--- a/r/R/dplyr-eval.R
+++ b/r/R/dplyr-eval.R
@@ -252,6 +252,15 @@ abandon_ship <- function(err, env) {
arrow_mask <- function(.data) {
f_env <- new_environment(.cache$functions)
+ # Empty column names can't be bound into an environment (GH-40303).
+ # Like dplyr, refuse to transform such data rather than repairing names.
+ if (!all(nzchar(names(.data$selected_columns)))) {
+ abort(c(
+ "Can't transform data with empty (`\"\"`) column names.",
+ i = "Rename or drop the unnamed columns first, e.g. with `rename()` or
`select()`."
+ ))
+ }
+
# Assign the schema to the expressions
schema <- .data$.data$schema
walk(.data$selected_columns, ~ (.$schema <- schema))
diff --git a/r/tests/testthat/test-dplyr-eval.R
b/r/tests/testthat/test-dplyr-eval.R
index 0b0b9f98f4..78bd84eb6d 100644
--- a/r/tests/testthat/test-dplyr-eval.R
+++ b/r/tests/testthat/test-dplyr-eval.R
@@ -59,3 +59,29 @@ test_that("try_arrow_dplyr/abandon_ship adds the right
message about collect()",
expect_snapshot(tester(ds, i), error = TRUE)
}
})
+
+test_that("dplyr verbs error clearly on empty column names", {
+ skip_if_not_available("acero")
+ # GH-40303
+ tbl <- example_data
+ names(tbl)[1] <- ""
+
+ # dplyr also refuses these ("Can't transform a data frame with `NA` or
+ # `""` names."), but our wording differs since the input isn't a data frame
+ tab <- arrow_table(tbl)
+ msg <- "Can't transform data with empty"
+ expect_error(tab |> mutate(z = dbl + 1), msg)
+ expect_error(tab |> filter(dbl > 4), msg)
+ expect_error(tab |> arrange(dbl), msg)
+ expect_error(tab |> group_by(dbl), msg)
+
+ # select() and rename() still work as an escape hatch, as in dplyr
+ compare_dplyr_binding(
+ .input |> select(-1) |> mutate(z = dbl + 1) |> collect(),
+ tbl
+ )
+ compare_dplyr_binding(
+ .input |> rename(int = 1) |> mutate(z = dbl + 1) |> collect(),
+ tbl
+ )
+})