Skip to content

Commit

Permalink
Resist against renaming of columns
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Aug 5, 2023
1 parent 6a3fcab commit 681a9c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion R/relational-duckdb.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ duckdb_rel_from_df <- function(df) {
tryCatch(
{
# duckdb:::df_is_materialized() is broken and unneeded
return(duckdb:::rel_from_altrep_df(df))
rel <- duckdb:::rel_from_altrep_df(df)
rel_names <- duckdb:::rapi_rel_names(rel)
if (!identical(rel_names, names(df))) {
# This can happen when column names change for an existing relational data frame
exprs <- nexprs_from_loc(rel_names, set_names(seq_along(df), names(df)))
rel <- rel_project.duckdb_relation(rel, exprs)
}
return(rel)
},
error = function(e) {}
)
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-relational-duckdb.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ test_that("duckdb_rel_from_df()", {

expect_silent(duckdb_rel_from_df(df))

# If this is no longer an eror, we need to make sure that subsetting
# forwards to the vector class
expect_snapshot(error = TRUE, {
data.frame(a = vctrs::new_vctr(1:3)) %>%
duckdb_rel_from_df()
})
})

test_that("duckdb_rel_from_df() and changing column names", {
withr::local_envvar(DUCKPLYR_FORCE = TRUE)

x <-
data.frame(a = 1) %>%
duckplyr_select(a)

names(x) <- "b"
expect_equal(x %>% duckplyr_filter(b == 1), data.frame(b = 1))
})

test_that("rel_aggregate()", {
expr_species <- relexpr_reference("species")
expr_aggregate <- relexpr_function(alias = "mean_bill_length_mm", "avg", list(
Expand Down

0 comments on commit 681a9c5

Please sign in to comment.