Skip to content

Commit

Permalink
Merge pull request #156 from duckdblabs/b-131-head-neg
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed May 5, 2024
2 parents 7968c11 + cddc4ba commit 0c69f17
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
16 changes: 12 additions & 4 deletions R/head.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
head.duckplyr_df <- function(x, n = 6L, ...) {
stopifnot(is_integerish(n))

rel <- duckdb_rel_from_df(x)
out_rel <- rel_limit(rel, n)
out <- rel_to_df(out_rel)
dplyr_reconstruct(out, x)
rel_try(call = list(name = "head", x = x, args = list(n = n)),
"Can't process negative n" = (n < 0),
{
rel <- duckdb_rel_from_df(x)
out_rel <- rel_limit(rel, n)
out <- rel_to_df(out_rel)
out <- dplyr_reconstruct(out, x)
return(out)
}
)

NextMethod()
}
32 changes: 32 additions & 0 deletions tests/testthat/test-head.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
test_that("head(2)", {
withr::local_envvar(DUCKPLYR_FORCE = TRUE)

out <-
data.frame(a = 1:5) %>%
duckplyr::as_duckplyr_df() %>%
head(2)

expect_identical(out$a, 1:2)
})

test_that("head(-2)", {
withr::local_envvar(DUCKPLYR_FORCE = FALSE)

out <-
data.frame(a = 1:5) %>%
duckplyr::as_duckplyr_df() %>%
head(-2)

expect_identical(out$a, 1:3)
})

test_that("head(0)", {
withr::local_envvar(DUCKPLYR_FORCE = TRUE)

out <-
data.frame(a = 1:5) %>%
duckplyr::as_duckplyr_df() %>%
head(0)

expect_identical(out$a, integer(0))
})

0 comments on commit 0c69f17

Please sign in to comment.