Skip to content

[Compatibility]: DATE_ADD and DATE_SUB reject prepared value parameters #28119

Closed

Description

@Ariznawlll

Summary

DATE_ADD and DATE_SUB cannot be executed through a prepared statement when either the date operand or the INTERVAL value is a parameter marker. The same SQL succeeds with literals, and the related TIMESTAMPADD function accepts a parameter marker normally.

This prevents applications from binding ordinary runtime values to DATE_ADD/DATE_SUB even though the interval unit remains a SQL keyword.

Environment

  • MatrixOne official main: 99ed717b769e261b842c2f17345bd65865fc1470
  • Clean local single-CN build and isolated data directory
  • Repeated three times through both MySQL text prepared statements and the MySQL binary prepared-statement protocol

Reproduction

SELECT DATE_ADD('2024-02-28 23:59:59.123456', INTERVAL 1 DAY);
-- 2024-02-29 23:59:59.123456

PREPARE p_date FROM 'SELECT DATE_ADD(?, INTERVAL 1 DAY)';
SET @d = '2024-02-28 23:59:59.123456';
EXECUTE p_date USING @d;
-- ERROR 20203 (HY000): invalid argument date_add/date_sub function need two args, bad value 3

PREPARE p_interval FROM 'SELECT DATE_ADD(''2024-02-28 23:59:59.123456'', INTERVAL ? DAY)';
SET @n = 1;
EXECUTE p_interval USING @n;
-- ERROR 20203 (HY000): invalid argument date_add/date_sub function need two args, bad value 3

The same failure occurs for DATE_SUB. Parameterizing both operands fails as well.

Control

PREPARE p_timestampadd FROM
  'SELECT TIMESTAMPADD(DAY, ?, ''2024-02-28 23:59:59.123456'')';
SET @n = 1;
EXECUTE p_timestampadd USING @n;
-- 2024-02-29 23:59:59

The binary protocol has the same split: DATE_ADD(?, INTERVAL 1 DAY) and DATE_ADD(literal, INTERVAL ? DAY) both fail, while the binary prepared TIMESTAMPADD control succeeds. All outcomes were identical in 3/3 runs.

Expected behavior

Prepared execution should accept parameter markers in value positions, including the first DATE_ADD/DATE_SUB operand and the numeric expression after INTERVAL; the DAY unit remains static SQL syntax. MySQL documents INTERVAL expr unit as expression syntax and permits prepared-statement parameter markers where data values occur.

Preliminary code observation

The SQL is first rewritten from its two surface arguments into the internal three-argument form (date, interval-value, interval-unit). At prepared execution, the binder appears to re-enter the public date_add/date_sub validation and rejects that internal form with the observed “need two args, bad value 3” error. This is a diagnosis hypothesis, not a proposed fix.

Suggested regression coverage

Cover text and binary prepared statements for:

  1. DATE_ADD(?, INTERVAL 1 DAY).
  2. DATE_ADD(literal, INTERVAL ? DAY).
  3. DATE_SUB equivalents and repeated executions with different values.
  4. A TIMESTAMPADD prepared-statement control to preserve the distinct supported path.

Activity

  1. added
    kind/bug
    on Sep 4, 2026
  2. added this to the MO-202609 milestone on Sep 4, 2026
  3. added theissue type on Sep 4, 2026
  4. XuPeng-SH commented on Oct 23, 2026

    @XuPeng-SH
    Contributor

    Consolidation: #28135 reports the same prepared-expression arity failure through the ADDDATE/SUBDATE aliases. Both reports observe invalid argument date_add/date_sub function need two args, bad value 3 after the surface syntax is normalized to the internal three-argument form and revalidated as the public two-argument function. The suspected validation boundary is the same; this does not claim the code-level diagnosis is proven.

    Keep these alias cases in this issue's regression matrix:

    • ADDDATE(?, INTERVAL ? DAY) and SUBDATE(?, INTERVAL 1 DAY) through SQL PREPARE and the MySQL binary prepared protocol.
    • Literal controls, repeated executions with different values, and the existing DATE_ADD/DATE_SUB cases.

    The original reproductions and environment details remain in #28135. This issue is the single open tracker for the shared prepared DATE_ADD/DATE_SUB family; no fix is claimed by this consolidation.

  5. XuPeng-SH commented on Oct 23, 2026

    @XuPeng-SH
    Contributor

    Additional historical path from #28240: a prepared UPDATE that changes a base column used by a STORED or VIRTUAL DATE_ADD/DATE_SUB generated column reported the same internal need two args, bad value 3 error after rebinding the normalized expression. Keep this as a regression case alongside the direct prepared SELECT and ADDDATE/SUBDATE aliases.

    Status boundary: merged PR #28932 explicitly says the #28240 behavior was already present on its upstream base. I have not re-run that generated-column scenario on today's main, so this is historical regression coverage, not evidence that the path currently fails. #30194 remains separate: its prepared expression executes and the defect is result-schema/type metadata.

  6. XuPeng-SH commented on Nov 22, 2026

    @XuPeng-SH
    Contributor

    QA 交接:DATE_ADD/DATE_SUB 值位置的服务器预编译绑定

    #32002 已合入 main,合并 SHA d9a43b3156dd6ee54f14bdd4197071e522efd285,最终 PR head a2b88df5。

    较早基线与候选对照,日期参数、INTERVAL 数值参数、重复绑定与 NULL 通过;PR 将其作为已有路径的回归控制,未证明是本 PR 首次修复。 上述独立 QA 证据对应较早候选 005ea33b,不是当前合并版的实测。正式评审针对较早 head b1bc02f6,最终 head 的 Iceberg E2E 通过;合并队列记录未显示 Required 已满足。未在当前合并提交复跑,不推定 4.2 或整体 CI 通过。

    请验 SQL PREPARE 与二进制协议,覆盖 ADDDATE/SUBDATE 别名、重复绑定、预编译 UPDATE 触发的 STORED/VIRTUAL 生成列与 NULL;这些扩展不宣称已测。 转测不等于验收通过,工单保持开放。

  7. Ariznawlll commented on Nov 23, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    通过。在官方 main 测试开始时的最新提交 c8805c3e2613bcb53f279035fdf2e2337c86a68e 上,原始 DATE_ADD / DATE_SUB 预编译值参数场景及本 Issue 合并跟踪的别名、生成列更新场景均已通过。本次验证 main,不推定 4.2-dev 同样支持。

    关键证据

    • 环境:macOS arm64,隔离单机 MO(1 CN / 1 TN / 1 LogService);git_version() 和二进制源码版本均为上述完整 SHA。研发交接的 #32002 合并提交包含在此版本中;该交接指出该路径此前也可能已可工作,不将修复归因于此 PR。
    • Happy:SQL PREPARE 和 MySQL 二进制协议分别覆盖日期参数、INTERVAL 数值参数、两者同时参数化,DATE_ADD / DATE_SUB、ADDDATE / SUBDATE 以及同一预编译句柄重复绑定;跨闰日、0 天和微秒结果符合预期。原始 TIMESTAMPADD 文本预编译控制正常。
    • Unhappy:NULL 日期、NULL 间隔和非法日期返回 NULL;在同一预编译句柄上再次绑定有效值可以正常执行。对含 STORED DATE_ADD 和 VIRTUAL DATE_SUB 生成列的表,用文本和二进制预编译 UPDATE 修改日期及置 NULL,读取实际生成值与 NULL 传播均正确。
    • 白盒:检查日期函数参数归一化及计划绑定路径,执行 TestCalendarIntervalTypedBinding、TestCalendarIntervalPreparedTimeKeepsLegacyNormalizer,各重复 3 次通过;这些测试验证的是绑定控制,并非替代上述真实 SQL 执行。
    • 正式 MOTR runner 重复 3 次,3/3 通过、0 失败、0 跳过;回归已追加到同一个 MOTR PR #374,提交 0631f9b50e0bb5ca39d7ffbe1cdfd16720525875,只针对 main。新提交 CI 正在运行,尚不宣称其已通过。按用户要求,回归 PR 提交后先关闭,稍后合入。

    待解决点

    本次验收目标是参数绑定及计算值。个别字面量参数形式和别名在文本协议下返回 Python 字符串,测试按实际日期时间值比较;未据此宣称所有表达式的结果元数据都已验收,也不将其与本 Issue 的参数绑定问题混为一谈。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions