diff --git a/R/pkg/NAMESPACE b/R/pkg/NAMESPACE index 232f5cf31f319721a191db0434c026618283cce3..a1dd1af4bf2664690033a4a8d1071734c680e2c2 100644 --- a/R/pkg/NAMESPACE +++ b/R/pkg/NAMESPACE @@ -286,6 +286,8 @@ exportMethods("%<=>%", "lower", "lpad", "ltrim", + "map_keys", + "map_values", "max", "md5", "mean", diff --git a/R/pkg/R/functions.R b/R/pkg/R/functions.R index 86507f13f038dfadeb6075c2f98b4f02cf1604db..5a46d737aeeb7abea1d7b6938d2fca09dab103a2 100644 --- a/R/pkg/R/functions.R +++ b/R/pkg/R/functions.R @@ -195,7 +195,10 @@ NULL #' head(tmp2) #' head(select(tmp, posexplode(tmp$v1))) #' head(select(tmp, sort_array(tmp$v1))) -#' head(select(tmp, sort_array(tmp$v1, asc = FALSE)))} +#' head(select(tmp, sort_array(tmp$v1, asc = FALSE))) +#' tmp3 <- mutate(df, v3 = create_map(df$model, df$cyl)) +#' head(select(tmp3, map_keys(tmp3$v3))) +#' head(select(tmp3, map_values(tmp3$v3)))} NULL #' Window functions for Column operations @@ -3055,6 +3058,34 @@ setMethod("array_contains", column(jc) }) +#' @details +#' \code{map_keys}: Returns an unordered array containing the keys of the map. +#' +#' @rdname column_collection_functions +#' @aliases map_keys map_keys,Column-method +#' @export +#' @note map_keys since 2.3.0 +setMethod("map_keys", + signature(x = "Column"), + function(x) { + jc <- callJStatic("org.apache.spark.sql.functions", "map_keys", x@jc) + column(jc) + }) + +#' @details +#' \code{map_values}: Returns an unordered array containing the values of the map. +#' +#' @rdname column_collection_functions +#' @aliases map_values map_values,Column-method +#' @export +#' @note map_values since 2.3.0 +setMethod("map_values", + signature(x = "Column"), + function(x) { + jc <- callJStatic("org.apache.spark.sql.functions", "map_values", x@jc) + column(jc) + }) + #' @details #' \code{explode}: Creates a new row for each element in the given array or map column. #' diff --git a/R/pkg/R/generics.R b/R/pkg/R/generics.R index 92098741f72f9fa9f66b084d7f24ce1a70411e34..df91c35f7d851ebfd3dfbe06236a5729dbfabe0a 100644 --- a/R/pkg/R/generics.R +++ b/R/pkg/R/generics.R @@ -1213,6 +1213,16 @@ setGeneric("lpad", function(x, len, pad) { standardGeneric("lpad") }) #' @name NULL setGeneric("ltrim", function(x) { standardGeneric("ltrim") }) +#' @rdname column_collection_functions +#' @export +#' @name NULL +setGeneric("map_keys", function(x) { standardGeneric("map_keys") }) + +#' @rdname column_collection_functions +#' @export +#' @name NULL +setGeneric("map_values", function(x) { standardGeneric("map_values") }) + #' @rdname column_misc_functions #' @export #' @name NULL diff --git a/R/pkg/tests/fulltests/test_sparkSQL.R b/R/pkg/tests/fulltests/test_sparkSQL.R index 77052d4a2834581c0afdfe044686cf6da1d50137..deb0e163a8d5864e8c728f95c5b9ed83ddd2a3ca 100644 --- a/R/pkg/tests/fulltests/test_sparkSQL.R +++ b/R/pkg/tests/fulltests/test_sparkSQL.R @@ -1436,6 +1436,14 @@ test_that("column functions", { result <- collect(select(df, sort_array(df[[1]])))[[1]] expect_equal(result, list(list(1L, 2L, 3L), list(4L, 5L, 6L))) + # Test map_keys() and map_values() + df <- createDataFrame(list(list(map = as.environment(list(x = 1, y = 2))))) + result <- collect(select(df, map_keys(df$map)))[[1]] + expect_equal(result, list(list("x", "y"))) + + result <- collect(select(df, map_values(df$map)))[[1]] + expect_equal(result, list(list(1, 2))) + # Test that stats::lag is working expect_equal(length(lag(ldeaths, 12)), 72)