Skip to content

Commit

Permalink
Merge pull request apache#198 from cafreeman/sparkr-sql
Browse files Browse the repository at this point in the history
`selectExpr`
  • Loading branch information
shivaram committed Mar 4, 2015
2 parents 198c130 + 8b9a963 commit 6a1fe64
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ exportMethods("columns",
"schema",
"sortDF",
"select",
"selectExpr",
"toRDD",
"where")

Expand Down
30 changes: 30 additions & 0 deletions pkg/R/DataFrame.R
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,36 @@ setMethod("select", signature(x = "DataFrame", col = "Column"),
dataFrame(sdf)
})

#' SelectExpr
#'
#' Select from a DataFrame using a set of SQL expressions.
#'
#' @param x A DataFrame to be selected from.
#' @param expr A string containing a SQL expression
#' @param ... Additional expressions
#' @return A DataFrame
#' @rdname selectExpr
#' @export
#' @examples
#'\dontrun{
#' sc <- sparkR.init()
#' sqlCtx <- sparkRSQL.init(sc)
#' path <- "path/to/file.json"
#' df <- jsonFile(sqlCtx, path)
#' selectExpr(df, "col1", "(col2 * 5) as newCol")
#' }
setGeneric("selectExpr", function(x, expr, ...) { standardGeneric("selectExpr") })

#' @rdname selectExpr
#' @export
setMethod("selectExpr",
signature(x = "DataFrame", expr = "character"),
function(x, expr, ...) {
exprList <- list(expr, ...)
sdf <- callJMethod(x@sdf, "selectExpr", listToSeq(exprList))
dataFrame(sdf)
})

#' SortDF
#'
#' Sort a DataFrame by the specified column(s).
Expand Down
11 changes: 11 additions & 0 deletions pkg/inst/tests/test_sparkSQL.R
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@ test_that("select with column", {
expect_true(count(df2) == 3)
})

test_that("selectExpr() on a DataFrame", {
df <- jsonFile(sqlCtx, jsonPath)
selected <- selectExpr(df, "age * 2")
expect_true(names(selected) == "(age * 2)")
expect_equal(collect(selected), collect(select(df, df$age * 2L)))

selected2 <- selectExpr(df, "name as newName", "abs(age) as age")
expect_equal(names(selected2), c("newName", "age"))
expect_true(count(selected2) == 3)
})

test_that("column calculation", {
df <- jsonFile(sqlCtx, jsonPath)
d <- collect(select(df, alias(df$age + 1, "age2")))
Expand Down

0 comments on commit 6a1fe64

Please sign in to comment.