@@ -49,17 +49,23 @@ mockLinesNa <- c("{\"name\":\"Bob\",\"age\":16,\"height\":176.5}",
4949jsonPathNa <- tempfile(pattern = " sparkr-test" , fileext = " .tmp" )
5050writeLines(mockLinesNa , jsonPathNa )
5151
52+ # For test complex types in DataFrame
53+ mockLinesComplexType <-
54+ c(" {\" c1\" :[1, 2, 3], \" c2\" :[\" a\" , \" b\" , \" c\" ], \" c3\" :[1.0, 2.0, 3.0]}" ,
55+ " {\" c1\" :[4, 5, 6], \" c2\" :[\" d\" , \" e\" , \" f\" ], \" c3\" :[4.0, 5.0, 6.0]}" ,
56+ " {\" c1\" :[7, 8, 9], \" c2\" :[\" g\" , \" h\" , \" i\" ], \" c3\" :[7.0, 8.0, 9.0]}" )
57+ complexTypeJsonPath <- tempfile(pattern = " sparkr-test" , fileext = " .tmp" )
58+ writeLines(mockLinesComplexType , complexTypeJsonPath )
59+
5260test_that(" infer types" , {
5361 expect_equal(infer_type(1L ), " integer" )
5462 expect_equal(infer_type(1.0 ), " double" )
5563 expect_equal(infer_type(" abc" ), " string" )
5664 expect_equal(infer_type(TRUE ), " boolean" )
5765 expect_equal(infer_type(as.Date(" 2015-03-11" )), " date" )
5866 expect_equal(infer_type(as.POSIXlt(" 2015-03-11 12:13:04.043" )), " timestamp" )
59- expect_equal(infer_type(c(1L , 2L )),
60- list (type = " array" , elementType = " integer" , containsNull = TRUE ))
61- expect_equal(infer_type(list (1L , 2L )),
62- list (type = " array" , elementType = " integer" , containsNull = TRUE ))
67+ expect_equal(infer_type(c(1L , 2L )), " array<integer>" )
68+ expect_equal(infer_type(list (1L , 2L )), " array<integer>" )
6369 testStruct <- infer_type(list (a = 1L , b = " 2" ))
6470 expect_equal(class(testStruct ), " structType" )
6571 checkStructField(testStruct $ fields()[[1 ]], " a" , " IntegerType" , TRUE )
@@ -236,8 +242,7 @@ test_that("create DataFrame with different data types", {
236242 expect_equal(collect(df ), data.frame (l , stringsAsFactors = FALSE ))
237243})
238244
239- # TODO: enable this test after fix serialization for nested object
240- # test_that("create DataFrame with nested array and struct", {
245+ test_that(" create DataFrame with nested array and struct" , {
241246# e <- new.env()
242247# assign("n", 3L, envir = e)
243248# l <- list(1:10, list("a", "b"), e, list(a="aa", b=3L))
@@ -247,7 +252,32 @@ test_that("create DataFrame with different data types", {
247252# expect_equal(count(df), 1)
248253# ldf <- collect(df)
249254# expect_equal(ldf[1,], l[[1]])
250- # })
255+
256+
257+ # ArrayType only for now
258+ l <- list (as.list(1 : 10 ), list (" a" , " b" ))
259+ df <- createDataFrame(sqlContext , list (l ), c(" a" , " b" ))
260+ expect_equal(dtypes(df ), list (c(" a" , " array<int>" ), c(" b" , " array<string>" )))
261+ expect_equal(count(df ), 1 )
262+ ldf <- collect(df )
263+ expect_equal(names(ldf ), c(" a" , " b" ))
264+ expect_equal(ldf [1 , 1 ][[1 ]], l [[1 ]])
265+ expect_equal(ldf [1 , 2 ][[1 ]], l [[2 ]])
266+ })
267+
268+ test_that(" Collect DataFrame with complex types" , {
269+ # only ArrayType now
270+ # TODO: tests for StructType and MapType after they are supported
271+ df <- jsonFile(sqlContext , complexTypeJsonPath )
272+
273+ ldf <- collect(df )
274+ expect_equal(nrow(ldf ), 3 )
275+ expect_equal(ncol(ldf ), 3 )
276+ expect_equal(names(ldf ), c(" c1" , " c2" , " c3" ))
277+ expect_equal(ldf $ c1 , list (list (1 , 2 , 3 ), list (4 , 5 , 6 ), list (7 , 8 , 9 )))
278+ expect_equal(ldf $ c2 , list (list (" a" , " b" , " c" ), list (" d" , " e" , " f" ), list (" g" , " h" , " i" )))
279+ expect_equal(ldf $ c3 , list (list (1.0 , 2.0 , 3.0 ), list (4.0 , 5.0 , 6.0 ), list (7.0 , 8.0 , 9.0 )))
280+ })
251281
252282test_that(" jsonFile() on a local file returns a DataFrame" , {
253283 df <- jsonFile(sqlContext , jsonPath )
0 commit comments