Skip to content

Commit 0a79533

Browse files
viiryashivaram
authored andcommitted
[SPARK-8807] [SPARKR] Add between operator in SparkR
JIRA: https://issues.apache.org/jira/browse/SPARK-8807 Add between operator in SparkR. Author: Liang-Chi Hsieh <[email protected]> Closes apache#7356 from viirya/add_r_between and squashes the following commits: 7f51b44 [Liang-Chi Hsieh] Add test for non-numeric column. c6a25c5 [Liang-Chi Hsieh] Add between function.
1 parent e272123 commit 0a79533

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,18 @@ test_that("column functions", {
638638
c7 <- floor(c) + log(c) + log10(c) + log1p(c) + rint(c)
639639
c8 <- sign(c) + sin(c) + sinh(c) + tan(c) + tanh(c)
640640
c9 <- toDegrees(c) + toRadians(c)
641+
642+
df <- jsonFile(sqlContext, jsonPath)
643+
df2 <- select(df, between(df$age, c(20, 30)), between(df$age, c(10, 20)))
644+
expect_equal(collect(df2)[[2, 1]], TRUE)
645+
expect_equal(collect(df2)[[2, 2]], FALSE)
646+
expect_equal(collect(df2)[[3, 1]], FALSE)
647+
expect_equal(collect(df2)[[3, 2]], TRUE)
648+
649+
df3 <- select(df, between(df$name, c("Apache", "Spark")))
650+
expect_equal(collect(df3)[[1, 1]], TRUE)
651+
expect_equal(collect(df3)[[2, 1]], FALSE)
652+
expect_equal(collect(df3)[[3, 1]], TRUE)
641653
})
642654

643655
test_that("column binary mathfunctions", {

0 commit comments

Comments
 (0)