Skip to content

Commit

Permalink
special handling of equality
Browse files Browse the repository at this point in the history
  • Loading branch information
marmbrus committed Jun 19, 2015
1 parent 1172c60 commit 1f09adf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,16 @@ trait HiveTypeCoercion {
case a @ BinaryArithmetic(left, right @ StringType()) =>
a.makeCopy(Array(left, Cast(right, DoubleType)))

// We should cast all timestamp/date/string compare into string comparisions
// For equality between string and timestamp we cast the string to a timestamp
// so that things like rounding of subsecond precision does not affect the comparison.
case p @ Equality(left @ StringType(), right @ TimestampType()) =>
p.makeCopy(Array(Cast(left, TimestampType), right))
case p @ Equality(left @ TimestampType(), right @ StringType()) =>
p.makeCopy(Array(left, Cast(right, TimestampType)))

// We should cast all relative timestamp/date/string comparison into string comparisions
// This behaves as a user would expect because timestamp strings sort lexicographically.
// i.e. TimeStamp(2013-01-01 00:00 ...) < "2014" = true
case p @ BinaryComparison(left @ StringType(), right @ DateType()) =>
p.makeCopy(Array(left, Cast(right, StringType)))
case p @ BinaryComparison(left @ DateType(), right @ StringType()) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ private[sql] object BinaryComparison {
def unapply(e: BinaryComparison): Option[(Expression, Expression)] = Some((e.left, e.right))
}

/** An extractor that matches both standard 3VL equality and null-safe equality. */
private[sql] object Equality {
def unapply(e: BinaryComparison): Option[(Expression, Expression)] = e match {
case EqualTo(l, r) => Some((l, r))
case EqualNullSafe(l, r) => Some((l, r))
case _ => None
}
}

case class EqualTo(left: Expression, right: Expression) extends BinaryComparison {
override def symbol: String = "="

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll with SQLTestUtils {
(0 to 3).map(i => Tuple1(new Timestamp(i))).toDF("time").registerTempTable("timestamps")

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

checkAnswer(sql(
Expand Down

0 comments on commit 1f09adf

Please sign in to comment.