Repository navigation
Description
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
maincommit3a1e42b4308bd0098e055ddb9360a07f85201d9c, isolated local one-TN/two-CN cluster, 2026-10-21. - Independent local MySQL 8.3.0 oracle, socket-only, with
secure_file_privrestricted 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
Ariznawlll commentedon Oct 21, 2026
补充同一“未被包围的字段未转义分隔符”根因的 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对照。[-][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[/+]daviszhen commentedon Oct 21, 2026
Reproduced both the original report and the BIT comment against main
04fe057bd7through 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.
XuPeng-SH commentedon Oct 22, 2026
增量复审补充(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。Ariznawlll commentedon Oct 22, 2026
测试版本
main,完整 commit:75ae9a79d626622e6ccec0bcb28412f5fcf84c7f,包含已合并修复 fix(frontend): escape CSV values before OUTFILE framing #30719;未改动产品源码。原问题验证
两个 CN 分别建立 3 个全新数据库,原始
ENCLOSED BY ''CSV OUTFILE → LOAD DATA 路径与双引号 enclosure 对照均通过。独立 Python 输入 oracle 比较完整行集,未将当前产品输出制作成 golden。NULL、字面反斜杠 N、引号和中文;回导逐值与输入完全一致。44 → 0已消失,实际回导为44。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 覆盖
n:字段终止符:6/6 返回 20301,输出文件不存在,源表 11 行完整保留。白盒验证
本轮以真实 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。
测试证据
csv-30702.log、csv-independent.log、csv_cases.py,包括每 CN/轮次的源值、回导值与拒绝结果;本地文件未上传,不作为公开下载制品。测试结论
测试通过。