From f334474802d22cc40e38476091a68e5135d4b03f Mon Sep 17 00:00:00 2001 From: Alexandru Diaconu Date: Sun, 15 Mar 2026 12:42:54 +0200 Subject: [PATCH] MDEV-38792 TO_DATE: Inconsistent treatment of separators Fixed a bug where TO_DATE (Oracle mode) returned NULL if the input string had extra separators (like '-') not defined in the format. Added a loop to skip non-alphanumeric characters, making the data parsing just as flexible as the format parsing. Signed-off-by: Alexandru Diaconu --- mysql-test/main/mdev-38792.result | 10 ++++++++++ mysql-test/main/mdev-38792.test | 11 +++++++++++ sql/item_timefunc.cc | 15 +++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 mysql-test/main/mdev-38792.result create mode 100644 mysql-test/main/mdev-38792.test diff --git a/mysql-test/main/mdev-38792.result b/mysql-test/main/mdev-38792.result new file mode 100644 index 0000000000000..31e07d8cd1223 --- /dev/null +++ b/mysql-test/main/mdev-38792.result @@ -0,0 +1,10 @@ +SET sql_mode = 'ORACLE'; +SELECT TO_DATE('2002 AD', 'YYYY - AD') FROM DUAL; +TO_DATE('2002 AD', 'YYYY - AD') +2002-03-15 00:00:00 +SELECT TO_DATE('2002 - AD', 'YYYY AD') FROM DUAL; +TO_DATE('2002 - AD', 'YYYY AD') +2002-03-15 00:00:00 +SELECT TO_DATE('2002 - AD', 'YYYY-AD') FROM DUAL; +TO_DATE('2002 - AD', 'YYYY-AD') +2002-03-15 00:00:00 diff --git a/mysql-test/main/mdev-38792.test b/mysql-test/main/mdev-38792.test new file mode 100644 index 0000000000000..0396c4baec7ba --- /dev/null +++ b/mysql-test/main/mdev-38792.test @@ -0,0 +1,11 @@ +# TO_DATE: Inconsistent treatment of different separators in the date and format strings +SET sql_mode = 'ORACLE'; + +# case 1: +SELECT TO_DATE('2002 AD', 'YYYY - AD') FROM DUAL; + +# case 2: The fixed bug: +SELECT TO_DATE('2002 - AD', 'YYYY AD') FROM DUAL; + +# case 3: +SELECT TO_DATE('2002 - AD', 'YYYY-AD') FROM DUAL; diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc index c008c2089ba29..b3e9e42a8564e 100644 --- a/sql/item_timefunc.cc +++ b/sql/item_timefunc.cc @@ -721,6 +721,21 @@ extract_oracle_date_time(THD *thd, uint16 *format_ptr, continue; } + /* + MDEV-38792: TO_DATE: Inconsistent treatment of different separators in the date and format strings. + Skip punctuation and spaces in the input string before matching the next + format token, but avoid skipping a '-' sign if it's followed by a digit + (as it might be part of a negative year in SYYYY). + */ + while (val < val_end && !my_isalnum(val_cs, *val)) + { + if (*val == '-' && (val + 1 < val_end) && my_isdigit(val_cs, *(val + 1))) + { + break; + } + val++; + } + error= 0; if (!(val_len= (uint) (val_end - val))) goto error;