aboutsummaryrefslogtreecommitdiff
path: root/sql/core/src/main/scala/org/apache
diff options
context:
space:
mode:
authorTakeshi Yamamuro <yamamuro@apache.org>2017-03-29 12:37:49 -0700
committerXiao Li <gatorsmile@gmail.com>2017-03-29 12:37:49 -0700
commitc4008480b781379ac0451b9220300d83c054c60d (patch)
tree59931ecbbe483b9c963eeec0472e770d41d22d6f /sql/core/src/main/scala/org/apache
parent142f6d14928c780cc9e8d6d7749c5d7c08a30972 (diff)
downloadspark-c4008480b781379ac0451b9220300d83c054c60d.tar.gz
spark-c4008480b781379ac0451b9220300d83c054c60d.tar.bz2
spark-c4008480b781379ac0451b9220300d83c054c60d.zip
[SPARK-20009][SQL] Support DDL strings for defining schema in functions.from_json
## What changes were proposed in this pull request? This pr added `StructType.fromDDL` to convert a DDL format string into `StructType` for defining schemas in `functions.from_json`. ## How was this patch tested? Added tests in `JsonFunctionsSuite`. Author: Takeshi Yamamuro <yamamuro@apache.org> Closes #17406 from maropu/SPARK-20009.
Diffstat (limited to 'sql/core/src/main/scala/org/apache')
-rw-r--r--sql/core/src/main/scala/org/apache/spark/sql/functions.scala15
1 files changed, 12 insertions, 3 deletions
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 acdb8e2d3e..0f9203065e 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
@@ -21,6 +21,7 @@ import scala.collection.JavaConverters._
import scala.language.implicitConversions
import scala.reflect.runtime.universe.{typeTag, TypeTag}
import scala.util.Try
+import scala.util.control.NonFatal
import org.apache.spark.annotation.{Experimental, InterfaceStability}
import org.apache.spark.sql.catalyst.ScalaReflection
@@ -3055,13 +3056,21 @@ object functions {
* 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
+ * @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.1.0
*/
- def from_json(e: Column, schema: String, options: java.util.Map[String, String]): Column =
- from_json(e, DataType.fromJson(schema), options)
+ def from_json(e: Column, schema: String, options: java.util.Map[String, String]): Column = {
+ val dataType = try {
+ DataType.fromJson(schema)
+ } catch {
+ case NonFatal(_) => StructType.fromDDL(schema)
+ }
+ from_json(e, dataType, options)
+ }
/**
* (Scala-specific) Converts a column containing a `StructType` or `ArrayType` of `StructType`s