spark git commit: [SPARK-19130][SPARKR] Support setting literal value as column implicitly
Repository: spark Updated Branches: refs/heads/branch-2.1 1022049c7 -> 82fcc1330 [SPARK-19130][SPARKR] Support setting literal value as column implicitly ## What changes were proposed in this pull request? ``` df$foo <- 1 ``` instead of ``` df$foo <- lit(1) ``` ## How was this patch tested? unit tests Author: Felix Cheung Closes #16510 from felixcheung/rlitcol. (cherry picked from commit d749c06677c2fd38377f1c00f542da122b8d) Signed-off-by: Shivaram Venkataraman Project: http://git-wip-us.apache.org/repos/asf/spark/repo Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/82fcc133 Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/82fcc133 Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/82fcc133 Branch: refs/heads/branch-2.1 Commit: 82fcc133040cb5ef32f10df73fcb9fd8914aa9c1 Parents: 1022049 Author: Felix Cheung Authored: Wed Jan 11 08:29:09 2017 -0800 Committer: Shivaram Venkataraman Committed: Wed Jan 11 08:29:30 2017 -0800 -- R/pkg/R/DataFrame.R | 22 +- R/pkg/R/utils.R | 4 R/pkg/inst/tests/testthat/test_sparkSQL.R | 18 ++ 3 files changed, 39 insertions(+), 5 deletions(-) -- http://git-wip-us.apache.org/repos/asf/spark/blob/82fcc133/R/pkg/R/DataFrame.R -- diff --git a/R/pkg/R/DataFrame.R b/R/pkg/R/DataFrame.R index 058a77e..c79b1d3 100644 --- a/R/pkg/R/DataFrame.R +++ b/R/pkg/R/DataFrame.R @@ -1721,14 +1721,21 @@ setMethod("$", signature(x = "SparkDataFrame"), getColumn(x, name) }) -#' @param value a Column or \code{NULL}. If \code{NULL}, the specified Column is dropped. +#' @param value a Column or an atomic vector in the length of 1 as literal value, or \code{NULL}. +#' If \code{NULL}, the specified Column is dropped. #' @rdname select #' @name $<- #' @aliases $<-,SparkDataFrame-method #' @note $<- since 1.4.0 setMethod("$<-", signature(x = "SparkDataFrame"), function(x, name, value) { -stopifnot(class(value) == "Column" || is.null(value)) +if (class(value) != "Column" && !is.null(value)) { + if (isAtomicLengthOne(value)) { +value <- lit(value) + } else { +stop("value must be a Column, literal value as atomic in length of 1, or NULL") + } +} if (is.null(value)) { nx <- drop(x, name) @@ -1941,10 +1948,10 @@ setMethod("selectExpr", #' #' @param x a SparkDataFrame. #' @param colName a column name. -#' @param col a Column expression. +#' @param col a Column expression, or an atomic vector in the length of 1 as literal value. #' @return A SparkDataFrame with the new column added or the existing column replaced. #' @family SparkDataFrame functions -#' @aliases withColumn,SparkDataFrame,character,Column-method +#' @aliases withColumn,SparkDataFrame,character-method #' @rdname withColumn #' @name withColumn #' @seealso \link{rename} \link{mutate} @@ -1957,11 +1964,16 @@ setMethod("selectExpr", #' newDF <- withColumn(df, "newCol", df$col1 * 5) #' # Replace an existing column #' newDF2 <- withColumn(newDF, "newCol", newDF$col1) +#' newDF3 <- withColumn(newDF, "newCol", 42) #' } #' @note withColumn since 1.4.0 setMethod("withColumn", - signature(x = "SparkDataFrame", colName = "character", col = "Column"), + signature(x = "SparkDataFrame", colName = "character"), function(x, colName, col) { +if (class(col) != "Column") { + if (!isAtomicLengthOne(col)) stop("Literal value must be atomic in length of 1") + col <- lit(col) +} sdf <- callJMethod(x@sdf, "withColumn", colName, col@jc) dataFrame(sdf) }) http://git-wip-us.apache.org/repos/asf/spark/blob/82fcc133/R/pkg/R/utils.R -- diff --git a/R/pkg/R/utils.R b/R/pkg/R/utils.R index 1283449..74b3e50 100644 --- a/R/pkg/R/utils.R +++ b/R/pkg/R/utils.R @@ -863,3 +863,7 @@ basenameSansExtFromUrl <- function(url) { # then, strip extension by the last '.' sub("([^.]+)\\.[[:alnum:]]+$", "\\1", filename) } + +isAtomicLengthOne <- function(x) { + is.atomic(x) && length(x) == 1 +} http://git-wip-us.apache.org/repos/asf/spark/blob/82fcc133/R/pkg/inst/tests/testthat/test_sparkSQL.R -- diff --git a/R/pkg/inst/tests/testthat/test_sparkSQL.R b/R/pkg/inst/tests/testthat/test_sparkSQL.R index 4490f31..0be924f 100644 --- a/R/pkg/inst/tests/testthat/test_sparkSQL.R +++ b/R/pkg/inst/tests/testthat/test_sparkSQL.R @@ -1001,6 +1001,17 @@ test_that("select operators", { expect_equal(column
spark git commit: [SPARK-19130][SPARKR] Support setting literal value as column implicitly
Repository: spark Updated Branches: refs/heads/master 4239a1081 -> d749c0667 [SPARK-19130][SPARKR] Support setting literal value as column implicitly ## What changes were proposed in this pull request? ``` df$foo <- 1 ``` instead of ``` df$foo <- lit(1) ``` ## How was this patch tested? unit tests Author: Felix Cheung Closes #16510 from felixcheung/rlitcol. Project: http://git-wip-us.apache.org/repos/asf/spark/repo Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/d749c066 Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/d749c066 Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/d749c066 Branch: refs/heads/master Commit: d749c06677c2fd38377f1c00f542da122b8d Parents: 4239a10 Author: Felix Cheung Authored: Wed Jan 11 08:29:09 2017 -0800 Committer: Shivaram Venkataraman Committed: Wed Jan 11 08:29:09 2017 -0800 -- R/pkg/R/DataFrame.R | 22 +- R/pkg/R/utils.R | 4 R/pkg/inst/tests/testthat/test_sparkSQL.R | 18 ++ 3 files changed, 39 insertions(+), 5 deletions(-) -- http://git-wip-us.apache.org/repos/asf/spark/blob/d749c066/R/pkg/R/DataFrame.R -- diff --git a/R/pkg/R/DataFrame.R b/R/pkg/R/DataFrame.R index c56648a..3d912c9 100644 --- a/R/pkg/R/DataFrame.R +++ b/R/pkg/R/DataFrame.R @@ -1727,14 +1727,21 @@ setMethod("$", signature(x = "SparkDataFrame"), getColumn(x, name) }) -#' @param value a Column or \code{NULL}. If \code{NULL}, the specified Column is dropped. +#' @param value a Column or an atomic vector in the length of 1 as literal value, or \code{NULL}. +#' If \code{NULL}, the specified Column is dropped. #' @rdname select #' @name $<- #' @aliases $<-,SparkDataFrame-method #' @note $<- since 1.4.0 setMethod("$<-", signature(x = "SparkDataFrame"), function(x, name, value) { -stopifnot(class(value) == "Column" || is.null(value)) +if (class(value) != "Column" && !is.null(value)) { + if (isAtomicLengthOne(value)) { +value <- lit(value) + } else { +stop("value must be a Column, literal value as atomic in length of 1, or NULL") + } +} if (is.null(value)) { nx <- drop(x, name) @@ -1947,10 +1954,10 @@ setMethod("selectExpr", #' #' @param x a SparkDataFrame. #' @param colName a column name. -#' @param col a Column expression. +#' @param col a Column expression, or an atomic vector in the length of 1 as literal value. #' @return A SparkDataFrame with the new column added or the existing column replaced. #' @family SparkDataFrame functions -#' @aliases withColumn,SparkDataFrame,character,Column-method +#' @aliases withColumn,SparkDataFrame,character-method #' @rdname withColumn #' @name withColumn #' @seealso \link{rename} \link{mutate} @@ -1963,11 +1970,16 @@ setMethod("selectExpr", #' newDF <- withColumn(df, "newCol", df$col1 * 5) #' # Replace an existing column #' newDF2 <- withColumn(newDF, "newCol", newDF$col1) +#' newDF3 <- withColumn(newDF, "newCol", 42) #' } #' @note withColumn since 1.4.0 setMethod("withColumn", - signature(x = "SparkDataFrame", colName = "character", col = "Column"), + signature(x = "SparkDataFrame", colName = "character"), function(x, colName, col) { +if (class(col) != "Column") { + if (!isAtomicLengthOne(col)) stop("Literal value must be atomic in length of 1") + col <- lit(col) +} sdf <- callJMethod(x@sdf, "withColumn", colName, col@jc) dataFrame(sdf) }) http://git-wip-us.apache.org/repos/asf/spark/blob/d749c066/R/pkg/R/utils.R -- diff --git a/R/pkg/R/utils.R b/R/pkg/R/utils.R index 1283449..74b3e50 100644 --- a/R/pkg/R/utils.R +++ b/R/pkg/R/utils.R @@ -863,3 +863,7 @@ basenameSansExtFromUrl <- function(url) { # then, strip extension by the last '.' sub("([^.]+)\\.[[:alnum:]]+$", "\\1", filename) } + +isAtomicLengthOne <- function(x) { + is.atomic(x) && length(x) == 1 +} http://git-wip-us.apache.org/repos/asf/spark/blob/d749c066/R/pkg/inst/tests/testthat/test_sparkSQL.R -- diff --git a/R/pkg/inst/tests/testthat/test_sparkSQL.R b/R/pkg/inst/tests/testthat/test_sparkSQL.R index c3f0310..3e8b96a 100644 --- a/R/pkg/inst/tests/testthat/test_sparkSQL.R +++ b/R/pkg/inst/tests/testthat/test_sparkSQL.R @@ -1001,6 +1001,17 @@ test_that("select operators", { expect_equal(columns(df), c("name", "age", "age2")) expect_equal(count(where(df, df$age2 == df$age * 2)), 2) + df$age2 <- 21 + e