Skip to content

Conversation

@qidaye
Copy link
Contributor

@qidaye qidaye commented Sep 15, 2020

Proposed changes

  1. add date range check in DateLiteral for FEFunctions
  2. select str_to_date(202009,'%Y%m') and select str_to_date(str,'%Y%m') from tb where tb.str = '202009' will return same output 2020-09-00.
  3. add support of zero-date to function str_to_date(),date_format()
  4. fix FE can calculate negative value bug, eg: select str_to_date('-2020', '%Y') will return NULL instead of date value.

current behavior is same as MySQL without sql_mode NO_ZERO_IN_DATE and NO_ZERO_DATE.

current behavior

mysql> select siteid,str_to_date(siteid,'%Y%m%d') from table2  order by siteid;
+------------+---------------------------------+
| siteid     | str_to_date(`siteid`, '%Y%m%d') |
+------------+---------------------------------+
|          1 | 2001-00-00                      |
|          2 | 2002-00-00                      |
|          2 | 2002-00-00                      |
|          3 | 2003-00-00                      |
|          4 | 2004-00-00                      |
|          5 | 2005-00-00                      |
|         20 | 2020-00-00                      |
|        202 | 0202-00-00                      |
|       2020 | 2020-00-00                      |
|      20209 | 2020-09-00                      |
|     202008 | 2020-08-00                      |
|     202009 | 2020-09-00                      |
|    2020009 | 2020-00-09                      |
|   20200009 | 2020-00-09                      |
|   20201309 | NULL                            |
| 2020090909 | 2020-09-09                      |
+------------+---------------------------------+

mysql> select str_to_date('2','%Y%m%d'),str_to_date('20','%Y%m%d'),str_to_date('202','%Y%m%d'),str_to_date('2020','%Y%m%d'),str_to_date('20209','%Y%m%d'),str_to_date('202009','%Y%m%d'),str_to_date('2020099','%Y%m%d'),str_to_date('20200909','%Y%m%d'),str_to_date('2020090909','%Y%m%d'),str_to_date('2020009','%Y%m%d'),str_to_date('20200009','%Y%m%d'),str_to_date('20201309','%Y%m%d');
+----------------------------+-----------------------------+------------------------------+-------------------------------+--------------------------------+---------------------------------+----------------------------------+-----------------------------------+-------------------------------------+----------------------------------+-----------------------------------+-----------------------------------+
| str_to_date('2', '%Y%m%d') | str_to_date('20', '%Y%m%d') | str_to_date('202', '%Y%m%d') | str_to_date('2020', '%Y%m%d') | str_to_date('20209', '%Y%m%d') | str_to_date('202009', '%Y%m%d') | str_to_date('2020099', '%Y%m%d') | str_to_date('20200909', '%Y%m%d') | str_to_date('2020090909', '%Y%m%d') | str_to_date('2020009', '%Y%m%d') | str_to_date('20200009', '%Y%m%d') | str_to_date('20201309', '%Y%m%d') |
+----------------------------+-----------------------------+------------------------------+-------------------------------+--------------------------------+---------------------------------+----------------------------------+-----------------------------------+-------------------------------------+----------------------------------+-----------------------------------+-----------------------------------+
| 2002-00-00                 | 2020-00-00                  | 0202-00-00                   | 2020-00-00                    | 2020-09-00                     | 2020-09-00                      | 2020-09-09                       | 2020-09-09                        | 2020-09-09                          | 2020-00-09                       | 2020-00-09                        | NULL                              |
+----------------------------+-----------------------------+------------------------------+-------------------------------+--------------------------------+---------------------------------+----------------------------------+-----------------------------------+-------------------------------------+----------------------------------+-----------------------------------+-----------------------------------+

fix #4494

Types of changes

  • Bugfix (non-breaking change which fixes an issue)

Checklist

  • I have create an issue on (Fix #ISSUE), and have described the bug/feature there in detail
  • Compiling and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • If this change need a document change, I have updated the document
  • Any dependent changes have been merged

@qidaye
Copy link
Contributor Author

qidaye commented Sep 15, 2020

date_format() behavior

mysql> select date_format('20201300','%Y.%m.%d'),date_format('20200000','%Y.%m.%d');
+-------------------------------------+-------------------------------------+
| date_format('20201300', '%Y.%m.%d') | date_format('20200000', '%Y.%m.%d') |
+-------------------------------------+-------------------------------------+
| NULL                                | 2020.00.00                          |
+-------------------------------------+-------------------------------------+

@morningman morningman added area/sql/function Issues or PRs related to the SQL functions good first issue kind/fix Categorizes issue or PR as related to a bug. labels Sep 16, 2020
@morningman morningman self-assigned this Sep 16, 2020
Copy link
Contributor

@morningman morningman left a comment

Choose a reason for hiding this comment

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

LGTM

@morningman morningman merged commit 065b979 into apache:master Sep 17, 2020
@qidaye qidaye deleted the str_to_date branch September 17, 2020 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/sql/function Issues or PRs related to the SQL functions good first issue kind/fix Categorizes issue or PR as related to a bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] function str_to_date()'s behavior on BE and FE is inconsistent

2 participants