Skip to content

Commit

Permalink
fix: grepl(x = NA) gives correct results
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Nov 16, 2023
1 parent a242a29 commit f7a9358
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/relational-duckdb.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ duckplyr_macros <- c(
# https://github.com/duckdb/duckdb/discussions/8599
# "as.Date" = '(x) AS strptime(x, \'%Y-%m-%d\')',

"grepl" = "(pattern, x) AS regexp_matches(x, pattern)",
"grepl" = "(pattern, x) AS (CASE WHEN x IS NULL THEN FALSE ELSE regexp_matches(x, pattern) END)",
"as.integer" = "(x) AS CAST(x AS int32)",
"if_else" = "(test, yes, no) AS (CASE WHEN test THEN yes ELSE no END)",
"|" = "(x, y) AS (x OR y)",
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-as_duckplyr_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,19 @@ test_that("as_duckplyr_df() and mutate(c = 0, d = -1, e = log(c), f = log10(d))"
expect_equal(pre, post)
})


test_that("as_duckplyr_df() and mutate(c = NA_character_, d = grepl('.', c))", {
# Data
test_df <- data.frame(a = 1:6 + 0, b = 2, g = rep(1:3, 1:3))

# Run
pre <- test_df %>% as_duckplyr_df() %>% mutate(c = NA_character_, d = grepl('.', c))
post <- test_df %>% mutate(c = NA_character_, d = grepl('.', c)) %>% as_duckplyr_df()

# Compare
expect_equal(pre, post)
})

test_that("as_duckplyr_df() and nest_by()", {
withr::local_envvar(DUCKPLYR_FORCE = "FALSE")

Expand Down
3 changes: 3 additions & 0 deletions tools/00-funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,9 @@ test_extra_arg_map <- list(
"c = 0, d = -1, e = log(c), f = log(d)",
"c = 0, d = -1, e = log(c), f = log10(d)",

# grepl with NA
"c = NA_character_, d = grepl('.', c)",

NULL
),
nest_join = "join_by(a)",
Expand Down

0 comments on commit f7a9358

Please sign in to comment.