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
1 change: 1 addition & 0 deletions R/pkg/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ exportMethods("%<=>%",
"to_utc_timestamp",
"translate",
"trim",
"trunc",
Copy link
Member

Choose a reason for hiding this comment

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

it doesn't mask base::trunc?

Copy link
Member

Choose a reason for hiding this comment

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

and looks like it has a generic already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is part of the internally S4 methods and there is already generics.
This is similar to math functions like abs.
https://stat.ethz.ch/R-manual/R-devel/library/base/html/groupGeneric.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And yes, it doesn't mask base. You can still do trunc(10.5).

"unbase64",
"unhex",
"unix_timestamp",
Expand Down
29 changes: 29 additions & 0 deletions R/pkg/R/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -4015,3 +4015,32 @@ 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, as.character(format))
column(jc)
})
2 changes: 2 additions & 0 deletions R/pkg/tests/fulltests/test_sparkSQL.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down