Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ object TypeCoercion {
// We should cast all relative timestamp/date/string comparison into string comparisons
// This behaves as a user would expect because timestamp strings sort lexicographically.
// i.e. TimeStamp(2013-01-01 00:00 ...) < "2014" = true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks we should update this comments too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks for pointing it out

case (StringType, DateType) => Some(StringType)
case (DateType, StringType) => Some(StringType)
case (StringType, DateType) => Some(DateType)
case (DateType, StringType) => Some(DateType)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this mean we always find the common type as date when any arbitrary strings are compared to any dates?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it there any issue in your opinion?

case (StringType, TimestampType) => Some(StringType)
case (TimestampType, StringType) => Some(StringType)
case (StringType, NullType) => Some(StringType)
Expand Down
14 changes: 14 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3024,6 +3024,20 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext {
sql("reset")
}
}

test("string date comparison") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add test cases for <=>, =

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

spark.range(1).selectExpr("date '2000-01-01' as d").createOrReplaceTempView("t1")
val result = Date.valueOf("2000-01-01")
checkAnswer(sql("select * from t1 where d >= '2000'"), Row(result))
checkAnswer(sql("select * from t1 where d >= '2000-1'"), Row(result))
checkAnswer(sql("select * from t1 where d >= '2000-1-1'"), Row(result))
checkAnswer(sql("select * from t1 where d >= '2000-1-01'"), Row(result))
checkAnswer(sql("select * from t1 where d >= '2000-01-1'"), Row(result))
checkAnswer(sql("select * from t1 where d >= '2000-01-01'"), Row(result))
checkAnswer(sql("select * from t1 where d > '2000-01-01'"), Nil)
checkAnswer(sql("select * from t1 where '2000' >= d"), Row(result))
checkAnswer(sql("select * from t1 where d > '2000-13'"), Nil)
}
}

case class Foo(bar: Option[String])