[SPARK-32946][R][SQL] Add withColumn to SparkR
### What changes were proposed in this pull request? This PR adds `withColumn` function SparkR. ### Why are the changes needed? ### Does this PR introduce _any_ user-facing change? Yes, new function, equivalent to Scala and PySpark equivalents, is exposed to the end user. ### How was this patch tested? New unit tests added. Closes #29814 from zero323/SPARK-32946. Authored-by: zero323 <mszymkiewicz@gmail.com> Signed-off-by: HyukjinKwon <gurwls223@apache.org>
This commit is contained in:
parent
0c66813ad9
commit
1ad1f71535
|
@ -428,6 +428,7 @@ exportMethods("%<=>%",
|
|||
"weekofyear",
|
||||
"when",
|
||||
"window",
|
||||
"withField",
|
||||
"xxhash64",
|
||||
"year")
|
||||
|
||||
|
|
|
@ -356,3 +356,34 @@ setMethod("%<=>%",
|
|||
#' }
|
||||
#' @note ! since 2.3.0
|
||||
setMethod("!", signature(x = "Column"), function(x) not(x))
|
||||
|
||||
#' withField
|
||||
#'
|
||||
#' Adds/replaces field in a struct \code{Column} by name.
|
||||
#'
|
||||
#' @param x a Column
|
||||
#' @param fieldName a character
|
||||
#' @param col a Column expression
|
||||
#'
|
||||
#' @rdname withField
|
||||
#' @aliases withField withField,Column-method
|
||||
#' @examples
|
||||
#' \dontrun{
|
||||
#' df <- withColumn(
|
||||
#' createDataFrame(iris),
|
||||
#' "sepal",
|
||||
#' struct(column("Sepal_Width"), column("Sepal_Length"))
|
||||
#' )
|
||||
#'
|
||||
#' head(select(
|
||||
#' df,
|
||||
#' withField(df$sepal, "product", df$Sepal_Length * df$Sepal_Width)
|
||||
#' ))
|
||||
#' }
|
||||
#' @note withField since 3.1.0
|
||||
setMethod("withField",
|
||||
signature(x = "Column", fieldName = "character", col = "Column"),
|
||||
function(x, fieldName, col) {
|
||||
jc <- callJMethod(x@jc, "withField", fieldName, col@jc)
|
||||
column(jc)
|
||||
})
|
||||
|
|
|
@ -729,6 +729,9 @@ setGeneric("over", function(x, window) { standardGeneric("over") })
|
|||
#' @rdname eq_null_safe
|
||||
setGeneric("%<=>%", function(x, value) { standardGeneric("%<=>%") })
|
||||
|
||||
#' @rdname withField
|
||||
setGeneric("withField", function(x, fieldName, col) { standardGeneric("withField") })
|
||||
|
||||
###################### WindowSpec Methods ##########################
|
||||
|
||||
#' @rdname partitionBy
|
||||
|
|
|
@ -1803,6 +1803,19 @@ test_that("column functions", {
|
|||
)
|
||||
|
||||
expect_equal(actual, expected)
|
||||
|
||||
# Test withField
|
||||
lines <- c("{\"Person\": {\"name\":\"Bob\", \"age\":24}}")
|
||||
jsonPath <- tempfile(pattern = "sparkr-test", fileext = ".tmp")
|
||||
writeLines(lines, jsonPath)
|
||||
df <- read.df(jsonPath, "json")
|
||||
result <- collect(
|
||||
select(
|
||||
select(df, alias(withField(df$Person, "dummy", lit(42)), "Person")),
|
||||
"Person.dummy"
|
||||
)
|
||||
)
|
||||
expect_equal(result, data.frame(dummy = 42))
|
||||
})
|
||||
|
||||
test_that("column binary mathfunctions", {
|
||||
|
|
Loading…
Reference in a new issue