Repository: spark
Updated Branches:
  refs/heads/master c1e7989c4 -> 2dbe0c528


[SPARK-20775][SQL] Added scala support from_json

## What changes were proposed in this pull request?

from_json function required to take in a java.util.Hashmap. For other 
functions, a java wrapper is provided which casts a java hashmap to a scala 
map. Only a java function is provided in this case, forcing scala users to pass 
in a java.util.Hashmap.

Added the missing wrapper.

## How was this patch tested?
Added a unit test for passing in a scala map

Author: setjet <rubenljans...@gmail.com>

Closes #18094 from setjet/spark-20775.


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/2dbe0c52
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/2dbe0c52
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/2dbe0c52

Branch: refs/heads/master
Commit: 2dbe0c5288b48733bae0e39a6c5d8047f4a55088
Parents: c1e7989
Author: setjet <rubenljans...@gmail.com>
Authored: Fri May 26 10:21:39 2017 +0800
Committer: Wenchen Fan <wenc...@databricks.com>
Committed: Fri May 26 10:21:39 2017 +0800

----------------------------------------------------------------------
 .../scala/org/apache/spark/sql/functions.scala  | 22 ++++++++++++++++++--
 .../apache/spark/sql/JsonFunctionsSuite.scala   |  9 +++++++-
 2 files changed, 28 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/2dbe0c52/sql/core/src/main/scala/org/apache/spark/sql/functions.scala
----------------------------------------------------------------------
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/functions.scala 
b/sql/core/src/main/scala/org/apache/spark/sql/functions.scala
index 36c0f18..7eea6d8 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/functions.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/functions.scala
@@ -3060,8 +3060,9 @@ object functions {
     from_json(e, schema, Map.empty[String, String])
 
   /**
-   * Parses a column containing a JSON string into a `StructType` or 
`ArrayType` of `StructType`s
-   * with the specified schema. Returns `null`, in the case of an unparseable 
string.
+   * (Java-specific) Parses a column containing a JSON string into a 
`StructType` or `ArrayType`
+   * of `StructType`s with the specified schema. Returns `null`, in the case 
of an unparseable
+   * string.
    *
    * @param e a string column containing JSON data.
    * @param schema the schema to use when parsing the json string as a json 
string. In Spark 2.1,
@@ -3072,6 +3073,23 @@ object functions {
    * @since 2.1.0
    */
   def from_json(e: Column, schema: String, options: java.util.Map[String, 
String]): Column = {
+    from_json(e, schema, options.asScala.toMap)
+  }
+
+  /**
+   * (Scala-specific) Parses a column containing a JSON string into a 
`StructType` or `ArrayType`
+   * of `StructType`s with the specified schema. Returns `null`, in the case 
of an unparseable
+   * string.
+   *
+   * @param e a string column containing JSON data.
+   * @param schema the schema to use when parsing the json string as a json 
string. In Spark 2.1,
+   *               the user-provided schema has to be in JSON format. Since 
Spark 2.2, the DDL
+   *               format is also supported for the schema.
+   *
+   * @group collection_funcs
+   * @since 2.3.0
+   */
+  def from_json(e: Column, schema: String, options: Map[String, String]): 
Column = {
     val dataType = try {
       DataType.fromJson(schema)
     } catch {

http://git-wip-us.apache.org/repos/asf/spark/blob/2dbe0c52/sql/core/src/test/scala/org/apache/spark/sql/JsonFunctionsSuite.scala
----------------------------------------------------------------------
diff --git 
a/sql/core/src/test/scala/org/apache/spark/sql/JsonFunctionsSuite.scala 
b/sql/core/src/test/scala/org/apache/spark/sql/JsonFunctionsSuite.scala
index 69a500c..cf2d00f 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/JsonFunctionsSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/JsonFunctionsSuite.scala
@@ -156,13 +156,20 @@ class JsonFunctionsSuite extends QueryTest with 
SharedSQLContext {
       Row(Seq(Row(1, "a"), Row(2, null), Row(null, null))))
   }
 
-  test("from_json uses DDL strings for defining a schema") {
+  test("from_json uses DDL strings for defining a schema - java") {
     val df = Seq("""{"a": 1, "b": "haa"}""").toDS()
     checkAnswer(
       df.select(from_json($"value", "a INT, b STRING", new 
java.util.HashMap[String, String]())),
       Row(Row(1, "haa")) :: Nil)
   }
 
+  test("from_json uses DDL strings for defining a schema - scala") {
+    val df = Seq("""{"a": 1, "b": "haa"}""").toDS()
+    checkAnswer(
+      df.select(from_json($"value", "a INT, b STRING", Map[String, String]())),
+      Row(Row(1, "haa")) :: Nil)
+  }
+
   test("to_json - struct") {
     val df = Seq(Tuple1(Tuple1(1))).toDF("a")
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org

Reply via email to