Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-50364

Row.jsonValue is not able to serialize LocalDateTime

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 3.5.3
    • 4.0.0
    • SQL

    Description

      Trying to serialize to JSON a Row containing a column of type TimestampNTZType (LocalDateTime) with methods:

      def json: String = compact(jsonValue)
      def prettyJson: String = pretty(render(jsonValue)) 
      

      Will fail with:

      [FAILED_ROW_TO_JSON] Failed to convert the row value '2018-05-14T12:13' of the class class java.time.LocalDateTime to the target SQL type "TIMESTAMPNTZTYPE" in the JSON format. SQLSTATE: 2203G
      org.apache.spark.SparkIllegalArgumentException: [FAILED_ROW_TO_JSON] Failed to convert the row value '2018-05-14T12:13' of the class class java.time.LocalDateTime to the target SQL type "TIMESTAMPNTZTYPE" in the JSON format. SQLSTATE: 2203G
      	at org.apache.spark.sql.Row.toJson$1(Row.scala:663)
      	at org.apache.spark.sql.Row.toJson$1(Row.scala:651)
      	at org.apache.spark.sql.Row.jsonValue(Row.scala:665)
      	at org.apache.spark.sql.Row.jsonValue$(Row.scala:598)
      	at org.apache.spark.sql.catalyst.expressions.GenericRow.jsonValue(rows.scala:28)
      	at org.apache.spark.sql.RowJsonSuite.$anonfun$testJson$1(RowJsonSuite.scala:41)
      

      This happens because the toJson method inside of the class is missing a serializer for the LocalDateTime type

          def toJson(value: Any, dataType: DataType): JValue = (value, dataType) match {
            case (null, _) => JNull
            case (b: Boolean, _) => JBool(b)
            case (b: Byte, _) => JLong(b)
            case (s: Short, _) => JLong(s)
            case (i: Int, _) => JLong(i)
            case (l: Long, _) => JLong(l)
      ...
      

      Attachments

        Issue Links

          Activity

            People

              krm915 Karim Ramadan
              krm915 Karim Ramadan
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: