Skip to content

Commit 1f09adf

Browse files
committed
special handling of equality
1 parent 1172c60 commit 1f09adf

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,16 @@ trait HiveTypeCoercion {
277277
case a @ BinaryArithmetic(left, right @ StringType()) =>
278278
a.makeCopy(Array(left, Cast(right, DoubleType)))
279279

280-
// We should cast all timestamp/date/string compare into string comparisions
280+
// For equality between string and timestamp we cast the string to a timestamp
281+
// so that things like rounding of subsecond precision does not affect the comparison.
282+
case p @ Equality(left @ StringType(), right @ TimestampType()) =>
283+
p.makeCopy(Array(Cast(left, TimestampType), right))
284+
case p @ Equality(left @ TimestampType(), right @ StringType()) =>
285+
p.makeCopy(Array(left, Cast(right, TimestampType)))
286+
287+
// We should cast all relative timestamp/date/string comparison into string comparisions
281288
// This behaves as a user would expect because timestamp strings sort lexicographically.
289+
// i.e. TimeStamp(2013-01-01 00:00 ...) < "2014" = true
282290
case p @ BinaryComparison(left @ StringType(), right @ DateType()) =>
283291
p.makeCopy(Array(left, Cast(right, StringType)))
284292
case p @ BinaryComparison(left @ DateType(), right @ StringType()) =>

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ private[sql] object BinaryComparison {
266266
def unapply(e: BinaryComparison): Option[(Expression, Expression)] = Some((e.left, e.right))
267267
}
268268

269+
/** An extractor that matches both standard 3VL equality and null-safe equality. */
270+
private[sql] object Equality {
271+
def unapply(e: BinaryComparison): Option[(Expression, Expression)] = e match {
272+
case EqualTo(l, r) => Some((l, r))
273+
case EqualNullSafe(l, r) => Some((l, r))
274+
case _ => None
275+
}
276+
}
277+
269278
case class EqualTo(left: Expression, right: Expression) extends BinaryComparison {
270279
override def symbol: String = "="
271280

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll with SQLTestUtils {
350350
(0 to 3).map(i => Tuple1(new Timestamp(i))).toDF("time").registerTempTable("timestamps")
351351

352352
checkAnswer(sql(
353-
"SELECT time FROM timestamps WHERE time='1969-12-31 16:00:00'"),
353+
"SELECT time FROM timestamps WHERE time='1969-12-31 16:00:00.0'"),
354354
Row(java.sql.Timestamp.valueOf("1969-12-31 16:00:00")))
355355

356356
checkAnswer(sql(

0 commit comments

Comments
 (0)