Skip to content

Commit 1c3e956

Browse files
committed
Adds date_trunc in R API
1 parent 9962390 commit 1c3e956

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

R/pkg/NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ exportMethods("%<=>%",
230230
"date_add",
231231
"date_format",
232232
"date_sub",
233+
"date_trunc",
233234
"datediff",
234235
"dayofmonth",
235236
"dayofweek",

R/pkg/R/functions.R

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ NULL
4141
#' @param x Column to compute on. In \code{window}, it must be a time Column of
4242
#' \code{TimestampType}.
4343
#' @param format For \code{to_date} and \code{to_timestamp}, it is the string to use to parse
44-
#' Column \code{x} to DateType or TimestampType. For \code{trunc}, it is the string
45-
#' to use to specify the truncation method. For example, "year", "yyyy", "yy" for
46-
#' truncate by year, or "month", "mon", "mm" for truncate by month.
44+
#' Column \code{x} to DateType or TimestampType.
45+
#'
46+
#' For \code{trunc}, it is the string to use to specify the truncation method.
47+
#' For example, "year", "yyyy", "yy" for truncate by year, or "month", "mon",
48+
#' "mm" for truncate by month.
49+
#'
50+
#' For \code{date_trunc}, it is similar with \code{trunc}'s but additionally
51+
#' supports "day", "dd", "second", "minute", "hour", "week" and "quarter".
52+
#'
4753
#' @param ... additional argument(s).
4854
#' @name column_datetime_functions
4955
#' @rdname column_datetime_functions
@@ -3478,3 +3484,22 @@ setMethod("trunc",
34783484
x@jc, as.character(format))
34793485
column(jc)
34803486
})
3487+
3488+
#' @details
3489+
#' \code{date_trunc}: Returns timestamp truncated to the unit specified by the format.
3490+
#'
3491+
#' @rdname column_datetime_functions
3492+
#' @aliases date_trunc date_trunc,character,Column-method
3493+
#' @export
3494+
#' @examples
3495+
#'
3496+
#' \dontrun{
3497+
#' head(select(df, df$time, date_trunc("hour", df$time), date_trunc("minute", df$time),
3498+
#' date_trunc("week", df$time), date_trunc("quarter", df$time)))}
3499+
#' @note date_trunc since 2.3.0
3500+
setMethod("date_trunc",
3501+
signature(format = "character", x = "Column"),
3502+
function(format, x) {
3503+
jc <- callJStatic("org.apache.spark.sql.functions", "date_trunc", format, x@jc)
3504+
column(jc)
3505+
})

R/pkg/R/generics.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,11 @@ setGeneric("date_format", function(y, x) { standardGeneric("date_format") })
10431043
#' @name NULL
10441044
setGeneric("date_sub", function(y, x) { standardGeneric("date_sub") })
10451045

1046+
#' @rdname column_datetime_functions
1047+
#' @export
1048+
#' @name NULL
1049+
setGeneric("date_trunc", function(format, x) { standardGeneric("date_trunc") })
1050+
10461051
#' @rdname column_datetime_functions
10471052
#' @export
10481053
#' @name NULL

R/pkg/tests/fulltests/test_sparkSQL.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,8 @@ test_that("column functions", {
14181418
c22 <- not(c)
14191419
c23 <- trunc(c, "year") + trunc(c, "yyyy") + trunc(c, "yy") +
14201420
trunc(c, "month") + trunc(c, "mon") + trunc(c, "mm")
1421+
c24 <- date_trunc("hour", c) + date_trunc("minute", c) + date_trunc("week", c) +
1422+
date_trunc("quarter", c)
14211423

14221424
# Test if base::is.nan() is exposed
14231425
expect_equal(is.nan(c("a", "b")), c(FALSE, FALSE))
@@ -1729,6 +1731,7 @@ test_that("date functions on a DataFrame", {
17291731
expect_gt(collect(select(df2, unix_timestamp()))[1, 1], 0)
17301732
expect_gt(collect(select(df2, unix_timestamp(df2$b)))[1, 1], 0)
17311733
expect_gt(collect(select(df2, unix_timestamp(lit("2015-01-01"), "yyyy-MM-dd")))[1, 1], 0)
1734+
expect_equal(collect(select(df2, month(date_trunc("yyyy", df2$b))))[, 1], c(1, 1))
17321735

17331736
l3 <- list(list(a = 1000), list(a = -1000))
17341737
df3 <- createDataFrame(l3)

0 commit comments

Comments
 (0)