@@ -196,6 +196,26 @@ test_that("create DataFrame from RDD", {
196196 expect_equal(dtypes(df ), list (c(" name" , " string" ), c(" age" , " int" ), c(" height" , " float" )))
197197 expect_equal(as.list(collect(where(df , df $ name == " John" ))),
198198 list (name = " John" , age = 19L , height = 176.5 ))
199+ expect_equal(getNumPartitions(toRDD(df )), 1 )
200+
201+ df <- as.DataFrame(cars , numPartitions = 2 )
202+ expect_equal(getNumPartitions(toRDD(df )), 2 )
203+ df <- createDataFrame(cars , numPartitions = 3 )
204+ expect_equal(getNumPartitions(toRDD(df )), 3 )
205+ # validate limit by num of rows
206+ df <- createDataFrame(cars , numPartitions = 60 )
207+ expect_equal(getNumPartitions(toRDD(df )), 50 )
208+ # validate when 1 < (length(coll) / numSlices) << length(coll)
209+ df <- createDataFrame(cars , numPartitions = 20 )
210+ expect_equal(getNumPartitions(toRDD(df )), 20 )
211+
212+ df <- as.DataFrame(data.frame (0 ))
213+ expect_is(df , " SparkDataFrame" )
214+ df <- createDataFrame(list (list (1 )))
215+ expect_is(df , " SparkDataFrame" )
216+ df <- as.DataFrame(data.frame (0 ), numPartitions = 2 )
217+ # no data to partition, goes to 1
218+ expect_equal(getNumPartitions(toRDD(df )), 1 )
199219
200220 setHiveContext(sc )
201221 sql(" CREATE TABLE people (name string, age double, height float)" )
@@ -213,7 +233,8 @@ test_that("createDataFrame uses files for large objects", {
213233 # To simulate a large file scenario, we set spark.r.maxAllocationLimit to a smaller value
214234 conf <- callJMethod(sparkSession , " conf" )
215235 callJMethod(conf , " set" , " spark.r.maxAllocationLimit" , " 100" )
216- df <- suppressWarnings(createDataFrame(iris ))
236+ df <- suppressWarnings(createDataFrame(iris , numPartitions = 3 ))
237+ expect_equal(getNumPartitions(toRDD(df )), 3 )
217238
218239 # Resetting the conf back to default value
219240 callJMethod(conf , " set" , " spark.r.maxAllocationLimit" , toString(.Machine $ integer.max / 10 ))
@@ -1001,6 +1022,17 @@ test_that("select operators", {
10011022 expect_equal(columns(df ), c(" name" , " age" , " age2" ))
10021023 expect_equal(count(where(df , df $ age2 == df $ age * 2 )), 2 )
10031024
1025+ df $ age2 <- 21
1026+ expect_equal(columns(df ), c(" name" , " age" , " age2" ))
1027+ expect_equal(count(where(df , df $ age2 == 21 )), 3 )
1028+
1029+ df $ age2 <- c(22 )
1030+ expect_equal(columns(df ), c(" name" , " age" , " age2" ))
1031+ expect_equal(count(where(df , df $ age2 == 22 )), 3 )
1032+
1033+ expect_error(df $ age3 <- c(22 , NA ),
1034+ " value must be a Column, literal value as atomic in length of 1, or NULL" )
1035+
10041036 # Test parameter drop
10051037 expect_equal(class(df [, 1 ]) == " SparkDataFrame" , T )
10061038 expect_equal(class(df [, 1 , drop = T ]) == " Column" , T )
@@ -1778,6 +1810,13 @@ test_that("withColumn() and withColumnRenamed()", {
17781810 expect_equal(length(columns(newDF )), 2 )
17791811 expect_equal(first(filter(newDF , df $ name != " Michael" ))$ age , 32 )
17801812
1813+ newDF <- withColumn(df , " age" , 18 )
1814+ expect_equal(length(columns(newDF )), 2 )
1815+ expect_equal(first(newDF )$ age , 18 )
1816+
1817+ expect_error(withColumn(df , " age" , list (" a" )),
1818+ " Literal value must be atomic in length of 1" )
1819+
17811820 newDF2 <- withColumnRenamed(df , " age" , " newerAge" )
17821821 expect_equal(length(columns(newDF2 )), 2 )
17831822 expect_equal(columns(newDF2 )[1 ], " newerAge" )
0 commit comments