Repository: spark
Updated Branches:
  refs/heads/master 3eb315d71 -> 223d83ee9


[SPARK-22476][R] Add dayofweek function to R

## What changes were proposed in this pull request?

This PR adds `dayofweek` to R API:

```r
data <- list(list(d = as.Date("2012-12-13")),
             list(d = as.Date("2013-12-14")),
             list(d = as.Date("2014-12-15")))
df <- createDataFrame(data)
collect(select(df, dayofweek(df$d)))
```

```
  dayofweek(d)
1            5
2            7
3            2
```

## How was this patch tested?

Manual tests and unit tests in `R/pkg/tests/fulltests/test_sparkSQL.R`

Author: hyukjinkwon <gurwls...@gmail.com>

Closes #19706 from HyukjinKwon/add-dayofweek.


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/223d83ee
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/223d83ee
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/223d83ee

Branch: refs/heads/master
Commit: 223d83ee93e604009afea4af3d13a838d08625a4
Parents: 3eb315d
Author: hyukjinkwon <gurwls...@gmail.com>
Authored: Sat Nov 11 19:16:31 2017 +0900
Committer: hyukjinkwon <gurwls...@gmail.com>
Committed: Sat Nov 11 19:16:31 2017 +0900

----------------------------------------------------------------------
 R/pkg/NAMESPACE                       |  1 +
 R/pkg/R/functions.R                   | 17 ++++++++++++++++-
 R/pkg/R/generics.R                    |  5 +++++
 R/pkg/tests/fulltests/test_sparkSQL.R |  1 +
 4 files changed, 23 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/223d83ee/R/pkg/NAMESPACE
----------------------------------------------------------------------
diff --git a/R/pkg/NAMESPACE b/R/pkg/NAMESPACE
index 3fc756b..57838f5 100644
--- a/R/pkg/NAMESPACE
+++ b/R/pkg/NAMESPACE
@@ -232,6 +232,7 @@ exportMethods("%<=>%",
               "date_sub",
               "datediff",
               "dayofmonth",
+              "dayofweek",
               "dayofyear",
               "decode",
               "dense_rank",

http://git-wip-us.apache.org/repos/asf/spark/blob/223d83ee/R/pkg/R/functions.R
----------------------------------------------------------------------
diff --git a/R/pkg/R/functions.R b/R/pkg/R/functions.R
index 0143a3e..237ef06 100644
--- a/R/pkg/R/functions.R
+++ b/R/pkg/R/functions.R
@@ -696,7 +696,7 @@ setMethod("hash",
 #'
 #' \dontrun{
 #' head(select(df, df$time, year(df$time), quarter(df$time), month(df$time),
-#'            dayofmonth(df$time), dayofyear(df$time), weekofyear(df$time)))
+#'             dayofmonth(df$time), dayofweek(df$time), dayofyear(df$time), 
weekofyear(df$time)))
 #' head(agg(groupBy(df, year(df$time)), count(df$y), avg(df$y)))
 #' head(agg(groupBy(df, month(df$time)), avg(df$y)))}
 #' @note dayofmonth since 1.5.0
@@ -708,6 +708,21 @@ setMethod("dayofmonth",
           })
 
 #' @details
+#' \code{dayofweek}: Extracts the day of the week as an integer from a
+#' given date/timestamp/string.
+#'
+#' @rdname column_datetime_functions
+#' @aliases dayofweek dayofweek,Column-method
+#' @export
+#' @note dayofweek since 2.3.0
+setMethod("dayofweek",
+          signature(x = "Column"),
+          function(x) {
+            jc <- callJStatic("org.apache.spark.sql.functions", "dayofweek", 
x@jc)
+            column(jc)
+          })
+
+#' @details
 #' \code{dayofyear}: Extracts the day of the year as an integer from a
 #' given date/timestamp/string.
 #'

http://git-wip-us.apache.org/repos/asf/spark/blob/223d83ee/R/pkg/R/generics.R
----------------------------------------------------------------------
diff --git a/R/pkg/R/generics.R b/R/pkg/R/generics.R
index 8312d41..8fcf269 100644
--- a/R/pkg/R/generics.R
+++ b/R/pkg/R/generics.R
@@ -1051,6 +1051,11 @@ setGeneric("dayofmonth", function(x) { 
standardGeneric("dayofmonth") })
 #' @rdname column_datetime_functions
 #' @export
 #' @name NULL
+setGeneric("dayofweek", function(x) { standardGeneric("dayofweek") })
+
+#' @rdname column_datetime_functions
+#' @export
+#' @name NULL
 setGeneric("dayofyear", function(x) { standardGeneric("dayofyear") })
 
 #' @rdname column_string_functions

http://git-wip-us.apache.org/repos/asf/spark/blob/223d83ee/R/pkg/tests/fulltests/test_sparkSQL.R
----------------------------------------------------------------------
diff --git a/R/pkg/tests/fulltests/test_sparkSQL.R 
b/R/pkg/tests/fulltests/test_sparkSQL.R
index a0dbd47..8a7fb12 100644
--- a/R/pkg/tests/fulltests/test_sparkSQL.R
+++ b/R/pkg/tests/fulltests/test_sparkSQL.R
@@ -1699,6 +1699,7 @@ test_that("date functions on a DataFrame", {
             list(a = 2L, b = as.Date("2013-12-14")),
             list(a = 3L, b = as.Date("2014-12-15")))
   df <- createDataFrame(l)
+  expect_equal(collect(select(df, dayofweek(df$b)))[, 1], c(5, 7, 2))
   expect_equal(collect(select(df, dayofmonth(df$b)))[, 1], c(13, 14, 15))
   expect_equal(collect(select(df, dayofyear(df$b)))[, 1], c(348, 348, 349))
   expect_equal(collect(select(df, weekofyear(df$b)))[, 1], c(50, 50, 51))


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org

Reply via email to