Github user cloud-fan commented on a diff in the pull request: https://github.com/apache/spark/pull/16138#discussion_r99527096 --- Diff: sql/core/src/test/scala/org/apache/spark/sql/DateFunctionsSuite.scala --- @@ -354,31 +354,58 @@ class DateFunctionsSuite extends QueryTest with SharedSQLContext { test("function to_date") { val d1 = Date.valueOf("2015-07-22") val d2 = Date.valueOf("2015-07-01") + val d3 = Date.valueOf("2014-12-31") val t1 = Timestamp.valueOf("2015-07-22 10:00:00") val t2 = Timestamp.valueOf("2014-12-31 23:59:59") + val t3 = Timestamp.valueOf("2014-12-31 23:59:59") val s1 = "2015-07-22 10:00:00" val s2 = "2014-12-31" - val df = Seq((d1, t1, s1), (d2, t2, s2)).toDF("d", "t", "s") + val s3 = "2014-31-12" + val df = Seq((d1, t1, s1), (d2, t2, s2), (d3, t3, s3)).toDF("d", "t", "s") checkAnswer( df.select(to_date(col("t"))), - Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2014-12-31")))) + Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2014-12-31")), + Row(Date.valueOf("2014-12-31")))) checkAnswer( df.select(to_date(col("d"))), - Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2015-07-01")))) + Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2015-07-01")), + Row(Date.valueOf("2014-12-31")))) checkAnswer( df.select(to_date(col("s"))), - Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2014-12-31")))) + Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2014-12-31")), Row(null))) checkAnswer( df.selectExpr("to_date(t)"), - Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2014-12-31")))) + Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2014-12-31")), + Row(Date.valueOf("2014-12-31")))) checkAnswer( df.selectExpr("to_date(d)"), - Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2015-07-01")))) + Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2015-07-01")), + Row(Date.valueOf("2014-12-31")))) checkAnswer( df.selectExpr("to_date(s)"), - Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2014-12-31")))) + Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2014-12-31")), Row(null))) + + // Now with format + checkAnswer( + df.select(to_date(col("t"), "yyyy-MM-dd")), + Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2014-12-31")), + Row(Date.valueOf("2014-12-31")))) + checkAnswer( + df.select(to_date(col("d"), "yyyy-MM-dd")), + Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2015-07-01")), + Row(Date.valueOf("2014-12-31")))) + checkAnswer( + df.select(to_date(col("s"), "yyyy-MM-dd")), + Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2014-12-31")), + Row(Date.valueOf("2016-07-12")))) + + // now switch format + checkAnswer( + df.select(to_date(col("s"), "yyyy-dd-MM")), + Seq(Row(Date.valueOf("2016-10-07")), Row(Date.valueOf("2016-07-12")), --- End diff -- I'm surprised, converting `2015-07-22 10:00:00` to date with format `yyyy-dd-MM` will result to `2016-10-07`? When we convert `2014-31-12` to date with default format, it returns null.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org