Skip to content

Commit f9705d4

Browse files
viiryarxin
authored andcommitted
[SPARK-7098][SQL] Make the WHERE clause with timestamp show consistent result
JIRA: https://issues.apache.org/jira/browse/SPARK-7098 The WHERE clause with timstamp shows inconsistent results. This pr fixes it. Author: Liang-Chi Hsieh <[email protected]> Closes apache#5682 from viirya/consistent_timestamp and squashes the following commits: 171445a [Liang-Chi Hsieh] Merge remote-tracking branch 'upstream/master' into consistent_timestamp 4e98520 [Liang-Chi Hsieh] Make the WHERE clause with timestamp show consistent result.
1 parent 6d0633e commit f9705d4

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/HiveTypeCoercion.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,10 @@ trait HiveTypeCoercion {
251251
p.makeCopy(Array(Cast(p.left, StringType), p.right))
252252
case p: BinaryComparison if p.left.dataType == StringType &&
253253
p.right.dataType == TimestampType =>
254-
p.makeCopy(Array(p.left, Cast(p.right, StringType)))
254+
p.makeCopy(Array(Cast(p.left, TimestampType), p.right))
255255
case p: BinaryComparison if p.left.dataType == TimestampType &&
256256
p.right.dataType == StringType =>
257-
p.makeCopy(Array(Cast(p.left, StringType), p.right))
257+
p.makeCopy(Array(p.left, Cast(p.right, TimestampType)))
258258
case p: BinaryComparison if p.left.dataType == TimestampType &&
259259
p.right.dataType == DateType =>
260260
p.makeCopy(Array(Cast(p.left, StringType), Cast(p.right, StringType)))
@@ -274,7 +274,7 @@ trait HiveTypeCoercion {
274274
i.makeCopy(Array(Cast(a, StringType), b))
275275
case i @ In(a, b) if a.dataType == TimestampType &&
276276
b.forall(_.dataType == StringType) =>
277-
i.makeCopy(Array(Cast(a, StringType), b))
277+
i.makeCopy(Array(a, b.map(Cast(_, TimestampType))))
278278
case i @ In(a, b) if a.dataType == DateType &&
279279
b.forall(_.dataType == TimestampType) =>
280280
i.makeCopy(Array(Cast(a, StringType), b.map(Cast(_, StringType))))

sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {
297297
}
298298

299299
test("SPARK-3173 Timestamp support in the parser") {
300+
checkAnswer(sql(
301+
"SELECT time FROM timestamps WHERE time='1969-12-31 16:00:00.0'"),
302+
Row(java.sql.Timestamp.valueOf("1969-12-31 16:00:00")))
303+
300304
checkAnswer(sql(
301305
"SELECT time FROM timestamps WHERE time=CAST('1969-12-31 16:00:00.001' AS TIMESTAMP)"),
302306
Row(java.sql.Timestamp.valueOf("1969-12-31 16:00:00.001")))

sql/core/src/test/scala/org/apache/spark/sql/TestData.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ object TestData {
175175
"4, D4, true, 2147483644" :: Nil)
176176

177177
case class TimestampField(time: Timestamp)
178-
val timestamps = TestSQLContext.sparkContext.parallelize((1 to 3).map { i =>
178+
val timestamps = TestSQLContext.sparkContext.parallelize((0 to 3).map { i =>
179179
TimestampField(new Timestamp(i))
180180
})
181181
timestamps.toDF().registerTempTable("timestamps")

0 commit comments

Comments
 (0)