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

Spark to_json parses UTC timestamp incorrectly

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Cannot Reproduce
    • 2.3.0
    • None
    • Spark Core
    • None

    Description

      When parsing ISO-8601 timestamp, if there is UTC suffix symbol, and more than 3 digits in the fraction part, from_json will give incorrect result.

       

      scala> val schema = new StructType().add("t", TimestampType)
      
      #
      # no "Z", no problem
      #
      
      scala> val t = "2019-03-20T09:01:03.1234567"
      scala> Seq((s"""{"t":"${t}"}""")).toDF("json").select(from_json(col("json"), schema)).show(false)
      +-------------------------+
      |jsontostructs(json)      |
      +-------------------------+
      |[2019-03-20 09:01:03.123]|
      +-------------------------+
      
      #
      # Add "Z", incorrect
      #
      
      scala> val t = "2019-03-20T09:01:03.1234567Z"
      scala> Seq((s"""{"t":"${t}"}""")).toDF("json").select(from_json(col("json"), schema)).show(false)
      +-------------------------+
      |jsontostructs(json)      |
      +-------------------------+
      |[2019-03-20 02:21:37.567]|
      +-------------------------+
      
      #
      # reduce the # of digits, the conversion is incorrect until only we reach 3 digits
      #
      
      scala> val t = "2019-03-20T09:01:03.123456Z"
      
      +-------------------------+
      |jsontostructs(json)      |
      +-------------------------+
      |[2019-03-20 02:03:06.456]|
      +-------------------------+
      
      scala> val t = "2019-03-20T09:01:03.12345Z
      
      +-------------------------+
      |jsontostructs(json)      |
      +-------------------------+
      |[2019-03-20 02:01:15.345]|
      +-------------------------+
      
      
      scala> val t = "2019-03-20T09:01:03.1234Z"
      
      +-------------------------+
      |jsontostructs(json)      |
      +-------------------------+
      |[2019-03-20 02:01:04.234]|
      +-------------------------+
      
      
      # correct when there is <=3 digits in fraction
      
      
      scala> val t = "2019-03-20T09:01:03.123Z"
      
      +-------------------------+
      |jsontostructs(json)      |
      +-------------------------+
      |[2019-03-20 02:01:03.123]|
      +-------------------------+
      
      
      scala> val t = "2019-03-20T09:01:03.999Z"
      
      +-------------------------+
      |jsontostructs(json)      |
      +-------------------------+
      |[2019-03-20 02:01:03.999]|
      +-------------------------+
      
      

       

      This could be related to SPARK-17914.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              jeffx Jeff Xu
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: