Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin-apache/master' into SPARK-22465
Browse files Browse the repository at this point in the history
  • Loading branch information
sujithjay committed Dec 23, 2017
2 parents 7d88e6c + aeb45df commit 6623227
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions R/pkg/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ exportMethods("%<=>%",
"date_add",
"date_format",
"date_sub",
"date_trunc",
"datediff",
"dayofmonth",
"dayofweek",
Expand Down
34 changes: 30 additions & 4 deletions R/pkg/R/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ NULL
#'
#' @param x Column to compute on. In \code{window}, it must be a time Column of
#' \code{TimestampType}.
#' @param format For \code{to_date} and \code{to_timestamp}, it is the string to use to parse
#' Column \code{x} to DateType or TimestampType. For \code{trunc}, it is the string
#' to use to specify the truncation method. For example, "year", "yyyy", "yy" for
#' truncate by year, or "month", "mon", "mm" for truncate by month.
#' @param format The format for the given dates or timestamps in Column \code{x}. See the
#' format used in the following methods:
#' \itemize{
#' \item \code{to_date} and \code{to_timestamp}: it is the string to use to parse
#' Column \code{x} to DateType or TimestampType.
#' \item \code{trunc}: it is the string to use to specify the truncation method.
#' For example, "year", "yyyy", "yy" for truncate by year, or "month", "mon",
#' "mm" for truncate by month.
#' \item \code{date_trunc}: it is similar with \code{trunc}'s but additionally
#' supports "day", "dd", "second", "minute", "hour", "week" and "quarter".
#' }
#' @param ... additional argument(s).
#' @name column_datetime_functions
#' @rdname column_datetime_functions
Expand Down Expand Up @@ -3478,3 +3485,22 @@ setMethod("trunc",
x@jc, as.character(format))
column(jc)
})

#' @details
#' \code{date_trunc}: Returns timestamp truncated to the unit specified by the format.
#'
#' @rdname column_datetime_functions
#' @aliases date_trunc date_trunc,character,Column-method
#' @export
#' @examples
#'
#' \dontrun{
#' head(select(df, df$time, date_trunc("hour", df$time), date_trunc("minute", df$time),
#' date_trunc("week", df$time), date_trunc("quarter", df$time)))}
#' @note date_trunc since 2.3.0
setMethod("date_trunc",
signature(format = "character", x = "Column"),
function(format, x) {
jc <- callJStatic("org.apache.spark.sql.functions", "date_trunc", format, x@jc)
column(jc)
})
5 changes: 5 additions & 0 deletions R/pkg/R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,11 @@ setGeneric("date_format", function(y, x) { standardGeneric("date_format") })
#' @name NULL
setGeneric("date_sub", function(y, x) { standardGeneric("date_sub") })

#' @rdname column_datetime_functions
#' @export
#' @name NULL
setGeneric("date_trunc", function(format, x) { standardGeneric("date_trunc") })

#' @rdname column_datetime_functions
#' @export
#' @name NULL
Expand Down
3 changes: 3 additions & 0 deletions R/pkg/tests/fulltests/test_sparkSQL.R
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,8 @@ test_that("column functions", {
c22 <- not(c)
c23 <- trunc(c, "year") + trunc(c, "yyyy") + trunc(c, "yy") +
trunc(c, "month") + trunc(c, "mon") + trunc(c, "mm")
c24 <- date_trunc("hour", c) + date_trunc("minute", c) + date_trunc("week", c) +
date_trunc("quarter", c)

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

l3 <- list(list(a = 1000), list(a = -1000))
df3 <- createDataFrame(l3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ object SparkHiveExample {
hiveTableDF.coalesce(10).write.mode(SaveMode.Overwrite)
.partitionBy("key").parquet(hiveExternalTableLocation)
// $example off:spark_hive$

spark.stop()
}
}

0 comments on commit 6623227

Please sign in to comment.