Skip to content

Commit d4f6335

Browse files
yu-iskwDavies Liu
authored andcommitted
[SPARK-8431] [SPARKR] Add in operator to DataFrame Column in SparkR
[[SPARK-8431] Add in operator to DataFrame Column in SparkR - ASF JIRA](https://issues.apache.org/jira/browse/SPARK-8431) Author: Yu ISHIKAWA <[email protected]> Closes #6941 from yu-iskw/SPARK-8431 and squashes the following commits: 1f64423 [Yu ISHIKAWA] Modify the comment f4309a7 [Yu ISHIKAWA] Make a `setMethod` for `%in%` be independent 6e37936 [Yu ISHIKAWA] Modify a variable name c196173 [Yu ISHIKAWA] [SPARK-8431][SparkR] Add in operator to DataFrame Column in SparkR
1 parent 164fe2a commit d4f6335

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

R/pkg/R/column.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,22 @@ setMethod("cast",
210210
}
211211
})
212212

213+
#' Match a column with given values.
214+
#'
215+
#' @rdname column
216+
#' @return a matched values as a result of comparing with given values.
217+
#' \dontrun{
218+
#' filter(df, "age in (10, 30)")
219+
#' where(df, df$age %in% c(10, 30))
220+
#' }
221+
setMethod("%in%",
222+
signature(x = "Column"),
223+
function(x, table) {
224+
table <- listToSeq(as.list(table))
225+
jc <- callJMethod(x@jc, "in", table)
226+
return(column(jc))
227+
})
228+
213229
#' Approx Count Distinct
214230
#'
215231
#' @rdname column

R/pkg/inst/tests/test_sparkSQL.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,16 @@ test_that("filter() on a DataFrame", {
693693
filtered2 <- where(df, df$name != "Michael")
694694
expect_true(count(filtered2) == 2)
695695
expect_true(collect(filtered2)$age[2] == 19)
696+
697+
# test suites for %in%
698+
filtered3 <- filter(df, "age in (19)")
699+
expect_equal(count(filtered3), 1)
700+
filtered4 <- filter(df, "age in (19, 30)")
701+
expect_equal(count(filtered4), 2)
702+
filtered5 <- where(df, df$age %in% c(19))
703+
expect_equal(count(filtered5), 1)
704+
filtered6 <- where(df, df$age %in% c(19, 30))
705+
expect_equal(count(filtered6), 2)
696706
})
697707

698708
test_that("join() on a DataFrame", {

0 commit comments

Comments
 (0)