Skip to content

[Bug]: DATE() aborts the query on invalid date strings where MySQL 8 returns NULL #30537

Closed

Description

@iamlinjunhong

Is there an existing issue for the same bug?

Environment

  • MatrixOne commit: beffc64c486d369406a41cdb1447b120bc93cb41 (main, 2026-10-18), standalone mo-service started via ./mo-service -launch <launch.toml> (single CN/TN/log config; all ports shifted, mysql protocol on 31201).
  • OS: macOS 15.7 arm64 (Apple Silicon).
  • Reference: local temp instance of MySQL 8.0.46 (/opt/homebrew/opt/[email protected] homebrew), compared with identical client and identical SQL.

Actual Behavior

Every unparseable date string passed to the scalar DATE() function raises a query-killing error instead of producing NULL:

$ mysql -h127.0.0.1 -P31201 -udump -p ... -N
mysql> SELECT DATE('2026-02-30');
ERROR 1690 (22003) at line 1: data out of range: data type date, '2026-02-30'
mysql> SELECT DATE('2026-13-01');
ERROR 1690 (22003) at line 1: data out of range: data type date, '2026-13-01'
mysql> SELECT DATE('not a date');
ERROR 1690 (22003) at line 1: data out of range: data type date, 'not a date'

CAST(expr AS DATETIME) fails the same way for unparseable strings:

mysql> SELECT CAST('2026-02-30' AS DATETIME);
ERROR 20301 (HY000) at line 1: invalid input: invalid datetime value 2026-02-30
mysql> SELECT CAST('not a date' AS DATETIME);
ERROR 20301 (HY000) at line 1: invalid input: invalid datetime value not a date

The behavior is identical with SET SESSION sql_mode='' (non-strict) and under the default STRICT_TRANS_TABLES,NO_ZERO_DATE,... mode, so this is not a mode-gating issue.

Expected Behavior

MySQL 8.0.46 returns NULL (with warning 1292 "Incorrect date value") for the exact same statements, in every sql_mode:

$ mysql -h127.0.0.1 -P23206 -uroot -N --force < repro.sql
NULL   -- SELECT DATE('2026-02-30');
NULL   -- SELECT DATE('2026-13-01');
NULL   -- SELECT DATE('not a date');
NULL   -- SELECT CAST('2026-02-30' AS DATE);
NULL   -- SELECT CAST('2026-02-30' AS DATETIME);
NULL   -- SELECT CAST('not a date' AS DATETIME);
NULL   -- SELECT DATE_FORMAT('2026-02-30', '%Y-%m-%d');

MatrixOne's own CAST('2026-02-30' AS DATE) already returns NULL on the same build, so the NULL-return machinery exists in the cast layer; the scalar DATE() path and the DATETIME cast path just do not use it. This makes user queries such as SELECT ... WHERE DATE(dirty_json_or_text_col) = '2026-02-28' or SELECT CAST(ts_text AS DATETIME) fail wholesale on legacy/dirty data that MySQL handles row-wise with NULLs, and it also makes on-parity CLI/BVT suites diverge.

Steps to Reproduce

  1. Build standalone service from commit beffc64c486d369406a41cdb1447b120bc93cb41: make build then start ./mo-service -launch etc/launch/launch.toml (I shifted all ports to avoid collisions: mysql 31201, cn port-base 43200, tn port-base 44200, log 57200-57202, status 32201; no other config changes).
  2. Connect with any MySQL client: mysql -h127.0.0.1 -P31201 -udump -p111 --default-character-set=utf8mb4.
  3. Run the statements from the repro field (they are pure SELECTs, no schema needed). Every line errors on MatrixOne; every line returns NULL on MySQL 8.0.46.

Additional information

Root cause: DateStringToDate in pkg/sql/plan/function/func_unary.go (ParseDatetime call at line 1170, hard error at line 1172):

parsed, err := types.ParseDatetime(functionUtil.QuickBytesToStr(value), 6)
if err != nil {
    return moerr.NewOutOfRangeNoCtxf("date", "'%s'", value)
}

Any parse failure becomes a statement-fatal out-of-range error. The invalid-input error text seen for CAST(... AS DATETIME) is raised from pkg/container/types/datetime.go line 223 (moerr.NewInvalidInputNoCtxf("invalid datetime value %s", s)), which the CAST-to-DATETIME path surfaces to the user instead of returning NULL. By contrast the CAST-to-DATE path in pkg/sql/plan/function/func_cast.go already returns NULL for CAST('2026-02-30' AS DATE) and CAST('not a date' AS DATE) — verified on this build.

Recommended fix, keeping existing data/behavior compatible: make the scalar DATE() function and the CAST-to-DATETIME conversion append NULL for rows whose input string cannot be parsed (mirroring the CAST-to-DATE behavior), which matches MySQL 8.0 row-wise semantics. DATE_FORMAT('2026-02-30', '%Y-%m-%d') currently also errors (ERROR 20301 invalid input: invalid datetime value 2026-02-30 vs MySQL 2026-02-30) — the same parse call underlies it and should behave like MySQL: format what is parseable structurally and return the rendered string; if that part is considered covered by the open #30234 umbrella (zero/incomplete dates) it can be handled there separately.

Related history: issue #25306 ("date/time functions error on zero or invalid date strings instead of MySQL NULL/0 semantics") was closed on 2026-10-02, but its verification data contained only zero/incomplete dates and CONVERT_TZ-style cases; the scalar DATE() regression shown here is still reproducible on today's main (beffc64c486d), so this report tracks the residual path. Searches performed for dedup: date/time functions error, DATE invalid date, DATE('2026-02-30') — no open issue covers scalar DATE() on malformed calendar dates.

Full reproduction, exactly as Loom ran it
-- Run against MatrixOne (commit beffc64c486d) and against MySQL 8.0.46 with any mysql client.
-- MatrixOne: every statement 1-3 and 5-7 errors; MySQL 8.0.46: every statement returns NULL.

SELECT DATE('2026-02-30');
SELECT DATE('2026-13-01');
SELECT DATE('not a date');
SELECT CAST('2026-02-30' AS DATE);      -- NULL on BOTH engines (MatrixOne cast path is correct here)
SELECT CAST('2026-02-30' AS DATETIME);  -- MySQL: NULL | MatrixOne: ERROR 20301 invalid datetime value
SELECT CAST('not a date' AS DATETIME);  -- MySQL: NULL | MatrixOne: ERROR 20301 invalid datetime value
SELECT DATE_FORMAT('2026-02-30', '%Y-%m-%d'); -- MySQL: '2026-02-30' | MatrixOne: ERROR 20301

Found by Loom bug hunt (module: column-data; tests: unit, black-box, fuzzing)

Activity

  1. added
    kind/bug
    ai-easy
    on Oct 18, 2026
  2. Ariznawlll commented on Oct 20, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:官方 main
    • commit:7c146f4adbf068dd0c06b377157cfb7d7698b1eb
    • 修复:matrixone PR fix(sql): return NULL for invalid DATE and DATETIME expressions #30547,merge commit 8dd623e7d7338669f1ae2c63ef555f17ec93a5d8,已确认进入上述 main。
    • 部署方式:隔离构建的 macOS arm64 1 CN / 1 TN / 1 LogService,MySQL 协议端口 46001,独立数据目录。
    • 测试日期:2026-10-20(北京时间)。

    原问题验证

    三轮 SQL 实测:DATE('2026-02-30')、DATE('2026-13-01')、DATE('not a date')、CAST('2026-02-30' AS DATETIME) 都从原先的查询错误变为 NULL;协议结果中 DATE/DATETIME 类型分别为 10/12,IS NULL=1。默认严格模式、sql_mode='' 与显式 STRICT_TRANS_TABLES 的相应查询均返回 NULL。DATE_FORMAT('2026-02-30','%Y-%m-%d') 当前也返回 NULL。

    但原 issue 的 Expected Behavior 段明确要求 MySQL warning 1292。本轮在严格与非严格模式下对非法输入执行 SHOW WARNINGS,均没有记录;PR #30547 也明确写明未实现 1292 warning parity。此外,原 issue 的 Expected Behavior 列表把 DATE_FORMAT 预期写成 NULL,而末尾的补充段却写成字符串 '2026-02-30';两处期望相互矛盾,本轮不能把当前 NULL 行为宣称为该路径已完成 MySQL 一致性验收。

    Happy Path 覆盖

    1. DATE('2026-02-28') 三轮均为 2026-02-28,协议类型 DATE;DATE(NULL) 为 NULL。
    2. 混合行表中正常值、非法日期、垃圾文本及 SQL NULL 分别得到 2026-02-28/NULL/NULL/NULL,正常行的等值过滤 COUNT(*)=1,未因脏行整体中断。三轮测试库均清理。

    Unhappy Path 覆盖

    1. 不可能的日/月与垃圾输入均逐行返回 NULL,三轮无语句级错误。
    2. 对应非法输入的 warning 1292 未出现;此项是原报告所列预期,尚未通过。

    白盒验证

    TestDateStringToDate 及相关函数定向 Go 测试 -count=3 通过;未运行 -race。

    回归测试

    • MOTR #247 新增 issue_30537_invalid_date_null.test 与配对结果,覆盖非法/正常/NULL、混合行及两种 sql_mode;定向 3/3 通过(每次 13 条语句)。该回归只断言已确认的返回值和 NULL,不把缺失 warning 冒充为已通过。
    • 本轮未运行 MOTR 完整 suite,select without from clause is not supported #247 集成 CI 待确认。

    测试证据

    测试结论

    测试不通过。

  3. Ariznawlll commented on Oct 23, 2026

    @Ariznawlll
    Collaborator

    Still reproducible for the TIME target on today's main (3a1e42b430, 2026-10-23): the DATE/DATETIME cast paths now correctly return NULL for unparseable strings, but CAST(... AS TIME) still raises instead of returning NULL. Same root cause, one more target type the fix missed.

    SELECT CAST('abc' AS TIME);
    -- MatrixOne: ERROR 20301 (HY000) invalid input: invalid time value abc
    -- MySQL 8.0.45: NULL   (Warning 1292: Truncated incorrect time value: 'abc')
    
    SELECT CAST('' AS TIME);      -- MatrixOne: ERROR 20301 ;  MySQL: NULL
    SELECT CAST('not a time' AS TIME);  -- MatrixOne: ERROR ; MySQL: NULL

    The TIME column/type uses the same strict-parse path (the 20301 invalid time value error text) that DATE/DATETIME used to, but those two were relaxed while TIME was left strict. Adding TIME to the same NULL-on-unparseable-string handling closes the family.

  4. XuPeng-SH commented on Oct 29, 2026

    @XuPeng-SH
    Contributor

    QA handoff for merged #31746 (1c80f16a2bc2fd59d5ca5cadd8b5fb418f8b8244): this isolated QA run inspected the source and issue history but did not run current-head functional tests or live SQL because the existing resource guard left insufficient headroom. Please verify temporal warnings and row results, prepared/folding and multi-CN diagnostics. Author-reported PR tests are separate evidence and are not results of this QA run.

  5. Ariznawlll commented on Nov 2, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 运行实例:8.0.30-MatrixOne-v1.3.0,SQL 127.0.0.1:6001。
    • 测试日期:2026-11-02。

    原问题验证

    执行 DATE('2026-02-30') 和 DATE('not-a-date') 均返回 SQL NULL,不会中止查询;合法日期 DATE('2026-01-15') 正常返回日期值。

    Happy Path 覆盖

    1. 合法日期返回正确日期。
    2. 闰年日期 DATE('2024-02-29') 返回正确日期。
    3. DATE(NULL) 返回 NULL。
    4. MOTR 用例连续三轮运行,6/6 statements 通过。

    Unhappy Path 覆盖

    1. 不可能日期 2026-02-30 返回 NULL,不再抛出致命错误。
    2. 非日期字符串 not-a-date 返回 NULL。
    3. 非闰年日期 2023-02-29 返回 NULL。

    白盒验证

    本轮通过 SQL 黑盒验证 DATE() 的错误输入边界,未修改产品源码。

    回归测试

    已加入 matrixorigin/motr#304:issue_30537_date_invalid_returns_null。

    测试证据

    MOTR --repeat 3 成功,累计 6/6 statements 通过。

    测试结论

    测试通过。

  6. Ariznawlll commented on Nov 2, 2026

    @Ariznawlll
    Collaborator

    测试结论更正

    之前“测试通过”的结论和关闭操作过早,已重新打开 Issue。本轮实际运行的二进制构建 SHA 尚未确认,不能等同于最新官方 main。

    补测原报告中的输入:DATE('2026-13-01')、DATE('not a date')、无效值的 DATE/DATETIME CAST 返回 NULL;DATE_FORMAT('2026-02-30','%Y-%m-%d') 返回 NULL,而原报告的 MySQL 参考结果是 2026-02-30。需在确认版本的官方 main 上复核全部路径、SQL mode、混合行和相关语义,再给出验收结论。现有 MOTR 仅覆盖部分 DATE() 场景,尚未完成全套验收。

  7. Ariznawlll commented on Nov 3, 2026

    @Ariznawlll
    Collaborator

    结论

    测试通过。非法日期和时间文本在已测路径按行返回 NULL 并产生 1292 warning,不再中止整个查询。

    关键证据

    • 基于官方 main@d61a7206c16b30baa4efc525eb6067c4364035aa 的独立 MO;与隔离 MySQL 8.0.45 在空 SQL mode 和 STRICT_TRANS_TABLES 对照三轮。
    • **Happy case:**合法日期、闰日、NULL、混合表行与过滤结果正确;定向 MOTR SQL 两项共三轮 6/6。直接表达式与 SQL/binary prepared 复用在两种 SQL mode 下通过三轮。
    • **Unhappy case:**不可能的日期/月、垃圾文本及非法 TIME 返回 NULL,立即查询 SHOW WARNINGS 均为 1292;六类非法日期/时间结果和 warning 代码与 MySQL 8.0.45 的对照三轮一致。DATE_FORMAT('2026-02-30',...) 本轮两端均为 NULL;此前报告对该值的期望描述相互矛盾,本次不宣称验证了 MySQL 8.0.46。
    • 白盒:TestDateStringToDate、时间 warning 与 CAST warning 定向 Go 测试各运行三轮。相关 SQL、预编译和诊断回归已加入 MOTR #305;新增场景与其他目标场景联合三轮 18/18。

    待解决点

    MOTR PR 尚待评审合入;本轮 MySQL 对照版本是 8.0.45。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easykind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions