Skip to content

Commit c6a25c5

Browse files
committed
Add between function.
1 parent aba5784 commit c6a25c5

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

R/pkg/NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ exportMethods("abs",
7777
"atan",
7878
"atan2",
7979
"avg",
80+
"between",
8081
"cast",
8182
"cbrt",
8283
"ceiling",

R/pkg/R/column.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,23 @@ setMethod("substr", signature(x = "Column"),
187187
column(jc)
188188
})
189189

190+
#' between
191+
#'
192+
#' Test if the column is between the lower bound and upper bound, inclusive.
193+
#'
194+
#' @rdname column
195+
#'
196+
#' @param bounds lower and upper bounds
197+
setMethod("between", signature(x = "Column"),
198+
function(x, bounds) {
199+
if (is.vector(bounds) && length(bounds) == 2) {
200+
jc <- callJMethod(x@jc, "between", bounds[1], bounds[2])
201+
column(jc)
202+
} else {
203+
stop("bounds should be a vector of lower and upper bounds")
204+
}
205+
})
206+
190207
#' Casts the column to a different data type.
191208
#'
192209
#' @rdname column

R/pkg/R/generics.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ setGeneric("asc", function(x) { standardGeneric("asc") })
567567
#' @export
568568
setGeneric("avg", function(x, ...) { standardGeneric("avg") })
569569

570+
#' @rdname column
571+
#' @export
572+
setGeneric("between", function(x, bounds) { standardGeneric("between") })
573+
570574
#' @rdname column
571575
#' @export
572576
setGeneric("cast", function(x, dataType) { standardGeneric("cast") })

R/pkg/inst/tests/test_sparkSQL.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,13 @@ test_that("column functions", {
612612
c7 <- floor(c) + log(c) + log10(c) + log1p(c) + rint(c)
613613
c8 <- sign(c) + sin(c) + sinh(c) + tan(c) + tanh(c)
614614
c9 <- toDegrees(c) + toRadians(c)
615+
616+
df <- jsonFile(sqlContext, jsonPath)
617+
df2 <- select(df, between(df$age, c(20, 30)), between(df$age, c(10, 20)))
618+
expect_equal(collect(df2)[[2, 1]], TRUE)
619+
expect_equal(collect(df2)[[2, 2]], FALSE)
620+
expect_equal(collect(df2)[[3, 1]], FALSE)
621+
expect_equal(collect(df2)[[3, 2]], TRUE)
615622
})
616623

617624
test_that("column binary mathfunctions", {

0 commit comments

Comments
 (0)