@@ -205,11 +205,18 @@ NULL
205
205
# ' also supported for the schema.
206
206
# ' \item \code{from_csv}: a DDL-formatted string
207
207
# ' }
208
- # ' @param ... additional argument(s). In \code{to_json}, \code{to_csv} and \code{from_json},
209
- # ' this contains additional named properties to control how it is converted, accepts
210
- # ' the same options as the JSON/CSV data source. Additionally \code{to_json} supports
211
- # ' the "pretty" option which enables pretty JSON generation. In \code{arrays_zip},
212
- # ' this contains additional Columns of arrays to be merged.
208
+ # ' @param ... additional argument(s).
209
+ # ' \itemize{
210
+ # ' \item \code{to_json}, \code{from_json} and \code{schema_of_json}: this contains
211
+ # ' additional named properties to control how it is converted and accepts the
212
+ # ' same options as the JSON data source.
213
+ # ' \item \code{to_json}: it supports the "pretty" option which enables pretty
214
+ # ' JSON generation.
215
+ # ' \item \code{to_csv}, \code{from_csv} and \code{schema_of_csv}: this contains
216
+ # ' additional named properties to control how it is converted and accepts the
217
+ # ' same options as the CSV data source.
218
+ # ' \item \code{arrays_zip}, this contains additional Columns of arrays to be merged.
219
+ # ' }
213
220
# ' @name column_collection_functions
214
221
# ' @rdname column_collection_functions
215
222
# ' @family collection functions
@@ -1771,12 +1778,16 @@ setMethod("to_date",
1771
1778
# ' df2 <- mutate(df2, people_json = to_json(df2$people))
1772
1779
# '
1773
1780
# ' # Converts a map into a JSON object
1774
- # ' df2 <- sql("SELECT map('name', 'Bob')) as people")
1781
+ # ' df2 <- sql("SELECT map('name', 'Bob') as people")
1775
1782
# ' df2 <- mutate(df2, people_json = to_json(df2$people))
1776
1783
# '
1777
1784
# ' # Converts an array of maps into a JSON array
1778
1785
# ' df2 <- sql("SELECT array(map('name', 'Bob'), map('name', 'Alice')) as people")
1779
- # ' df2 <- mutate(df2, people_json = to_json(df2$people))}
1786
+ # ' df2 <- mutate(df2, people_json = to_json(df2$people))
1787
+ # '
1788
+ # ' # Converts a map into a pretty JSON object
1789
+ # ' df2 <- sql("SELECT map('name', 'Bob') as people")
1790
+ # ' df2 <- mutate(df2, people_json = to_json(df2$people, pretty = TRUE))}
1780
1791
# ' @note to_json since 2.2.0
1781
1792
setMethod ("to_json ", signature(x = "Column"),
1782
1793
function (x , ... ) {
@@ -2285,6 +2296,32 @@ setMethod("from_json", signature(x = "Column", schema = "characterOrstructType")
2285
2296
column(jc )
2286
2297
})
2287
2298
2299
+ # ' @details
2300
+ # ' \code{schema_of_json}: Parses a JSON string and infers its schema in DDL format.
2301
+ # '
2302
+ # ' @rdname column_collection_functions
2303
+ # ' @aliases schema_of_json schema_of_json,characterOrColumn-method
2304
+ # ' @examples
2305
+ # '
2306
+ # ' \dontrun{
2307
+ # ' json <- "{\"name\":\"Bob\"}"
2308
+ # ' df <- sql("SELECT * FROM range(1)")
2309
+ # ' head(select(df, schema_of_json(json)))}
2310
+ # ' @note schema_of_json since 3.0.0
2311
+ setMethod ("schema_of_json ", signature(x = "characterOrColumn"),
2312
+ function (x , ... ) {
2313
+ if (class(x ) == " character" ) {
2314
+ col <- callJStatic(" org.apache.spark.sql.functions" , " lit" , x )
2315
+ } else {
2316
+ col <- x @ jc
2317
+ }
2318
+ options <- varargsToStrEnv(... )
2319
+ jc <- callJStatic(" org.apache.spark.sql.functions" ,
2320
+ " schema_of_json" ,
2321
+ col , options )
2322
+ column(jc )
2323
+ })
2324
+
2288
2325
# ' @details
2289
2326
# ' \code{from_csv}: Parses a column containing a CSV string into a Column of \code{structType}
2290
2327
# ' with the specified \code{schema}.
@@ -2315,6 +2352,32 @@ setMethod("from_csv", signature(x = "Column", schema = "characterOrColumn"),
2315
2352
column(jc )
2316
2353
})
2317
2354
2355
+ # ' @details
2356
+ # ' \code{schema_of_csv}: Parses a CSV string and infers its schema in DDL format.
2357
+ # '
2358
+ # ' @rdname column_collection_functions
2359
+ # ' @aliases schema_of_csv schema_of_csv,characterOrColumn-method
2360
+ # ' @examples
2361
+ # '
2362
+ # ' \dontrun{
2363
+ # ' csv <- "Amsterdam,2018"
2364
+ # ' df <- sql("SELECT * FROM range(1)")
2365
+ # ' head(select(df, schema_of_csv(csv)))}
2366
+ # ' @note schema_of_csv since 3.0.0
2367
+ setMethod ("schema_of_csv ", signature(x = "characterOrColumn"),
2368
+ function (x , ... ) {
2369
+ if (class(x ) == " character" ) {
2370
+ col <- callJStatic(" org.apache.spark.sql.functions" , " lit" , x )
2371
+ } else {
2372
+ col <- x @ jc
2373
+ }
2374
+ options <- varargsToStrEnv(... )
2375
+ jc <- callJStatic(" org.apache.spark.sql.functions" ,
2376
+ " schema_of_csv" ,
2377
+ col , options )
2378
+ column(jc )
2379
+ })
2380
+
2318
2381
# ' @details
2319
2382
# ' \code{from_utc_timestamp}: This is a common function for databases supporting TIMESTAMP WITHOUT
2320
2383
# ' TIMEZONE. This function takes a timestamp which is timezone-agnostic, and interprets it as a
0 commit comments