Skip to content

[Bug]: DATE/DATETIME/TIME use incorrect numeric coercion in bitwise and scalar functions #31904

Closed

Description

@MatrixAdventurer

问题描述

对 DATE/DATETIME/TIME 值用位运算符 &、|、^ 时,MO 不报错,而是把时间值当成二进制字符串按字节运算,返回一个 255 字节、末尾全是 \0 的二进制串,内容是被改了一个字节的日期文本(例如 '2024-01-01 10:00:00' | 1 得到 '3024-01-01 10:00:00\0\0...')。MySQL 先把时间值转成数字(20240101、20240101100000、10203),再按 BIGINT UNSIGNED 做位运算。

结果是静默的错误数据,客户端读到的是二进制垃圾值。<<、>> 在同样的输入上直接报 bad value [DATE BIGINT],两边行为也不一致。

环境

  • 分支:main(c32e75a2,2026-10-30)
  • 对照:MySQL 8.4.9

复现步骤

create table t (d date, dt datetime, tm time);
insert into t values ('2024-01-01', '2024-01-01 10:00:00', '01:02:03');
select d & 3, length(d & 3), hex(d & 3) from t;
select dt | 1, length(dt | 1) from t;
select tm ^ 1, length(tm ^ 1) from t;
select hex(date'2024-01-01' & 3), length(time'01:02:03' | 0);
select d << 1 from t;

实际结果(MO)

(用 PyMySQL 读取,截取前 24 字节)

d & 3           -> b'2\x00\x00\x00...'(255 字节)   length = 255   hex = 3200000000...(510 个十六进制字符)
dt | 1          -> b'3024-01-01 10:00:00\x00\x00\x00...'(255 字节)
tm ^ 1          -> b'\x011:02:03\x00\x00...'(255 字节)
hex(date'2024-01-01' & 3) -> 320000...;length(time'01:02:03' | 0) = 255
d << 1          -> ERROR 20203 (HY000): invalid argument operator <<, bad value [DATE BIGINT]

期望结果(MySQL 8.4.9)

d & 3  = 1                length = 1   hex = 1
dt | 1 = 20240101100001   length = 14
tm ^ 1 = 10202            length = 5
date'2024-01-01' & 3 = 1;time'01:02:03' | 0 = 10203
d << 1 = 40480202

本操作由 LW 授权的 Grok Bot 智能体执行。

Activity

  1. added
    kind/bug
    on Oct 30, 2026
  2. XuPeng-SH commented on Nov 9, 2026

    @XuPeng-SH
    Contributor

    Consolidating #31906 here under one numeric-coercion contract for DATE/DATETIME/TIME operands. Preserve both families: bitwise &, |, ^ must produce numeric results (not binary garbage), and numeric scalar consumers such as BIT_COUNT/SEC_TO_TIME must use MySQL temporal-to-number conversion, including warnings and TIME overflow behavior. These are distinct call paths and need separate assertions under the shared contract.

  3. changed the title [-][Bug]: bitwise & | ^ on DATE/DATETIME/TIME return 255-byte binary garbage instead of numeric results[/-] [+][Bug]: DATE/DATETIME/TIME use incorrect numeric coercion in bitwise and scalar functions[/+] on Nov 9, 2026
  4. added
    severity/s0
    ai-easy
    and removed on Nov 10, 2026
  5. gouhongshen commented on Nov 12, 2026

    @gouhongshen
    Contributor

    PR #32642 is trying to fix this issue.

  6. XuPeng-SH commented on Nov 17, 2026

    @XuPeng-SH
    Contributor

    转 QA 验证,非验收通过;本 issue 保持开放。

    关联修复 #32642 已于 2026-11-17 00:05 UTC 合入 main,merge SHA:e765c5fd44e842167d4271c37af6d25586511e16。已核对本 issue 完整评论、合并跟踪的 #31906 及 PR 修复范围/讨论,未发现当前适用的已验证反例或未完成开发 follow-up。

    作者报告的验证包括:temporal_numeric_consumer 57/57(两轮)、BIT_COUNT 210/210、SEC_TO_TIME 33/33 等相关 SQL BVT,完整 planner/function 包及原生 prepared 协议验证;最终 CI Required 已满足合入条件。本轮仅核对远端材料,未重复运行本地或部署测试。

    请 @Ariznawlll 在包含该 SHA 的版本上分别验证:

    边界:原生 Connector/Python timedelta 验证应使用纯编码器(use_pure=True);PR 说明可选 9.4.0 C 编码器会生成异常 TIME payload。既有 prepared BIT_COUNT DOUBLE 极值差异不在本次时间类型修复范围。若发现当前反例,请附精确版本、最小 SQL/协议参数及实际输出再转回开发。

  7. gouhongshen commented on Nov 17, 2026

    @gouhongshen
    Contributor

    PR #32642 has merged and is awaiting manual validation. The issue remains open.

  8. Ariznawlll commented on Nov 17, 2026

    @Ariznawlll
    Collaborator

    测试版本

    原问题及 SQL 契约

    • DATE/DATETIME/TIME 数值消费者的独立上游 temporal_numeric_consumer.test 已完整复跑:57/57 成功,0 失败。
    • 显式 SQL 与原生 prepared 的 TIME 数值域一致:01:02:03 -> 10203(BIT_COUNT=10,&3=3),02:50:03 -> 25003(BIT_COUNT=8,&3=3),00:00:00.500000 -> 1(BIT_COUNT=1,&3=1)。
    • 固定 UTC 写入的 TIMESTAMP 在 UTC / +08:00 / -05:00 分别得到 20240101100000/20240101180000/20240101050000;DATETIME 保持原数值。普通文本与 binary prepared 读取均一致。

    原生 prepared 重绑定

    同一 statement id 上按顺序验证 8 次绑定:

    1. DOUBLE 64.0 → BIT_COUNT=1;
    2. TIME 00:00:00.500000 → 1;
    3. DATETIME 2024-02-29 23:59:59.500000 → 20;
    4. 文本 2024-01-01 → 7,独立普通 cursor 执行 SHOW WARNINGS 得 1292;
    5. NULL → NULL,无 warning;
    6. 再次 DOUBLE → 1;
      7/8. 两组原生 TIME → 10 / 8。

    statement id 在整个序列中保持不变;另以 4 组原生 TIME 参数及显式 SQL 对照核对,避免客户端编码错误成为 oracle。

    范围归类与白盒

    fresh prepared cursor 首次绑定文本 2024-01-01 得到 BIT_COUNT=29、无 warning,是 prepared 初始参数采用 binary overload、对十个 ASCII 字节计位的既有契约;数值 latch 后的文本重绑定才得到 7 + warning 1292。核对了 pkg/sql/plan/prepared_binding.go、pkg/frontend/prepared_execution.go、pkg/frontend/computation_wrapper.go 及 function 定义;该 fresh-text 对照早于本次 temporal 修复且已有测试固定,不作为本 issue 反例。此前手工 TIME oracle 的误报已撤回。

    回归测试

    已追加 matrixorigin/motr#349:14_issue_regression.issue_31904_temporal_numeric_protocol,需 MOTR_MYSQL_CONNECTOR_TESTS=1 显式启用,覆盖 latch 重绑定、warning、TIME、TIMESTAMP 及 fresh-text 独立对照。

    新增提交 01ba5603bedc5f1bcb82ac91c0d1635fa59b27db;本批四用例录制后独立复跑,显式启用协议用例后 --repeat 3 合计 12/12 用例、159/159 调度语句通过,零 SKIP;PR 新 head 的 4 项 CI 全部 SUCCESS。

    测试结论

    本 issue 及合并跟踪的 #31906 范围内验证通过,关闭本 issue。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bugseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions