Skip to content

[Bug]: CSV OUTFILE leaves delimiters unescaped in unquoted values, breaking round-trip #30702

Closed

Description

@Ariznawlll

Problem

SELECT ... INTO OUTFILE reports success with FIELDS ... ENCLOSED BY '', but writes embedded field delimiters and line breaks unescaped. A subsequent LOAD DATA INFILE of its own output fails. The same format round-trips in MySQL 8.3.0, and MatrixOne round-trips the same rows when an enclosure character is used.

Environment

  • MatrixOne main commit 3a1e42b4308bd0098e055ddb9360a07f85201d9c, isolated local one-TN/two-CN cluster, 2026-10-21.
  • Independent local MySQL 8.3.0 oracle, socket-only, with secure_file_priv restricted to its own test directory.

Reproduce

Use a writable local path in place of /tmp/out.csv.

CREATE DATABASE outfile_repro;
USE outfile_repro;
CREATE TABLE src(id INT PRIMARY KEY, s VARCHAR(50));
CREATE TABLE dst LIKE src;
INSERT INTO src VALUES
  (1,'plain'),(2,'a,b'),(3,CAST(x'610A62' AS CHAR)),(4,NULL);

SELECT id,s FROM src ORDER BY id
  INTO OUTFILE '/tmp/out.csv'
  FIELDS TERMINATED BY ',' ENCLOSED BY '' HEADER 'false';

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

Actual vs expected

Export succeeds. The raw MatrixOne file is equivalent to:

1,plain\n
2,a,b\n
3,a\n
b\n
4,\N\n

The comma in a,b and the newline in a\nb are not escaped. Loading the comma-only two-row subset returns ERROR 20301: the data of row 2 contained is not equal to input columns; the newline-only subset similarly fails at row 3. Each failed LOAD leaves its destination empty, so the import's failure atomicity is correct, but the successfully exported file cannot be used for restoration.

With the same source rows and ENCLOSED BY '"', MatrixOne exports, imports and compares all four rows including the exact string bytes and NULL correctly. MySQL 8.3.0 with ENCLOSED BY '' escapes comma as \, and embedded LF as backslash+LF; its exported file imports back to the original four HEX(s) values.

Expected: when no enclosure is selected, escape field/line delimiters in the output (or reject an unrepresentable format before reporting export success). The default backslash escape convention is already understood by LOAD DATA.

Reproduction confidence and code path

Three independent fresh MatrixOne databases: comma and newline cases each 3/3, with exact raw-file bytes checked; failed LOAD remained atomic and the quoted-format control succeeded in all three. Evidence: /private/tmp/mo-sequence-run2-20261020/ten-bugs-1021/outfile-no-enclose-repeat.jsonl (39 checks). MySQL 8.3.0 output and re-import were checked separately using the same four rows.

White-box hypothesis: pkg/frontend/export.go constructByte calls addEscapeToString for string columns, but that helper escapes backslash and the enclosure byte only. With enclosure byte zero, it does not escape FIELDS TERMINATED BY or LINES TERMINATED BY bytes before formatOutputString writes the row. The CSV import then sees extra columns or rows. No matching open/closed issue or compatibility-contract exemption was found; closed #14601 is about JSON quotes with empty enclosure, and #5148 does not describe this current delimiter-specific condition.

Activity

  1. added this to the MO-202610 milestone on Oct 21, 2026
  2. added
    kind/bug
    on Oct 21, 2026
  3. added theissue type on Oct 21, 2026
  4. added
    severity/s0
    and removed on Oct 21, 2026
  5. Ariznawlll commented on Oct 21, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充同一“未被包围的字段未转义分隔符”根因的 BIT 边界:即使 CSV 使用默认的 ENCLOSED BY '"',BIT 列默认也不会被包围;BIT(8)=44 的原始单字节正好是逗号 0x2C,普通 SELECT id,b ... INTO OUTFILE ... FORMAT 'csv' 导出后 LOAD DATA 返回成功,但该行回导为 BIT(8)=0,数据静默改变。源值 1/44/255/NULL 中只有 44 改值。3 个全新源库/目标表均 3/3 重现,结果在 .../ten-bugs-1021/jsonline-bit-repeat.jsonl 的 csv-exact 对照中;FORCE_QUOTE (b) 的额外对照可正确恢复 1/44/255/NULL。

    同一次测试还发现 JSONLINE 导出 BIT 为十进制数后,单值 1 回导成 49、较大值回导报宽度错误。后者与已有 #30653 的“十进制文本 vs BIT 原始字节 LOAD”语义相同,会在该 issue 补充,不另提重复 bug。建议本 issue 的 CSV 回归同时覆盖 ENCLOSED BY '' 字符串、默认 enclosure 下 BIT 分隔符字节和 FORCE_QUOTE 对照。

  6. changed the title [-][Bug]: INTO OUTFILE with ENCLOSED BY '' leaves delimiters unescaped and cannot be loaded back[/-] [+][Bug]: CSV OUTFILE leaves delimiters unescaped in unquoted values, breaking round-trip[/+] on Oct 21, 2026
  7. daviszhen commented on Oct 21, 2026

    @daviszhen
    Contributor

    Reproduced both the original report and the BIT comment against main 04fe057bd7 through a real MySQL connection: bare comma/LF strings fail LOAD with error 20301; unquoted BIT(8) 44 loads successfully as 0. Submitted the fix in #30719 (c35a8e73ad).

    The CSV writer now escapes non-NULL values at the common output boundary in both batch/result-set paths. This covers raw BIT and scalar delimiter collisions, not only string branches. Genuine NULL remains a distinct marker; literal NULL/backslash-N, quotes and CR/CRLF are preserved. No normal-query, JSONLINE or Parquet encoder changes.

    Validation: 11 complete OUTFILE -> LOAD DATA SQL round-trips and an early-rejection/file-nonexistence control pass, including all 256 BIT(8) values, BIT(32), default/no enclosure, FORCE_QUOTE, custom delimiters, decimal-dot delimiters and NULL controls. The same 12 subcases fail with the verified exact-main export.go overlay. Component tests additionally cover all BIT bytes across both exporter paths and small parser buffers. New-head CI is pending; the PR records the unrelated existing frontend timing-test failures instead of claiming a clean full-suite run.

  8. XuPeng-SH commented on Oct 22, 2026

    @XuPeng-SH
    Contributor

    增量复审补充(PR #30719,head e10663ded4fe6d6ca5167f4541d60f3a186a3e2b):最新 review 在 CSV round-trip 之外确认了一个新的 framing 边界。BIT(24)=6382178,字段终止符为反斜杠、enclosure 为 CR、行终止符为 LF、未启用 FORCE_QUOTE 时,OUTFILE 成功但按相同参数 LOAD 返回 20301。review 记录的候选字节为 0d310d5c0d6162620d0a,而 merge-base 输出 315c6162620a 可成功回导;另有 n: 分隔符碰撞复现。建议在创建/打开输出文件前拒绝 CR enclosure + LF termination 组合,或以 LOAD 实际识别的物理与规范化边界完成校验。此为针对精确 head 的 reviewer 实测记录;我本机未运行该 SQL。

  9. Ariznawlll commented on Oct 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:官方 main,完整 commit:75ae9a79d626622e6ccec0bcb28412f5fcf84c7f,包含已合并修复 fix(frontend): escape CSV values before OUTFILE framing #30719;未改动产品源码。
    • 日期:2026-10-22;macOS arm64 本地隔离 2 CN / 1 TN / 1 LogService,DISK-V2;另执行仓库 embedded 单 CN 真实 MySQL TCP 回归。
    • 本次为 main 验收,不声明旧发布版本已回补。

    原问题验证

    两个 CN 分别建立 3 个全新数据库,原始 ENCLOSED BY '' CSV OUTFILE → LOAD DATA 路径与双引号 enclosure 对照均通过。独立 Python 输入 oracle 比较完整行集,未将当前产品输出制作成 golden。

    • 字符串:11 行,包括逗号、LF、CR、CRLF、空串、真正 NULL、字面 NULL、字面反斜杠 N、引号和中文;回导逐值与输入完全一致。
    • BIT:0–255 全部 256 个 BIT(8) 值加 NULL,共 257 行;全部精确回导。特别是原静默错值 44 → 0 已消失,实际回导为 44。
    • 2 CN × 3 轮 × 2 类数据 × 2 enclosure,共 24 组独立 round-trip 通过;源数据每组前后均保持不变。

    Happy Path 覆盖

    另外实际运行 main 已有 pkg/embed/outfile_csv_roundtrip_test.go::TestOutfileCSVRestoresStringAndBITValues,-count=3 全部通过(15.882 秒)。覆盖 BIT(24/32)、FORCE_QUOTE、多字节与反斜杠分隔符、小数点分隔符、CRLF、自定义行边界及所有 BIT 字节、值和 NULL 区分。测试实际读取导出文件字节并 LOAD 回表比较,不是只测编码函数。

    Unhappy Path 覆盖

    1. 独立双 CN 每轮使用无 enclosure 的 n: 字段终止符:6/6 返回 20301,输出文件不存在,源表 11 行完整保留。
    2. 仓库回归实际验证 enclosure/终止符前缀冲突、CR enclosure + LF 行终止符,以及不能无歧义表示的 bare CSV 配置;均在创建文件前拒绝。包括本 issue 最近 review 提出的 BIT(24) framing 边界及 FORCE_QUOTE 对照。
    3. 所有导入表/源库及本轮独立导出文件均清理;连接仍可复用,无本场景 panic/hang。

    白盒验证

    本轮以真实 SQL/协议/文件回导为主,没有使用产品 overlay、故障 hook 或 mock LOAD。未额外执行完整 frontend 包或本地全套测试。

    回归测试

    无需重复增加 MOTR:#30719 已合入上述真实协议回归及 frontend 编码回归,覆盖本问题原路径和必需格式/NULL/拒绝原子性边界。本轮实际将现有协议回归重跑 3 次,并补双 CN 独立 oracle。

    PR 最新检查:CI Required、Ubuntu UT、Linux/x64 多 CN PESSIMISTIC BVT、PROXY BVT、arm64 SCA 均 SUCCESS;被 workflow 跳过的 upgrade/其他矩阵不算通过。本地没有跑完整 MOTR suite,也没有新触发昂贵 CI。

    测试证据

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions