Skip to content

[Bug]: MODUMP QUERY_RESULT exports BIT as decimal text, causing wrong values after LOAD DATA round-trip #30653

Closed

Description

@Ariznawlll

问题

结果集 MODUMP QUERY_RESULT 导出 BIT 列后,再用 LOAD DATA 导入相同表结构,可能成功但改变数据:BIT(8) 的值 1 变成 49。较大的值则导入失败。

同一张源表通过普通 SELECT ... INTO OUTFILE 导出、导入结果正确。这里是 SQL 结果集 MODUMP,不是独立 mo-dump 命令。

环境

  • main:3829ee49c7c118f2f3b0ebf7ed3acbd52483d870
  • 本地隔离集群,DISK-V2;2026-10-20
  • 三套新表/新文件分别验证,均复现

最小复现

mysql 客户端需加 --comments,保留 /* save_result */。

CREATE DATABASE modump_bit_repro;
USE modump_bit_repro;
CREATE TABLE src(id INT PRIMARY KEY, b BIT(8));
INSERT INTO src VALUES(1,1);
CREATE TABLE dst LIKE src;

SET save_query_result=on;
/* save_result */ SELECT id,b FROM src;
SELECT last_query_id();

把上一步返回的 query ID 填入以下语句:

MODUMP QUERY_RESULT '<query ID>'
INTO '/tmp/modump_bit_repro.csv' HEADER 'false';

LOAD DATA INFILE '/tmp/modump_bit_repro.csv'
INTO TABLE dst FIELDS TERMINATED BY ',';

SELECT id,CAST(b AS UNSIGNED) FROM src;
-- 1, 1
SELECT id,CAST(b AS UNSIGNED) FROM dst;
-- 1, 49:导出、导入均成功,但值改变

正常对照:

CREATE TABLE control LIKE src;
SELECT * FROM src INTO OUTFILE '/tmp/modump_bit_control.csv' HEADER 'false';
LOAD DATA INFILE '/tmp/modump_bit_control.csv'
INTO TABLE control FIELDS TERMINATED BY ',';
SELECT id,CAST(b AS UNSIGNED) FROM control;
-- 1, 1:正确

结果与边界

  • 1 → 49 在 3 套独立源表、结果集、导出文件和目标表中均复现,3/3。
  • 同样 3 轮普通 OUTFILE 对照,含 BIT(8) 的 1/65/255/NULL,导入后逐行一致。
  • MODUMP 导出上述混合值后,LOAD 报 BIT 宽度溢出,目标表保持空;BIT(64) 最大值则报 data too long, len(val) = 20。
  • 错误后的连接仍能执行 SELECT 1。

代码线索

exportDataFromResultSetToCSVFile 的 MYSQL_TYPE_BIT 分支 调用 GetString,输出十进制文本;最小例子的文件内容为 1,1\n。

而 LOAD 的 T_bit 分支 按原始字节构造位值,因此 ASCII 1 被解释为 0x31 = 49。普通 OUTFILE 的 T_bit 分支 则输出对应宽度的原始字节,与 LOAD 一致。

预期同类型导出/导入应保持 BIT 值不变,不能静默改变数据。已查兼容性文档和 open/closed issue/PR,未发现该约定或同问题;#30557 是独立 mo-dump SQL 引号字节未转义,路径及故障不同。

Activity

  1. added this to the MO-202610 milestone on Oct 20, 2026
  2. added
    kind/bug
    on Oct 20, 2026
  3. added
    severity/s1
    on Oct 20, 2026
  4. added theissue type on Oct 20, 2026
  5. MatrixAdventurer commented on Oct 20, 2026

    @MatrixAdventurer
    Contributor

    由 LW 授权的 ChatGPT Personal 智能体提交。

    修复 PR 已提交:#30655(目标 main;上游没有 dev,按用户授权使用 main)。
    固定 head:8b23b474564c7b2d700e2fca46160ee5fcd69e31;基线:743e749a1e33d6c845bb73999e8c82d297a2b657。最终3文件,+171/-2,共173行,含回归测试。

    实际红/绿验证:

    • 将最终两组回归放到未修改生产源码的基线上,TestResultSetCSVBitEncoding 与 TestSavedQueryResultBitExport 均实际失败;前者30个非NULL数值案例输出十进制文本,后者真实保存结果导出文件字节不一致,NULL对照通过。
    • 修复后两组回归实际通过;保留BIT位宽元数据,MODUMP采用普通OUTFILE的原始字节编码。
    • 本地完整frontend测试与未修改基线均在相同12组既有view-schema测试失败,原因是沙箱无法从/proc获取内存上限;没有将该结果视为通过或跳过测试。
    • 已在用户fork的隔离Ubuntu24.04/Go1.27.1环境解决验证阻塞:run39389359284,job118516862335全部成功:make cgo、frontend build、vet、两组回归、完整frontend包测试(9.873s)。
    • 独立代码审查明确Approve,无material findings;包含Q1/Q2/Q3资源与生命周期审查。额外只读验证workflow也经独立Approve,只存在验证分支,不进入最终3文件PR。
    • 此修复恢复与普通OUTFILE的BIT编码一致性,不声称解决任意二进制值的通用CSV delimiter/enclosure问题,也不声称完整上游CI已通过。

    操作记录:上述智能体核实MatrixAdventurer本人fork/上游关系和写权限,创建候选commit、verify/modump-bit-30653验证分支及fix/modump-bit-bytes-30653修复分支。Issue与PR均已添加指派XuPeng-SH,PR审阅请求包含XuPeng-SH,已通过GitHub读取返回值再次确认;保留原Issue已有负责人matrix-meow。PR读回为3文件+171/-2、无冲突,完整上游CI正在运行。没有推送上游、合并、部署或关闭Issue。完整命令、固定提交和证据见PR正文。

  6. Ariznawlll commented on Oct 21, 2026

    @Ariznawlll
    CollaboratorAuthor

    同根输出格式错配还影响另一条结果导出路径:SELECT ... INTO OUTFILE ... FORMAT 'jsonline' 将 BIT(8) 作为十进制 JSON 数字写出,但 JSONLINE LOAD DATA 仍按字段原始字节构造 BIT。3 个全新数据库中,源 BIT(8)=1 导出为 {"b":1,"id":1},单行回导成功却得到 BIT(8)=49(3/3);含 1/44/255/NULL 的文件回导在 44 处报宽度错误,目标表保持空(3/3)。精确 SQL/输出:/private/tmp/mo-sequence-run2-20261020/ten-bugs-1021/jsonline-bit-repeat.jsonl。

    这与本 issue 的 MODUMP CSV 路径一样,是十进制文本表达与 BIT LOAD 原始字节语义不匹配;不再单独提 bug。另发现普通 CSV OUTFILE 的 BIT 字节若恰好是分隔符,未加引号时会静默回导成 0;该边界已补到 #30702,因为它属于 CSV 未转义分隔符的根因。修复/回归时请分别验证 JSONLINE 数字和 CSV 原始字节两种表示。

  7. MatrixAdventurer commented on Oct 21, 2026

    @MatrixAdventurer
    Contributor

    由LW授权的MOI智能体提交。

    @Ariznawlll 您在 #30655 中报告的三条 jsonline 复现(值 1 可载入但回读异常、值 44 报宽度错误且无部分载入、NULL 无法复现)已完成代码级静态复核,代码读取锚定 #30655 头节点 00f0b211308bde5a24de621e2c1c74a00b0a7d7e,结论:问题成立,且是与 CSV 不同的独立根因——jsonline 导入路径复用了 "T_bit 按原始字节逐字节解析" 的 CSV 语义,而 jsonline 导出侧把 BIT 写成十进制数字字面量,两种表示不兼容。CSV 原始字节语义不受影响,#30655 的既有回归仍然有效。

    依据:

    1. 导出侧 pkg/frontend/export.go vectorValueToJSON() L1364-L1366:case types.T_bit: → val := vector.GetFixedAtNoTypeCheck[uint64](vec, i) → 返回 uint64,经 json.Marshal 输出十进制数字。因此 BIT(8) 的 1 / 44 在 jsonline 中写为数字字面量 1 / 44。
    2. 导入侧 pkg/sql/colexec/external/external.go:getColData()(L1931 起,case types.T_bit: L2026 起)对 T_bit 执行 if len(field.Val) > 8 → data too long,随后 val = (val << 8) | uint64(field.Val[i]) 逐字节累加;isLegalLine()(L1053 起,L1084 起)同语义做严格预检,val > (1<<width)-1 判非法。L1952-L1955 注释明确写着 "T_bit carries raw bytes (parsed byte-by-byte, not as text)"——该语义对导出原始字节的 CSV 是正确的,但对 jsonline 的数字 token 是错的。
    3. 现象映射(与您的运行结果一致):
      • 值 1:JSON 数字 token 为 1(单字节 0x31)→ 按原始字节解释 → 回读 49;
      • 值 44:token 44 为 2 字节 0x34 0x34 → 逐字节值 0x3434=13364 > 255 → 宽度错误拒绝行、目标表为空(与"无部分载入"一致);
      • 值 NULL:JSON null → field.IsNull → 正常回读 NULL,因此不构成复现(您的观察正确)。
      • 附一条静态推论(未新跑运行验证):09 之外的单字节数字以外的值因 token ≥2 字节全部会报宽度错误;09 能载入但回读为对应 ASCII 码 48~57(如 9→57),与 1→49 同构。

    修复方向(后续单独补丁将实施并引用本评论):

    • 最小修复在 jsonline 导入侧的 T_bit 分支:当目标列为 T_bit 且字段为非引号 JSON 数字 token 时,按十进制解析为 uint64,再按列宽以大端字节序编码写入;CSV 原始字节路径保持不变。导出侧不改动——十进制数字是合法 JSON 且与现有文件兼容。
    • 回归测试按您的建议同时覆盖两种表示:jsonline 数字表示(0、1、9、44、255、NULL,BIT(1)/BIT(8)/BIT(64) 宽度边界,44 报错且无部分载入的目标行为)+ CSV 原始字节表示回读(沿用 fix(frontend): preserve raw BIT bytes in MODUMP query results #30655 已有断言)。

    本轮状态: 当前执行环境不具备代码推送通道(沙箱无法 push 到 fork),本轮仅完成定位与方案登记;补丁交付时将附回归证据并请求复核。CSV 修复与 #30655 的 CI 状态不受影响。

  8. MatrixAdventurer commented on Oct 21, 2026

    @MatrixAdventurer
    Contributor

    [Comment submitted by the MOI dev/test agent, authorized by LW]

    Follow-up on the remaining JSONLINE half of this issue (the CSV export half was fixed by #30655).

    Root cause (jsonline representation)

    • Export side: result-set export to JSONLINE writes BIT as a JSON number ({"b":1,"id":1}) — a valid textual representation of the value.
    • Load side: jsonLineScalarText (sole definition in pkg/sql/colexec/external/reader_csv.go, shared by jsondata='object' and 'array') passes the JSON scalar's text through to BIT construction with the same byte-oriented semantics as the CSV LOAD T_bit branch (cf. pkg/sql/colexec/external/external.go:2025). So the text "1" is interpreted as byte 0x31 → 49, and "44"/"255" exceed the 1-byte width and error out — matching the 3/3 repro reported above.

    The two file formats therefore disagree on representation: CSV now carries raw bytes (consistent after #30655), while JSONLINE carries a decimal number that LOAD still interprets as raw bytes.

    Suggested minimal fix (load side only)

    Give BIT columns numeric semantics in the jsonline scalar → column-value conversion:

    case types.T_bit:
        v, err := strconv.ParseUint(strings.TrimSpace(s), 10, 64)
        if err != nil || v >= 1<<colWidth { /* invalid / out-of-range for BIT(width) */ }
        b := types.EncodeUint64(&v)[:byteWidth]
        slices.Reverse(b) // big-endian raw bytes, mirroring the post-#30655 export encoding

    Both requested representations then round-trip correctly without touching the exporter or the CSV byte path:

    (An alternative — exporting BIT as {"$binary":"<base64 raw bytes>"} like #30663 does for the binary family — is byte-safe for embedded zeros but changes on-disk compatibility and needs both export and load changes; the numeric-semantics fix above is the smaller change for what is reported here.)

    Regression plan (red → green)

    1. BIT(8) rows {NULL, b'1', b'0101100' (=44), b'11111111' (=255)} exported via INTO OUTFILE ... FORMAT 'jsonline' and loaded back via LOAD DATA ... FORMAT='jsonline': currently red (1→49; 44/255 width error); after the fix all rows should round-trip identically.
    2. Same corpus with jsondata='array' (shared scalar path).
    3. Re-run the CSV raw-byte round-trip (post-fix(frontend): preserve raw BIT bytes in MODUMP query results #30655) as a guard against regressing the shared byte construction.
    4. MODUMP QUERY_RESULT jsonline variant of the original repro, end to end.

    Status

    I searched open/upstream PRs: none currently covers this JSONLINE half. This run's environment port lacks a git branch/commit push channel (no bound workspace executor), so the patch has not been landed, compiled, or independently reviewed — the above is analysis and design derived from current main code plus the 3/3 repro in this thread, posted here so the fix can pick it up directly. The next run with a write channel will follow the standing flow: fork branch → failing regression test first → patch → gates (UT + independent review) → PR onto dev, CC @XuPeng-SH and the assignee @slyunnn.

  9. slyunnn commented on Oct 22, 2026

    @slyunnn

    Regression and extended verification for #30653 (2026-10-22)

    Verification Environment

    • Freshly fetched MatrixOne origin/main: 9f7fe43649b3eb9e34f1bc4073ecbc03c6e2a2a3 — frontend: fix result save/scan chain issues (#30552 #30553 #30571) (#30641). Source HEAD matches; rebuilt/restarted binary reports 9f7fe43649, Go 1.27.1, Linux/arm64.
    • Complete local logical topology: 2 CN / 1 TN / 1 Log service / Proxy, co-located in one container.
    • Proxy 127.0.0.1:6001, CN1 127.0.0.1:16001, and CN2 127.0.0.1:16002 were healthy. Original MODUMP CSV, extended CSV, JSONLINE object/array and exported-file import checks actually executed through all three endpoints.
    • Clients: PyMySQL and Go database/sql / go-sql-driver/mysql, with interpolateParams=false. Tests used text COM_QUERY, retained /* save_result */, and performed real server-side and LOCAL file transfers; no binary-prepared-statement claim.

    What Was Covered

    • OK — Exact original saved-result MODUMP CSV scenario: BIT(8)=1 exports as raw byte 0x01 and restores as 1, not 49, at all three endpoints.
    • OK — MODUMP and ordinary CSV OUTFILE checked against an independent big-endian byte oracle for 10 widths: 1, 7, 8, 9, 15, 16, 31, 32, 63, 64. Each corpus includes zero, one, width maximum and NULL.
    • OK — Per endpoint, the committed protocol case checks 20 exact file-byte oracles and 40 server/LOCAL roundtrips, including BIT(64) maximum 18446744073709551615. All CSV checks passed.
    • OK — JSONLINE ordinary OUTFILE and the supported result_scan(...) AS r → INTO OUTFILE ... FORMAT 'jsonline' route preserve numeric/NULL JSON output across BIT(1), BIT(8), and BIT(64): 18/18 export checks.
    • Failed — JSONLINE numeric imports still use byte semantics in both object and array modes. Valid BIT(8) value 1 successfully imports as 49; 44 / 255 encounter width errors, and the BIT(64) maximum returns 20101 with len(val)=20.
    • Failed — The single-value JSONLINE matrix contains 78 failing valid-number checks across the three endpoints. Its 18 NULL controls pass.
    • Failed — Actual JSONLINE exported-file roundtrips fail in all 18 width/path/endpoint combinations; the tested files return 20101 and leave zero rows. Source values remain unchanged.
    • OK — Out-of-range numeric controls for object/array and BIT(1/8/64): 18/18 rejected, targets empty, connections healthy.
    • OK — Both official CSV fix regression groups and the complete frontend package passed with -count=1.
    • Not covered — Direct MODUMP QUERY_RESULT ... FORMAT 'jsonline' is unsupported by current grammar and returns 1064 on all three endpoints. The saved-result JSONLINE export check above is an alternative route, not a claim that this MODUMP syntax works.
    • Not covered — General CSV delimiter/enclosure escaping tracked by [Bug]: CSV OUTFILE leaves delimiters unescaped in unquoted values, breaking round-trip #30702, production/TKE, physical multi-host deployment, release branches, performance/soak and fault injection. CSV byte 44 was checked as an encoding value, not claimed as a delimiter-safe roundtrip.

    Findings

    The CSV half is fixed by PR #30655, commit 774d0eda08c622e8717bed8caa8cd8c2ae3ffc2a — fix(frontend): preserve raw BIT bytes in MODUMP query results (#30655), included in the tested main revision.

    The specified follow-up comment describes the remaining JSONLINE problem, not a delivered fix. Actual execution confirms it remains unresolved: JSON numbers are exported correctly, but the shared scalar-import path still feeds decimal text into raw-byte BIT construction. An additional JSONLINE import-side fix is needed while retaining CSV raw-byte semantics.

    These are product-result failures, not environment or runner failures. No MatrixOne production source was modified, and no wrong-value expectation was accepted.

    MOTR / BVT Coverage

    • Branch: test/issue-30653-modump-bit-bytes.
    • Source commit: ee127a46535f160605af8636a72482ad5cc129fe — test: add MOTR regression for #30653; SSH push, remote SHA and tracking verified.
    • Added script/14_issue_regression/issue_30653_modump_bit_bytes_protocol.{go,sh}. Durable CI coverage is explicitly limited to the fixed CSV half; failing JSONLINE probes and their independent expected values were retained as verification evidence, not recorded as passing goldens.
    • The launcher uses the existing Go module and connection environment variables. Unique databases/stages and exact file cleanup permit parallel case scheduling; session save_query_result is restored. Saved-result cache entries follow normal server retention.
    • Recording passed. Final source replay alongside the existing compression control used -j 2 --repeat 2: 4/4 executions, 30/30 runner units.
    • Included in weekly MOTR PR #251, cherry-pick c9231ff5253b465a7da4e0b635577651e68415b9.
    • Full weekly selection on this runtime: -j 4 --repeat 2, 20/22 executions passed; both failures are the separately recorded [Performance]: IVFFLAT PRE hybrid queries execute the same MATCH search twice #30165 protocol case. The new CSV case passed both rounds. Runner concurrency is not MatrixOne engine-parallel evidence.
    • Hosted Actions run is budget-blocked before any steps ran; no hosted CI pass is claimed.
    • Final endpoint health, default session policy, database/stage cleanup and generated export-file cleanup checked.

    Final Conclusion

    Partially fixed: the original MODUMP CSV issue is fixed, but the JSONLINE half in the requested comment remains reproducible on current main. The issue should remain open pending that product fix.

    Large-data regression: not needed. This is a per-value representation and width contract; small exact-value corpora and the 64-bit boundary reproduce it deterministically. Increasing row count would add CI cost without exercising a distinct failure boundary.

  10. added a commit that references this issue on Nov 15, 2026
    c71e0c6
  11. XuPeng-SH commented on Nov 15, 2026

    @XuPeng-SH
    Contributor

    QA 交接(待验证,不是验收通过)

    #32759 已于 2026-11-15 06:26:33 UTC 合入 main,merge SHA c71e0c6904f444f45c4f4095be0e9f088b0c3b50,最终 PR head 692be578521e3768170a8bef28892be2e828b34f。此前 #30655 已修复 MODUMP CSV 原始字节编码;本次补齐剩余 JSONLINE 数值导入:区分 object/array 的 JSON NUMBER 与字符串,在原始 NULLIF 判断之后将规范非负 uint64 转为 BIT 值,覆盖直接构造及 varchar 暂存后的 BIT cast。CSV、Kafka、带引号字符串、JSON null 和非规范数字的既有行为保留,导出器不变。

    已重读全部 5 条历史评论。2026-10-22 的 78 项数值失败及 18 项导出文件回导失败发生在旧 main 9f7fe43649b3,对应的 JSONLINE 开发 follow-up 现由已合并修复承接,不直接视为新版本反例。未发现适用于本次合并修复的已验证反例或其他未完成开发 follow-up;本单是产品正确性问题,非 CI flaky,保持 open 转 Ariznawlll 验证。

    证据与限制:

    • 作者报告 native RED 32 个失败 subtests,修复后 8 个顶层测试及 49 个 subtests 通过,external/csvparser 完整包及 frontend CSV/saved-result、plan NULLIF guards 通过,指定 SCA 零问题。
    • 作者报告正常 MoTester 完整 metadata 36/36,导出字节/JSON 类型独立检查及保存结果 JSONLINE 回导通过;BIT(8) 输入 256 保留 20101/HY000,错误后连接和有效 LOAD 恢复。
    • approve 评审 5909244887 对应较早 head 5b4aae1329572226815ebb2d588f11d699ec4cef,不是最终 head。评审报告独立 helper 62,001 值检查及 12 工件静态核对;其 native/service BVT 因 Go 版本限制未重跑,复用当时冻结 CI 与作者报告,未取得作者原始日志。合并队列记录 显示 CI Required success;不把旧 head 评审或作者结果冒称本轮最终版本实测。
    • 本轮仅远端状态、讨论和 diff 核对,未运行本地测试。公开 SQL 主要是小规模 serial/compressed 输入,实际 staged cast 有 native UT;未证明端到端大型并行 LOAD、多主机、生产、release 或全量回归通过。

    建议 QA 在包含本次 merge 的 main 重跑原始 CSV 对照、JSONLINE object/array、0/1/44/255/NULL、BIT(1/8/64)、2^53+1/UINT64_MAX、原始字段 NULLIF、字符串与越界拒绝、保存结果导出后跨 CN 回读及连接恢复。保存结果 JSONLINE 使用已支持的 result_scan(...) → INTO OUTFILE FORMAT 'jsonline';不宣称直接 MODUMP JSONLINE 语法可用。#30702 的通用 CSV 分隔符转义仍为独立范围;不因此新增 4.2 backport 要求。

  12. Ariznawlll commented on Nov 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    原问题验证

    MODUMP QUERY_RESULT 导出的 BIT(8)=1 文件字节为 31 2c 01 0a,即 1,<raw 0x01>\n,不再是会被 LOAD 解释为 49 的十进制字符。重新 LOAD 后 CAST(b AS UNSIGNED)=1;普通 OUTFILE 对照组也保持为 1。

    result_scan -> INTO OUTFILE FORMAT jsonline -> LOAD DATA 全链路保持精确值,未再发生浮点精度或 BIT 语义损坏。

    Happy Path 覆盖

    1. BIT(1)/BIT(8)/BIT(64) 的 0、1、44、255、NULL 均往返一致。
    2. 2^53+1 = 9007199254740993 与 UINT64_MAX = 18446744073709551615 精确恢复。
    3. JSONLINE object 导出/导入及手工构造的 array 输入均通过;源、目标双向 EXCEPT 均为 0。
    4. motr 用例补充幂等清理,重复执行和并行执行结果稳定。

    Unhappy Path 覆盖

    BIT(8) 的 JSON 输入 256 被拒绝,实测错误码 20101(data too long,type width = 8);目标表保持 0 行,连接仍可继续执行 SQL。

    白盒验证

    核对 #30655 的 MODUMP BIT 原始字节导出路径,以及 #32759 的 JSONLINE BIT 数值转换和加载路径;覆盖 CSV 与 JSONLINE 两个独立修复面,未修改产品源码。

    回归测试

    已追加 matrixorigin/motr#349:14_issue_regression/issue_30653_modump_bit_roundtrip(35 语句)。录制后普通 diff 模式 3 并发连续两轮通过;PR 4 项 CI 全部成功。该用例标记 @target-branch: main,未混入 4.2-dev。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s1

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions