-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-19828][R] Support array type in from_json in R #17178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
10feb9d
f13ad18
5c2450d
d01f952
8ec7cd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1342,28 +1342,52 @@ test_that("column functions", { | |||||
| df <- read.json(mapTypeJsonPath) | ||||||
| j <- collect(select(df, alias(to_json(df$info), "json"))) | ||||||
| expect_equal(j[order(j$json), ][1], "{\"age\":16,\"height\":176.5}") | ||||||
| df <- as.DataFrame(j) | ||||||
| schema <- structType(structField("age", "integer"), | ||||||
| structField("height", "double")) | ||||||
| s <- collect(select(df, alias(from_json(df$json, schema), "structcol"))) | ||||||
| expect_equal(ncol(s), 1) | ||||||
| expect_equal(nrow(s), 3) | ||||||
| expect_is(s[[1]][[1]], "struct") | ||||||
| expect_true(any(apply(s, 1, function(x) { x[[1]]$age == 16 } ))) | ||||||
|
|
||||||
| # passing option | ||||||
| df <- as.DataFrame(list(list("col" = "{\"date\":\"21/10/2014\"}"))) | ||||||
| schema2 <- structType(structField("date", "date")) | ||||||
| expect_error(tryCatch(collect(select(df, from_json(df$col, schema2))), | ||||||
| error = function(e) { stop(e) }), | ||||||
| paste0(".*(java.lang.NumberFormatException: For input string:).*")) | ||||||
| s <- collect(select(df, from_json(df$col, schema2, dateFormat = "dd/MM/yyyy"))) | ||||||
| expect_is(s[[1]][[1]]$date, "Date") | ||||||
| expect_equal(as.character(s[[1]][[1]]$date), "2014-10-21") | ||||||
|
|
||||||
| # check for unparseable | ||||||
| df <- as.DataFrame(list(list("a" = ""))) | ||||||
| expect_equal(collect(select(df, from_json(df$a, schema)))[[1]][[1]], NA) | ||||||
|
|
||||||
| schemas <- list(structType(structField("age", "integer"), structField("height", "double")), | ||||||
| "struct<age:integer,height:double>") | ||||||
|
||||||
| def from_json(e: Column, schema: String, options: java.util.Map[String, String]): Column = | |
| from_json(e, DataType.fromJson(schema), options) |
Yup, let me give a shot to provide an option for it first to show if it looks okay to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yap that's what I thought. sounds good!
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I re-used the existing tests codes with the loop below:
- df <- as.DataFrame(j)
- schema <- structType(structField("age", "integer"),
- structField("height", "double"))
+ schemas <- list(structType(structField("age", "integer"), structField("height", "double")),
+ "struct<age:integer,height:double>")
+ for (schema in schemas) {
+ df <- as.DataFrame(j)
...
+ }
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Just in case, from_json takes both DataType and json string from DataType.json. In that sense, I thought it'd be nicer if it takes what structField takes in R)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I use a class union here in this file?