Skip to content

JSON cast to CHAR silently truncates values beyond 65535 bytes #32374

Closed as duplicate

Description

@XuPeng-SH

Problem

Casting a JSON value larger than 65,535 bytes to CHAR silently truncates it. The result may no longer be valid JSON. This can corrupt applications that extract a JSON column as text.

Reproduced with 8.0.30-MatrixOne-v4.2.8. The same SQL on MySQL 8.0.46 preserves the full value.

Minimal reproduction

SET @chunk = REPEAT('a', 10000);
SET @payload = CONCAT(@chunk, @chunk, @chunk, @chunk, @chunk,
                      @chunk, @chunk, @chunk, @chunk, @chunk);
SELECT LENGTH(@payload) AS input_bytes,
       LENGTH(CAST(JSON_OBJECT('s', @payload) AS CHAR)) AS cast_bytes,
       JSON_VALID(CAST(JSON_OBJECT('s', @payload) AS CHAR)) AS valid_json;

Actual MatrixOne output: 100000 | 65535 | 0.

MySQL 8.0.46 output: 100000 | 100009 | 1 (the JSON serializer includes a space after the colon). The expected behavior is to preserve the entire value and produce valid JSON, or report an explicit error if a limit applies, rather than silently return truncated data.

This also reproduces for a stored JSON column read over the MySQL protocol, not just the JSON_OBJECT expression.

Workaround

Consumers that can decode the protocol's native JSON value can select the JSON column directly instead of casting it to CHAR. JSON_UNQUOTE is not a general replacement when callers need the original JSON representation of scalar strings.

Activity

  1. MatrixAdventurer commented on Nov 9, 2026

    @MatrixAdventurer
    Contributor

    分级建议:
    severity/s2 —— 超上限 JSON 经 CAST 提取落库被静默截断、无告警,正确性缺陷,但需 >64KB 负载且限于该提取写路径,触发面窄。
    模块:pkg/sql/plan/function/operator/cast.go(CastFromJsonToChar)及目标文本列写入。
    疑似重复:未发现同因;最接近 #31808 为长度上限边界类,不同缺陷。
    建议负责人:matrix-meow(现 assignee);目录 CODEOWNERS 为 XuPeng-SH/aunjgr/aptend。
    本操作由LW授权的MOI智能体提交

  2. Ariznawlll commented on Nov 9, 2026

    @Ariznawlll
    Collaborator

    结论

    测试不通过。当前 main 对超过 64 KiB 的 JSON 转 CHAR 仍静默截断并产生无效 JSON,复现与本 issue 同根。

    关键证据

    • 版本:main@c99234dcf4c94d1ff9d372917cd35041f1a3a682,隔离本地 CN,运行时 git_version() 为 c99234dcf4。
    • 3 次独立执行 CAST(JSON_OBJECT('s', REPEAT('a',100000)) AS CHAR),输入字符串长度均为 100000,转换结果均为 65535 字节且 JSON_VALID 为 0。
    • 长度边界探测中,65,534 字节字符串包入 JSON 后已产生 65,535 字节的截断结果且 JSON 无效;普通 CAST(REPEAT('x',100000) AS CHAR) 则返回 100000 字节。故此处观察到的是 JSON→CHAR 路径的独立限制/缺陷,不以普通字符串 CAST 结果替代 JSON 验收。

    待解决点

    • 本轮验证表达式路径;未重复 stored JSON 列经 MySQL 协议读取的变体。
  3. Ariznawlll commented on Nov 9, 2026

    @Ariznawlll
    Collaborator

    结论

    测试不通过。存储 JSON 列的直接协议读取保留完整值,但显式 JSON→CHAR 转换仍静默截断,与本 issue 同根。

    关键证据

    • 版本:main@c99234dcf4c94d1ff9d372917cd35041f1a3a682,隔离本地 CN,运行时 git_version() 为 c99234dcf4。
    • 3 个独立表各存储 JSON_OBJECT('s',REPEAT('a',100000))。直接 SELECT 通过 MySQL 协议解码后长度均为 100009 字节且 JSON_VALID(j)=1;CAST(j AS CHAR) 长度均为 65535 且 JSON_VALID(...)=0。
    • 首次使用 LENGTH(j) 被 MO 明确拒绝(JSON 参数类型不支持 LENGTH),该尝试未纳入结果;改用明确的 CAST(j AS CHAR) 后得到上述重复结论。

    待解决点

    • 此处验证本地 Python/MySQL 协议客户端;未复测其他语言驱动的 JSON 列类型映射。
  4. added
    severity/s0
    ai-easy
    and removed on Nov 9, 2026
  5. XuPeng-SH commented on Nov 20, 2026

    @XuPeng-SH
    ContributorAuthor

    Administratively consolidated into #32336. The reproduction and acceptance criteria remain tracked there. This does not claim that the defect is fixed or that every consolidated case has the same root cause. Assignee metadata is unchanged.

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

Metadata

Metadata

Assignees

Labels

ai-easyseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions