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

Spark SQL Unix Timestamp produces incorrect result with unix_timestamp UDF

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: In Progress
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.3.2, 2.4.4, 3.0.0
    • Fix Version/s: None
    • Component/s: SQL
    • Labels:
      None

      Description

       

      scala> spark.sql("select unix_timestamp('20201', 'yyyyww')").show();
      +-----------------------------+
      |unix_timestamp(20201, yyyyww)|
      +-----------------------------+
      |                         null|
      +-----------------------------+
       
      scala> spark.sql("select unix_timestamp('20202', 'yyyyww')").show();
      -----------------------------+
      |unix_timestamp(20202, yyyyww)|
      +-----------------------------+
      |                   1578182400|
      +-----------------------------+
       
      

       

       

      This seems to happen for leap year only, I dig deeper into it and it seems that  Spark is using the java.text.SimpleDateFormat and try to parse the expression here

      org.apache.spark.sql.catalyst.expressions.UnixTime#eval

      formatter.parse(
       t.asInstanceOf[UTF8String].toString).getTime / 1000L

       but fail and SimpleDateFormat unable to parse the date throw Unparseable Exception but Spark handle it silently and returns NULL.

       

      Spark-3.0: I did some tests where spark no longer using the legacy java.text.SimpleDateFormat but java date/time API, it seems  date/time API expect a valid date with valid format

       org.apache.spark.sql.catalyst.util.Iso8601TimestampFormatter#parse

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                Rajkumar Singh Rajkumar Singh
              • Votes:
                0 Vote for this issue
                Watchers:
                8 Start watching this issue

                Dates

                • Created:
                  Updated: