Github user HyukjinKwon commented on a diff in the pull request:
https://github.com/apache/spark/pull/18498#discussion_r126295982
--- Diff: R/pkg/R/functions.R ---
@@ -2163,10 +2164,15 @@ setMethod("date_format", signature(y = "Column", x
= "character"),
#' df2 <- sql("SELECT named_struct('name', 'Bob') as people")
#' df2 <- mutate(df2, people_json = to_json(df2$people))
#' schema <- structType(structField("name", "string"))
-#' head(select(df2, from_json(df2$people_json, schema)))}
+#' head(select(df2, from_json(df2$people_json, schema)))
+#' head(select(df2, from_json(df2$people_json, "name STRING")))}
#' @note from_json since 2.2.0
-setMethod("from_json", signature(x = "Column", schema = "structType"),
+setMethod("from_json", signature(x = "Column", schema =
"characterOrstructType"),
function(x, schema, as.json.array = FALSE, ...) {
+ if (is.character(schema)) {
+ schema <- structTypeFromDDL(schema)
+ }
--- End diff --
> btw, is it possible to support JSON format string in R too?
Yea, it is possible. I could resemble
[this](https://github.com/apache/spark/blob/c3712b77a915a5cb12b6c0204bc5bd6037aad1f5/sql/core/src/main/scala/org/apache/spark/sql/functions.scala#L3085-L3089),
for example, as below:
```R
if (is.character(schema)) {
tryCatch({
schema <- handledCallJStatic("org.apache.spark.sql.types.StructType",
"fromJson",
x)
}, error = function(e) {
schema <- structTypeFromDDL(schema)
}
}
```
I guess it is consistency vs practical use case for `from_json`. I'd follow
your decision @felixcheung.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]