From ef70cee9a64c0d2e274cf228e27723083dbd691e Mon Sep 17 00:00:00 2001 From: actuaryzhang Date: Tue, 13 Jun 2017 10:00:27 -0700 Subject: [PATCH 1/2] add trunc function to SparkR SQL --- R/pkg/NAMESPACE | 1 + R/pkg/R/functions.R | 28 +++++++++++++++++++++++++++ R/pkg/tests/fulltests/test_sparkSQL.R | 2 ++ 3 files changed, 31 insertions(+) diff --git a/R/pkg/NAMESPACE b/R/pkg/NAMESPACE index 4e3fe00a2e9b..229de4a997ee 100644 --- a/R/pkg/NAMESPACE +++ b/R/pkg/NAMESPACE @@ -357,6 +357,7 @@ exportMethods("%<=>%", "to_utc_timestamp", "translate", "trim", + "trunc", "unbase64", "unhex", "unix_timestamp", diff --git a/R/pkg/R/functions.R b/R/pkg/R/functions.R index 06a90192bb12..a6adcf4e9e59 100644 --- a/R/pkg/R/functions.R +++ b/R/pkg/R/functions.R @@ -4015,3 +4015,31 @@ setMethod("input_file_name", signature("missing"), jc <- callJStatic("org.apache.spark.sql.functions", "input_file_name") column(jc) }) + +#' trunc +#' +#' Returns date truncated to the unit specified by the format. +#' +#' @param x Column to compute on. +#' @param format string used for specify the truncation method. For example, "year", "yyyy", +#' "yy" for truncate by year, or "month", "mon", "mm" for truncate by month. +#' +#' @rdname trunc +#' @name trunc +#' @family date time functions +#' @aliases trunc,Column-method +#' @export +#' @examples +#' \dontrun{ +#' trunc(df$c, "year") +#' trunc(df$c, "yy") +#' trunc(df$c, "month") +#' trunc(df$c, "mon") +#' } +#' @note trunc since 2.3.0 +setMethod("trunc", + signature(x = "Column"), + function(x, format) { + jc <- callJStatic("org.apache.spark.sql.functions", "trunc", x@jc, format) + column(jc) + }) diff --git a/R/pkg/tests/fulltests/test_sparkSQL.R b/R/pkg/tests/fulltests/test_sparkSQL.R index af529067f43e..911b73b9ee55 100644 --- a/R/pkg/tests/fulltests/test_sparkSQL.R +++ b/R/pkg/tests/fulltests/test_sparkSQL.R @@ -1382,6 +1382,8 @@ test_that("column functions", { c20 <- to_timestamp(c) + to_timestamp(c, "yyyy") + to_date(c, "yyyy") c21 <- posexplode_outer(c) + explode_outer(c) c22 <- not(c) + c23 <- trunc(c, "year") + trunc(c, "yyyy") + trunc(c, "yy") + + trunc(c, "month") + trunc(c, "mon") + trunc(c, "mm") # Test if base::is.nan() is exposed expect_equal(is.nan(c("a", "b")), c(FALSE, FALSE)) From 797c3d0e77c7c91d499a55036e43c7ebfd6e9ced Mon Sep 17 00:00:00 2001 From: actuaryzhang Date: Thu, 15 Jun 2017 10:56:59 -0700 Subject: [PATCH 2/2] force format to be character --- R/pkg/R/functions.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/pkg/R/functions.R b/R/pkg/R/functions.R index a6adcf4e9e59..7128c3b9adff 100644 --- a/R/pkg/R/functions.R +++ b/R/pkg/R/functions.R @@ -4040,6 +4040,7 @@ setMethod("input_file_name", signature("missing"), setMethod("trunc", signature(x = "Column"), function(x, format) { - jc <- callJStatic("org.apache.spark.sql.functions", "trunc", x@jc, format) + jc <- callJStatic("org.apache.spark.sql.functions", "trunc", + x@jc, as.character(format)) column(jc) })