@@ -88,6 +88,13 @@ mockLinesComplexType <-
8888complexTypeJsonPath <- tempfile(pattern = " sparkr-test" , fileext = " .tmp" )
8989writeLines(mockLinesComplexType , complexTypeJsonPath )
9090
91+ # For test map type and struct type in DataFrame
92+ mockLinesMapType <- c(" {\" name\" :\" Bob\" ,\" info\" :{\" age\" :16,\" height\" :176.5}}" ,
93+ " {\" name\" :\" Alice\" ,\" info\" :{\" age\" :20,\" height\" :164.3}}" ,
94+ " {\" name\" :\" David\" ,\" info\" :{\" age\" :60,\" height\" :180}}" )
95+ mapTypeJsonPath <- tempfile(pattern = " sparkr-test" , fileext = " .tmp" )
96+ writeLines(mockLinesMapType , mapTypeJsonPath )
97+
9198test_that(" calling sparkRSQL.init returns existing SQL context" , {
9299 sqlContext <- suppressWarnings(sparkRSQL.init(sc ))
93100 expect_equal(suppressWarnings(sparkRSQL.init(sc )), sqlContext )
@@ -466,13 +473,6 @@ test_that("create DataFrame from a data.frame with complex types", {
466473 expect_equal(ldf $ an_envir , collected $ an_envir )
467474})
468475
469- # For test map type and struct type in DataFrame
470- mockLinesMapType <- c(" {\" name\" :\" Bob\" ,\" info\" :{\" age\" :16,\" height\" :176.5}}" ,
471- " {\" name\" :\" Alice\" ,\" info\" :{\" age\" :20,\" height\" :164.3}}" ,
472- " {\" name\" :\" David\" ,\" info\" :{\" age\" :60,\" height\" :180}}" )
473- mapTypeJsonPath <- tempfile(pattern = " sparkr-test" , fileext = " .tmp" )
474- writeLines(mockLinesMapType , mapTypeJsonPath )
475-
476476test_that(" Collect DataFrame with complex types" , {
477477 # ArrayType
478478 df <- read.json(complexTypeJsonPath )
@@ -1337,6 +1337,33 @@ test_that("column functions", {
13371337 df <- createDataFrame(data.frame (x = c(2.5 , 3.5 )))
13381338 expect_equal(collect(select(df , bround(df $ x , 0 )))[[1 ]][1 ], 2 )
13391339 expect_equal(collect(select(df , bround(df $ x , 0 )))[[1 ]][2 ], 4 )
1340+
1341+ # Test to_json(), from_json()
1342+ df <- read.json(mapTypeJsonPath )
1343+ j <- collect(select(df , alias(to_json(df $ info ), " json" )))
1344+ expect_equal(j [order(j $ json ), ][1 ], " {\" age\" :16,\" height\" :176.5}" )
1345+ df <- as.DataFrame(j )
1346+ schema <- structType(structField(" age" , " integer" ),
1347+ structField(" height" , " double" ))
1348+ s <- collect(select(df , alias(from_json(df $ json , schema ), " structcol" )))
1349+ expect_equal(ncol(s ), 1 )
1350+ expect_equal(nrow(s ), 3 )
1351+ expect_is(s [[1 ]][[1 ]], " struct" )
1352+ expect_true(any(apply(s , 1 , function (x ) { x [[1 ]]$ age == 16 } )))
1353+
1354+ # passing option
1355+ df <- as.DataFrame(list (list (" col" = " {\" date\" :\" 21/10/2014\" }" )))
1356+ schema2 <- structType(structField(" date" , " date" ))
1357+ expect_error(tryCatch(collect(select(df , from_json(df $ col , schema2 ))),
1358+ error = function (e ) { stop(e ) }),
1359+ paste0(" .*(java.lang.NumberFormatException: For input string:).*" ))
1360+ s <- collect(select(df , from_json(df $ col , schema2 , dateFormat = " dd/MM/yyyy" )))
1361+ expect_is(s [[1 ]][[1 ]]$ date , " Date" )
1362+ expect_equal(as.character(s [[1 ]][[1 ]]$ date ), " 2014-10-21" )
1363+
1364+ # check for unparseable
1365+ df <- as.DataFrame(list (list (" a" = " " )))
1366+ expect_equal(collect(select(df , from_json(df $ a , schema )))[[1 ]][[1 ]], NA )
13401367})
13411368
13421369test_that(" column binary mathfunctions" , {
@@ -1823,6 +1850,13 @@ test_that("union(), rbind(), except(), and intersect() on a DataFrame", {
18231850 expect_equal(count(unioned2 ), 12 )
18241851 expect_equal(first(unioned2 )$ name , " Michael" )
18251852
1853+ df3 <- df2
1854+ names(df3 )[1 ] <- " newName"
1855+ expect_error(rbind(df , df3 ),
1856+ " Names of input data frames are different." )
1857+ expect_error(rbind(df , df2 , df3 ),
1858+ " Names of input data frames are different." )
1859+
18261860 excepted <- arrange(except(df , df2 ), desc(df $ age ))
18271861 expect_is(unioned , " SparkDataFrame" )
18281862 expect_equal(count(excepted ), 2 )
@@ -2558,8 +2592,8 @@ test_that("coalesce, repartition, numPartitions", {
25582592
25592593 df2 <- repartition(df1 , 10 )
25602594 expect_equal(getNumPartitions(df2 ), 10 )
2561- expect_equal(getNumPartitions(coalesce(df2 , 13 )), 5 )
2562- expect_equal(getNumPartitions(coalesce(df2 , 7 )), 5 )
2595+ expect_equal(getNumPartitions(coalesce(df2 , 13 )), 10 )
2596+ expect_equal(getNumPartitions(coalesce(df2 , 7 )), 7 )
25632597 expect_equal(getNumPartitions(coalesce(df2 , 3 )), 3 )
25642598})
25652599
@@ -2867,5 +2901,7 @@ unlink(parquetPath)
28672901unlink(orcPath )
28682902unlink(jsonPath )
28692903unlink(jsonPathNa )
2904+ unlink(complexTypeJsonPath )
2905+ unlink(mapTypeJsonPath )
28702906
28712907sparkR.session.stop()
0 commit comments