aboutsummaryrefslogtreecommitdiff
path: root/R/pkg/inst/tests
diff options
context:
space:
mode:
authorfelixcheung <felixcheung_m@hotmail.com>2015-11-28 21:16:21 -0800
committerShivaram Venkataraman <shivaram@cs.berkeley.edu>2015-11-28 21:16:21 -0800
commitc793d2d9a1ccc203fc103eb0636958fe8d71f471 (patch)
treea031cf5f43c0808478d738a9cde8ba412a9ab758 /R/pkg/inst/tests
parent28e46ab46368ea3833c8e805163893bbb6f2a265 (diff)
downloadspark-c793d2d9a1ccc203fc103eb0636958fe8d71f471.tar.gz
spark-c793d2d9a1ccc203fc103eb0636958fe8d71f471.tar.bz2
spark-c793d2d9a1ccc203fc103eb0636958fe8d71f471.zip
[SPARK-9319][SPARKR] Add support for setting column names, types
Add support for for colnames, colnames<-, coltypes<- Also added tests for names, names<- which have no test previously. I merged with PR 8984 (coltypes). Clicked the wrong thing, crewed up the PR. Recreated it here. Was #9218 shivaram sun-rui Author: felixcheung <felixcheung_m@hotmail.com> Closes #9654 from felixcheung/colnamescoltypes.
Diffstat (limited to 'R/pkg/inst/tests')
-rw-r--r--R/pkg/inst/tests/test_sparkSQL.R40
1 files changed, 39 insertions, 1 deletions
diff --git a/R/pkg/inst/tests/test_sparkSQL.R b/R/pkg/inst/tests/test_sparkSQL.R
index 899fc3b977..d3b2f20bf8 100644
--- a/R/pkg/inst/tests/test_sparkSQL.R
+++ b/R/pkg/inst/tests/test_sparkSQL.R
@@ -622,6 +622,26 @@ test_that("schema(), dtypes(), columns(), names() return the correct values/form
expect_equal(testNames[2], "name")
})
+test_that("names() colnames() set the column names", {
+ df <- jsonFile(sqlContext, jsonPath)
+ names(df) <- c("col1", "col2")
+ expect_equal(colnames(df)[2], "col2")
+
+ colnames(df) <- c("col3", "col4")
+ expect_equal(names(df)[1], "col3")
+
+ # Test base::colnames base::names
+ m2 <- cbind(1, 1:4)
+ expect_equal(colnames(m2, do.NULL = FALSE), c("col1", "col2"))
+ colnames(m2) <- c("x","Y")
+ expect_equal(colnames(m2), c("x", "Y"))
+
+ z <- list(a = 1, b = "c", c = 1:3)
+ expect_equal(names(z)[3], "c")
+ names(z)[3] <- "c2"
+ expect_equal(names(z)[3], "c2")
+})
+
test_that("head() and first() return the correct data", {
df <- jsonFile(sqlContext, jsonPath)
testHead <- head(df)
@@ -1617,7 +1637,7 @@ test_that("with() on a DataFrame", {
expect_equal(nrow(sum2), 35)
})
-test_that("Method coltypes() to get R's data types of a DataFrame", {
+test_that("Method coltypes() to get and set R's data types of a DataFrame", {
expect_equal(coltypes(irisDF), c(rep("numeric", 4), "character"))
data <- data.frame(c1=c(1,2,3),
@@ -1636,6 +1656,24 @@ test_that("Method coltypes() to get R's data types of a DataFrame", {
x <- createDataFrame(sqlContext, list(list(as.environment(
list("a"="b", "c"="d", "e"="f")))))
expect_equal(coltypes(x), "map<string,string>")
+
+ df <- selectExpr(jsonFile(sqlContext, jsonPath), "name", "(age * 1.21) as age")
+ expect_equal(dtypes(df), list(c("name", "string"), c("age", "decimal(24,2)")))
+
+ df1 <- select(df, cast(df$age, "integer"))
+ coltypes(df) <- c("character", "integer")
+ expect_equal(dtypes(df), list(c("name", "string"), c("age", "int")))
+ value <- collect(df[, 2])[[3, 1]]
+ expect_equal(value, collect(df1)[[3, 1]])
+ expect_equal(value, 22)
+
+ coltypes(df) <- c(NA, "numeric")
+ expect_equal(dtypes(df), list(c("name", "string"), c("age", "double")))
+
+ expect_error(coltypes(df) <- c("character"),
+ "Length of type vector should match the number of columns for DataFrame")
+ expect_error(coltypes(df) <- c("environment", "list"),
+ "Only atomic type is supported for column types")
})
unlink(parquetPath)