Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions R/pkg/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ exportMethods("abs",
"contains",
"cos",
"cosh",
"concat",
"countDistinct",
"desc",
"endsWith",
Expand All @@ -106,10 +107,13 @@ exportMethods("abs",
"floor",
"getField",
"getItem",
"greatest",
"hypot",
"isNotNull",
"isNull",
"lit",
"last",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lit needs to be added to this list

"least",
"like",
"log",
"log10",
Expand Down
42 changes: 42 additions & 0 deletions R/pkg/R/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ createFunctions <- function() {

createFunctions()

#' @rdname functions
#' @return Creates a Column class of literal value.
setMethod("lit", signature("ANY"),
function(x) {
jc <- callJStatic("org.apache.spark.sql.functions", "lit", ifelse(class(x) == "Column", x@jc, x))
column(jc)
})

#' Approx Count Distinct
#'
#' @rdname functions
Expand All @@ -93,6 +101,40 @@ setMethod("countDistinct",
column(jc)
})

#' @rdname functions
#' @return Concatenates multiple input string columns together into a single string column.
setMethod("concat",
signature(x = "Column"),
function(x, ...) {
jcols <- lapply(list(x, ...), function(x) { x@jc })
jc <- callJStatic("org.apache.spark.sql.functions", "concat", listToSeq(jcols))
column(jc)
})

#' @rdname functions
#' @return Returns the greatest value of the list of column names, skipping null values.
#' This function takes at least 2 parameters. It will return null if all parameters are null.
setMethod("greatest",
signature(x = "Column"),
function(x, ...) {
stopifnot(length(list(...)) > 0)
jcols <- lapply(list(x, ...), function(x) { x@jc })
jc <- callJStatic("org.apache.spark.sql.functions", "greatest", listToSeq(jcols))
column(jc)
})

#' @rdname functions
#' @return Returns the least value of the list of column names, skipping null values.
#' This function takes at least 2 parameters. It will return null iff all parameters are null.
setMethod("least",
signature(x = "Column"),
function(x, ...) {
stopifnot(length(list(...)) > 0)
jcols <- lapply(list(x, ...), function(x) { x@jc })
jc <- callJStatic("org.apache.spark.sql.functions", "least", listToSeq(jcols))
column(jc)
})

#' @rdname functions
#' @aliases ceil
setMethod("ceiling",
Expand Down
16 changes: 16 additions & 0 deletions R/pkg/R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,10 @@ setGeneric("cbrt", function(x) { standardGeneric("cbrt") })
#' @export
setGeneric("ceil", function(x) { standardGeneric("ceil") })

#' @rdname functions
#' @export
setGeneric("concat", function(x, ...) { standardGeneric("concat") })

#' @rdname functions
#' @export
setGeneric("crc32", function(x) { standardGeneric("crc32") })
Expand All @@ -702,6 +706,10 @@ setGeneric("dayofyear", function(x) { standardGeneric("dayofyear") })
#' @export
setGeneric("explode", function(x) { standardGeneric("explode") })

#' @rdname functions
#' @export
setGeneric("greatest", function(x, ...) { standardGeneric("greatest") })

#' @rdname functions
#' @export
setGeneric("hex", function(x) { standardGeneric("hex") })
Expand All @@ -722,10 +730,18 @@ setGeneric("isNaN", function(x) { standardGeneric("isNaN") })
#' @export
setGeneric("last_day", function(x) { standardGeneric("last_day") })

#' @rdname functions
#' @export
setGeneric("least", function(x, ...) { standardGeneric("least") })

#' @rdname functions
#' @export
setGeneric("levenshtein", function(y, x) { standardGeneric("levenshtein") })

#' @rdname functions
#' @export
setGeneric("lit", function(x) { standardGeneric("lit") })

#' @rdname functions
#' @export
setGeneric("lower", function(x) { standardGeneric("lower") })
Expand Down
13 changes: 13 additions & 0 deletions R/pkg/inst/tests/test_sparkSQL.R
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,11 @@ test_that("select with column", {
df2 <- select(df, df$age)
expect_equal(columns(df2), c("age"))
expect_equal(count(df2), 3)

df3 <- select(df, lit("x"))
expect_equal(columns(df3), c("x"))
expect_equal(count(df3), 3)
expect_equal(collect(select(df3, "x"))[[1, 1]], "x")
})

test_that("selectExpr() on a DataFrame", {
Expand Down Expand Up @@ -692,6 +697,14 @@ test_that("string operators", {
expect_equal(count(where(df, startsWith(df$name, "A"))), 1)
expect_equal(first(select(df, substr(df$name, 1, 2)))[[1]], "Mi")
expect_equal(collect(select(df, cast(df$age, "string")))[[2, 1]], "30")
expect_equal(collect(select(df, concat(df$name, lit(":"), df$age)))[[2, 1]], "Andy:30")
})

test_that("greatest() and least() on a DataFrame", {
l <- list(list(a = 1, b = 2), list(a = 3, b = 4))
df <- createDataFrame(sqlContext, l)
expect_equal(collect(select(df, greatest(df$a, df$b)))[, 1], c(2, 4))
expect_equal(collect(select(df, least(df$a, df$b)))[, 1], c(1, 3))
})

test_that("group by", {
Expand Down