diff options
author | windpiger <songjun@outlook.com> | 2017-02-13 12:25:13 +0100 |
---|---|---|
committer | Herman van Hovell <hvanhovell@databricks.com> | 2017-02-13 12:25:13 +0100 |
commit | 04ad822534e8ded96a9ba4b7d43320e53c6d2808 (patch) | |
tree | 82d0d202e0782587345738a1746b1cfae3327ce1 /sql/catalyst | |
parent | 8f03ad547895b63825cff751265231b4fb75d660 (diff) | |
download | spark-04ad822534e8ded96a9ba4b7d43320e53c6d2808.tar.gz spark-04ad822534e8ded96a9ba4b7d43320e53c6d2808.tar.bz2 spark-04ad822534e8ded96a9ba4b7d43320e53c6d2808.zip |
[SPARK-19496][SQL] to_date udf to return null when input date is invalid
## What changes were proposed in this pull request?
Currently the udf `to_date` has different return value with an invalid date input.
```
SELECT to_date('2015-07-22', 'yyyy-dd-MM') -> return `2016-10-07`
SELECT to_date('2014-31-12') -> return null
```
As discussed in JIRA [SPARK-19496](https://issues.apache.org/jira/browse/SPARK-19496), we should return null in both situations when the input date is invalid
## How was this patch tested?
unit test added
Author: windpiger <songjun@outlook.com>
Closes #16870 from windpiger/to_date.
Diffstat (limited to 'sql/catalyst')
-rw-r--r-- | sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala index af70efbb0a..9e1de0fd2f 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala @@ -98,6 +98,10 @@ object DateTimeUtils { def newDateFormat(formatString: String, timeZone: TimeZone): DateFormat = { val sdf = new SimpleDateFormat(formatString, Locale.US) sdf.setTimeZone(timeZone) + // Enable strict parsing, if the input date/format is invalid, it will throw an exception. + // e.g. to parse invalid date '2016-13-12', or '2016-01-12' with invalid format 'yyyy-aa-dd', + // an exception will be throwed. + sdf.setLenient(false) sdf } |