Skip to content

Commit

Permalink
between support missing bounds for character input, closes #3667
Browse files Browse the repository at this point in the history
  • Loading branch information
jangorecki committed Jul 30, 2019
1 parent 2986736 commit 1142205
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@
24. `column not found` could incorrectly occur in rare non-equi-join cases, [#3635](https://github.com/Rdatatable/data.table/issues/3635). Thanks to @UweBlock for the report.
25. Function `between` and operator `%between%` now handles missing bounds for character input as documented, [#3667](https://github.com/Rdatatable/data.table/issues/3667). Thanks to @AnonymousBoba for the report.
#### NOTES
1. `rbindlist`'s `use.names="check"` now emits its message for automatic column names (`"V[0-9]+"`) too, [#3484](https://github.com/Rdatatable/data.table/pull/3484). See news item 5 of v1.12.2 below.
Expand Down
19 changes: 17 additions & 2 deletions R/between.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,23 @@ between = function(x,lower,upper,incbounds=TRUE) {
} else {
if (isTRUE(getOption("datatable.verbose"))) cat("optimised between not available for this data type, fallback to slow R routine\n")
# now just for character input. TODO: support character between in Cbetween and remove this branch
if (incbounds) x>=lower & x<=upper
else x>lower & x<upper
# support NAs as missing bounds also for character #3667
lower_na = is.na(lower)
upper_na = is.na(upper)
if (lower_na || upper_na) {
if (lower_na && upper_na) {
rep(TRUE, length(x))
} else if (lower_na) {
if (incbounds) x<=upper else x<upper
} else if (upper_na) {
if (incbounds) x>=lower else x>lower
} else {
stop("internal error in between, all cases of lower/upper NAs for character should be handled already, please report") # nocov
}
} else {
if (incbounds) x>=lower & x<=upper
else x>lower & x<upper
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -15428,6 +15428,16 @@ test(2071.10, dcast(data.table(a=1, b=1, l=list(list(1))), a ~ b, value.var='l')
test(2071.11, dcast(data.table(a = 1, b = 2, c = 3), a ~ b, value.var = 'c', fill = '2'),
data.table(a=1, `2`=3, key='a'))

# between NAs in character bounds #3667
test(2072.01, letters %between% c("m", NA_character_), c(rep(FALSE, 12L), rep(TRUE, 14L)))
test(2072.02, between(letters, "m", NA_character_, incbounds=TRUE), c(rep(FALSE, 12L), rep(TRUE, 14L)))
test(2072.03, between(letters, "m", NA_character_, incbounds=FALSE), c(rep(FALSE, 13L), rep(TRUE, 13L)))
test(2072.04, between(letters, NA_character_, "m", incbounds=TRUE), c(rep(TRUE, 13L), rep(FALSE, 13L)))
test(2072.05, between(letters, NA_character_, "m", incbounds=FALSE), c(rep(TRUE, 12L), rep(FALSE, 14L)))
test(2072.06, between(letters, NA_character_, NA_character_, incbounds=TRUE), rep(TRUE, 26L))
test(2072.07, between(letters, NA_character_, NA_character_, incbounds=FALSE), rep(TRUE, 26L))


###################################
# Add new tests above this line #
###################################
Expand Down

0 comments on commit 1142205

Please sign in to comment.