Github user brkyvz commented on a diff in the pull request:

    https://github.com/apache/spark/pull/16929#discussion_r103268655
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
 ---
    @@ -480,36 +480,79 @@ case class JsonTuple(children: Seq[Expression])
     }
     
     /**
    - * Converts an json input string to a [[StructType]] with the specified 
schema.
    + * Converts an json input string to a [[StructType]] or [[ArrayType]] with 
the specified schema.
      */
     case class JsonToStruct(
    -    schema: StructType,
    +    schema: DataType,
         options: Map[String, String],
         child: Expression,
         timeZoneId: Option[String] = None)
       extends UnaryExpression with TimeZoneAwareExpression with 
CodegenFallback with ExpectsInputTypes {
       override def nullable: Boolean = true
     
    -  def this(schema: StructType, options: Map[String, String], child: 
Expression) =
    +  def this(schema: DataType, options: Map[String, String], child: 
Expression) =
         this(schema, options, child, None)
     
    +  override def checkInputDataTypes(): TypeCheckResult = schema match {
    +    case _: StructType | ArrayType(_: StructType, _) =>
    +      super.checkInputDataTypes()
    +    case _ => TypeCheckResult.TypeCheckFailure(
    +      s"Input schema ${schema.simpleString} must be a struct or an array 
of structs.")
    +  }
    +
    +  @transient
    +  lazy val rowSchema = schema match {
    +    case st: StructType => st
    +    case ArrayType(st: StructType, _) => st
    +  }
    +
    +  // This converts parsed rows to the desired output by the given schema.
    +  @transient
    +  lazy val converter = schema match {
    +    case _: StructType =>
    +      // These are always produced from json objects by `objectSupport` in 
`JacksonParser`.
    +      (rows: Seq[InternalRow]) => rows.head
    +
    +    case ArrayType(_: StructType, _) =>
    +      // These are always produced from json arrays by `arraySupport` in 
`JacksonParser`.
    +      (rows: Seq[InternalRow]) => new GenericArrayData(rows)
    +  }
    +
       @transient
       lazy val parser =
         new JacksonParser(
    -      schema,
    -      new JSONOptions(options + ("mode" -> ParseModes.FAIL_FAST_MODE), 
timeZoneId.get))
    +      rowSchema,
    +      new JSONOptions(options + ("mode" -> ParseModes.FAIL_FAST_MODE), 
timeZoneId.get),
    +      objectSupport = schema.isInstanceOf[StructType],
    --- End diff --
    
    Do you think we need the `objectSupport` and `arraySupport`?
    I would rather not add it. If someone specifies an `ArrayType` but the row 
contains just an object, let's still just return it as an `ArrayType`. I think 
users would appreciate this.


---
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]

Reply via email to