aboutsummaryrefslogtreecommitdiff
path: root/R
diff options
context:
space:
mode:
authoractuaryzhang <actuaryzhang10@gmail.com>2017-02-05 11:37:45 -0800
committerFelix Cheung <felixcheung@apache.org>2017-02-05 11:37:45 -0800
commitb94f4b6fa65232b6113125198e031b5ad0faafd6 (patch)
tree52c4acfd5f93e79c283d1e4e8d5b4b6a1fe65a53 /R
parent0674e7eb85160e3f8da333b5243d76063824d58c (diff)
downloadspark-b94f4b6fa65232b6113125198e031b5ad0faafd6.tar.gz
spark-b94f4b6fa65232b6113125198e031b5ad0faafd6.tar.bz2
spark-b94f4b6fa65232b6113125198e031b5ad0faafd6.zip
[SPARK-19452][SPARKR] Fix bug in the name assignment method
## What changes were proposed in this pull request? The names method fails to check for validity of the assignment values. This can be fixed by calling colnames within names. ## How was this patch tested? new tests. Author: actuaryzhang <actuaryzhang10@gmail.com> Closes #16794 from actuaryzhang/sparkRNames.
Diffstat (limited to 'R')
-rw-r--r--R/pkg/R/DataFrame.R6
-rw-r--r--R/pkg/inst/tests/testthat/test_sparkSQL.R8
2 files changed, 10 insertions, 4 deletions
diff --git a/R/pkg/R/DataFrame.R b/R/pkg/R/DataFrame.R
index bfec3245cf..fefe25b148 100644
--- a/R/pkg/R/DataFrame.R
+++ b/R/pkg/R/DataFrame.R
@@ -323,10 +323,8 @@ setMethod("names",
setMethod("names<-",
signature(x = "SparkDataFrame"),
function(x, value) {
- if (!is.null(value)) {
- sdf <- callJMethod(x@sdf, "toDF", as.list(value))
- dataFrame(sdf)
- }
+ colnames(x) <- value
+ x
})
#' @rdname columns
diff --git a/R/pkg/inst/tests/testthat/test_sparkSQL.R b/R/pkg/inst/tests/testthat/test_sparkSQL.R
index 417a03ff61..418f128ce8 100644
--- a/R/pkg/inst/tests/testthat/test_sparkSQL.R
+++ b/R/pkg/inst/tests/testthat/test_sparkSQL.R
@@ -869,6 +869,14 @@ test_that("names() colnames() set the column names", {
colnames(df) <- c("col3", "col4")
expect_equal(names(df)[1], "col3")
+ expect_error(names(df) <- NULL, "Invalid column names.")
+ expect_error(names(df) <- c("sepal.length", "sepal_width"),
+ "Column names cannot contain the '.' symbol.")
+ expect_error(names(df) <- c(1, 2), "Invalid column names.")
+ expect_error(names(df) <- c("a"),
+ "Column names must have the same length as the number of columns in the dataset.")
+ expect_error(names(df) <- c("1", NA), "Column names cannot be NA.")
+
expect_error(colnames(df) <- c("sepal.length", "sepal_width"),
"Column names cannot contain the '.' symbol.")
expect_error(colnames(df) <- c(1, 2), "Invalid column names.")