Skip to content

Commit

Permalink
[SPARK-6785][SQL] fix DateTimeUtils.fromJavaDate(java.util.Date) for …
Browse files Browse the repository at this point in the history
…Dates before 1970
  • Loading branch information
ckadner committed Jun 24, 2015
1 parent 0401cba commit a451184
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ object DateTimeUtils {
}

// we should use the exact day as Int, for example, (year, month, day) -> day
def millisToDays(millisLocal: Long): Int = {
((millisLocal + LOCAL_TIMEZONE.get().getOffset(millisLocal)) / MILLIS_PER_DAY).toInt
def millisToDays(millisUtc: Long): Int = {
// SPARK-6785: use Math.floor so negative number of days (dates before 1970)
// will correctly work as input for function toJavaDate(Int)
val millisLocal = millisUtc.toDouble + LOCAL_TIMEZONE.get().getOffset(millisUtc)
Math.floor(millisLocal / MILLIS_PER_DAY).toInt
}

def toMillisSinceEpoch(days: Int): Long = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

package org.apache.spark.sql.catalyst.expressions

import java.sql.Date
import java.text.SimpleDateFormat

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.types.{StringType, BinaryType}
import org.apache.spark.sql.catalyst.util.DateTimeUtils
import org.apache.spark.sql.types.BinaryType

class MiscFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {

Expand All @@ -29,4 +33,41 @@ class MiscFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(Md5(Literal.create(null, BinaryType)), null)
}

test("SPARK-6785: java date conversion before and after epoch") {
def checkFromToJavaDate(d1: Date): Unit = {
val d2 = DateTimeUtils.toJavaDate(DateTimeUtils.fromJavaDate(d1))
assert(d2.toString === d1.toString)
}

val df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z")

checkFromToJavaDate(new Date(100))

checkFromToJavaDate(Date.valueOf("1970-01-01"))

checkFromToJavaDate(new Date(df1.parse("1970-01-01 00:00:00").getTime))
checkFromToJavaDate(new Date(df2.parse("1970-01-01 00:00:00 UTC").getTime))

checkFromToJavaDate(new Date(df1.parse("1970-01-01 00:00:01").getTime))
checkFromToJavaDate(new Date(df2.parse("1970-01-01 00:00:01 UTC").getTime))

checkFromToJavaDate(new Date(df1.parse("1969-12-31 23:59:59").getTime))
checkFromToJavaDate(new Date(df2.parse("1969-12-31 23:59:59 UTC").getTime))

checkFromToJavaDate(Date.valueOf("1969-01-01"))

checkFromToJavaDate(new Date(df1.parse("1969-01-01 00:00:00").getTime))
checkFromToJavaDate(new Date(df2.parse("1969-01-01 00:00:00 UTC").getTime))

checkFromToJavaDate(new Date(df1.parse("1969-01-01 00:00:01").getTime))
checkFromToJavaDate(new Date(df2.parse("1969-01-01 00:00:01 UTC").getTime))

checkFromToJavaDate(new Date(df1.parse("1989-11-09 11:59:59").getTime))
checkFromToJavaDate(new Date(df2.parse("1989-11-09 19:59:59 UTC").getTime))

checkFromToJavaDate(new Date(df1.parse("1776-07-04 10:30:00").getTime))
checkFromToJavaDate(new Date(df2.parse("1776-07-04 18:30:00 UTC").getTime))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ScalaReflectionRelationSuite extends SparkFunSuite {

test("query case class RDD") {
val data = ReflectData("a", 1, 1L, 1.toFloat, 1.toDouble, 1.toShort, 1.toByte, true,
new java.math.BigDecimal(1), new Date(12345), new Timestamp(12345), Seq(1, 2, 3))
new java.math.BigDecimal(1), Date.valueOf("1970-01-01"), new Timestamp(12345), Seq(1, 2, 3))
Seq(data).toDF().registerTempTable("reflectData")

assert(ctx.sql("SELECT * FROM reflectData").collect().head ===
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -324,20 +324,6 @@ class HiveQuerySuite extends HiveComparisonTest with BeforeAndAfter {
| FROM src LIMIT 1
""".stripMargin)

createQueryTest("Date comparison test 2",
"SELECT CAST(CAST(0 AS timestamp) AS date) > CAST(0 AS timestamp) FROM src LIMIT 1")

createQueryTest("Date cast",
"""
| SELECT
| CAST(CAST(0 AS timestamp) AS date),
| CAST(CAST(CAST(0 AS timestamp) AS date) AS string),
| CAST(0 AS timestamp),
| CAST(CAST(0 AS timestamp) AS string),
| CAST(CAST(CAST('1970-01-01 23:00:00' AS timestamp) AS date) AS timestamp)
| FROM src LIMIT 1
""".stripMargin)

createQueryTest("Simple Average",
"SELECT AVG(key) FROM src")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

package org.apache.spark.sql.hive.execution

import java.sql.{Date, Timestamp}

import org.apache.spark.sql._
import org.apache.spark.sql.catalyst.DefaultParserDialect
import org.apache.spark.sql.catalyst.analysis.EliminateSubQueries
import org.apache.spark.sql.catalyst.errors.DialectException
import org.apache.spark.sql._
import org.apache.spark.sql.hive.test.TestHive
import org.apache.spark.sql.hive.test.TestHive._
import org.apache.spark.sql.hive.test.TestHive.implicits._
Expand Down Expand Up @@ -962,4 +964,31 @@ class SQLQuerySuite extends QueryTest {
case None => // OK
}
}

test("SPARK-6785: HiveQuerySuite - Date comparison test 2") {
checkAnswer(
sql("SELECT CAST(CAST(0 AS timestamp) AS date) > CAST(0 AS timestamp) FROM src LIMIT 1"),
Row(false))
}

test("SPARK-6785: HiveQuerySuite - Date cast") {
// new Date(0) == 1970-01-01 00:00:00.0 GMT == 1969-12-31 16:00:00.0 PST
checkAnswer(
sql(
"""
| SELECT
| CAST(CAST(0 AS timestamp) AS date),
| CAST(CAST(CAST(0 AS timestamp) AS date) AS string),
| CAST(0 AS timestamp),
| CAST(CAST(0 AS timestamp) AS string),
| CAST(CAST(CAST('1970-01-01 23:00:00' AS timestamp) AS date) AS timestamp)
| FROM src LIMIT 1
""".stripMargin),
Row(
Date.valueOf("1969-12-31"),
String.valueOf("1969-12-31"),
Timestamp.valueOf("1969-12-31 16:00:00"),
String.valueOf("1969-12-31 16:00:00"),
Timestamp.valueOf("1970-01-01 00:00:00")))
}
}

0 comments on commit a451184

Please sign in to comment.