Skip to content

[Bug]: __mo_file_line reports wrong physical line after COMMENT or LINES STARTING BY skipped lines #30318

Closed

Description

@iamlinjunhong
  • I have checked the existing issues.

Branch Name

main

Commit ID

493d8b8

Other Environment Information

  • Hardware parameters: MacBook Pro, Apple Silicon (arm64)
  • OS type: macOS (darwin/arm64)
  • Others: single-node mo-service built from the workspace commit above; mysql CLI client

Actual Behavior

__mo_file_line does not report the physical line a record starts on when COMMENT lines or LINES STARTING BY discards precede the record.

Case A (COMMENT): file cmt.csv with physical lines 1:#h 2:a,1 3:#c 4:b,2, external table with 'comment'='#':

a  b  __mo_file_line
a  1  1
b  2  3

Case B (LINES STARTING BY): file sb2.csv with physical lines 1:junk1 2:junk2 3:xxxa,1, external table with lines starting by 'xxx':

a  b  __mo_file_line
a  1  1

Expected Behavior

Per the documented contract (ExternalErrorMode.RecordLine: "the physical line the record being materialized starts on"; CSVParser.RecordLine: "the 1-based physical line"), case A should report 2,4 and case B should report 3. Blank/whitespace-only skipped lines already follow this contract (existing TestRecordLine asserts physical lines 1,4,6), so comment/starting-by skips are inconsistent with the rest of the reader.

Steps to Reproduce

printf '#h\na,1\n#c\nb,2\n' > /tmp/loom_cmt.csv
printf 'junk1\njunk2\nxxxa,1\n' > /tmp/loom_sb2.csv
mysql -h 127.0.0.1 -P 6001 -u dump -p111 --default-character-set=utf8mb4 <<'EOF'
create database if not exists loomq; use loomq;
drop table if exists ext_cmt;
create external table ext_cmt(a varchar(20), b varchar(20))
infile{'filepath'='/tmp/loom_cmt.csv', 'format'='csv', 'comment'='#'}
fields terminated by ',';
select a, b, __mo_file_line from ext_cmt order by b;
drop table if exists ext_sb;
create external table ext_sb(a varchar(20), b varchar(20))
infile{'filepath'='/tmp/loom_sb2.csv', 'format'='csv'}
fields terminated by ',' lines starting by 'xxx';
select a, b, __mo_file_line from ext_sb;
EOF

The bug also reproduces without a server at the parser level: NewCSVParser with Comment: "#" on "#c1\n#c2\na,1\n" returns the row with RecordLine() == 1 instead of 3; with LinesStartingBy: "xxx" on "junk1\njunk2\nxxxa,1\n" it returns RecordLine() == 1 instead of 3.

Additional information

Root cause: in CSVParser.readRecord (pkg/sql/util/csvparser/csv_parser.go), recordStartLine is snapshotted once at entry (line 592) and is refreshed when blank/whitespace-only lines are skipped (lines 710, 716), but NOT on the COMMENT skip path (lines 613-621) nor on the LINES STARTING BY lookahead-discard path (lines 630-655, continue at the idx == -1 branch). Every newline consumed by those two paths still advances lineNo, so the reported recordStartLine + 1 lags behind by exactly the number of comment/discarded lines consumed during that Read() call. The value flows to SQL via CsvReader.makeBatchRows (pkg/sql/colexec/external/reader_csv.go:182-184, gated by WantLine) into ExternalErrorMode.RecordLine (pkg/sql/colexec/external/types.go:146-149) and is rendered as __mo_file_line (pkg/sql/colexec/external/external.go:1452). Impact: wrong file-line metadata for error triage whenever COMMENT or STARTING BY skips lines; no data corruption (values themselves are correct).

Found by Loom bug hunt (module: custom-a9872c7d6e; tests: unit, white-box, end-to-end)

Activity

  1. added
    kind/bug
    on Oct 15, 2026
  2. XuPeng-SH commented on Oct 19, 2026

    @XuPeng-SH
    Contributor

    本轮合并后独立实测通过,转 Ariznawlll;移除原开发负责人 iamlinjunhong,保持 issue OPEN 供验收。

    2026-10-19,精确 main/合并SHA 1ac287caeb695c793f2b4523774048621ee90c90(#30325)。新建隔离worktree并从本SHA重新 make cgo;Linux/amd64、Go1.27.1,未改生产源码。

    • mo-cgo-test -p1 -count=1 -v -timeout=10m ./pkg/sql/util/csvparser ./pkg/sql/colexec/external,normal 和独立 -race 都exit0。两种模式均有1个与本CSV修复无关的 ExternalArrow/LocalMinIO 用例因 local MinIO binary is not installed 跳过,明确未验证;不能记为该用例通过。normal 1944个命名PASS事件、race1943个;差1是Kafka分配预算测试使用 //go:build !race,并非race失败。
    • 原始 COMMENT 输入的物理行号严格为2/4,STARTING BY 输入为3;真实MySQL TCP分别在两个 CN 上运行。相邻组合跳过+多行记录3/7、CRLF3/5、引号内#数据2/3、EOF及全跳过0行均PASS,数据值/行数也核对。完整 TestAudit21CSVPhysicalLinesPublic exit0。
    • 因果负对照:只将 csv_parser.go 恢复到父提交 2ad1d7e5c2baafdee0b8fa097ce02ca32feb3a7b,保留新 TestRecordLine 全部断言,同一测试exit1,明确命中跳过行后的错误物理行号;小buffer、Reset等既有控制仍保留。

    真实命令与原始日志:pr-audit-continue21/run-latest.sh、logs/latest-csv-external-{normal,race}.、latest-csv-public.、latest-csv-before-control.*;公共SQL源码 csv_line_public_test.go,负对照源码来源/hash见 csv-before-manifest.json。没有用作者/CI执行代替本轮实测;未声称本轮完整mo-tester BVT或整仓CI通过。本PR目标main,不要求额外4.2回移。

  3. Ariznawlll commented on Oct 19, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:官方 main
    • commit:75ff736835c3d00d40dc00a0a1d3b11129388e5b(fix(csvparser): preserve physical record lines after skipped input #30325 的 merge commit 1ac287caeb695c793f2b4523774048621ee90c90 已在该提交祖先链)
    • 部署方式:隔离 Git worktree 重新构建 mo-service,macOS/arm64、Go 1.27.1;本地 1 CN + 1 TN + 1 LogService,MySQL TCP 6001。CGo 源码从该 SHA 编译,第三方预编译 native 库沿用本机已安装版本;与本次 CSV 行号逻辑无关。用户原工作目录未改动。
    • 测试日期:2026-10-19(Asia/Shanghai)

    原问题验证

    原始 COMMENT 文件物理行 1:#head, 2:a,1, 3:#middle, 4:b,2;真实外表 SELECT a,b,__mo_file_line 三轮均返回 (a,1,2),(b,2,4)。原始 LINES STARTING BY 文件物理行 1:junk1, 2:junk2, 3:xxxa,1,三轮均返回 (a,1,3);值、行数与物理起始行一致,而非旧错值 1/3 和 1。

    Happy Path 覆盖

    1. COMMENT 与 STARTING BY 两种独立跳行路径的真实 MySQL 协议外表查询各 3/3;行数分别为 2 和 1。
    2. 官方修复自带 TestRecordLine 在该 SHA 上 -count=3 通过,覆盖连续注释、起始前缀不匹配、两类跳行组合、多行引号记录、CRLF、引号内注释符、小 read block、Reset 和普通记录控制。

    Unhappy Path 覆盖

    1. TestRecordLine 的全输入被跳过场景读到 EOF,未产生伪行;Reset 后重新读取时行号从 1 起算。该 issue 的核心是合法跳行后的元数据,不以不存在的文件/权限错误作为修复判定。

    白盒验证

    • go test ./pkg/sql/util/csvparser -run TestRecordLine -count=3 通过;本轮未跑整个外表包、-race 或多 CN 外表,不能以作者此前的结果代替本轮执行。

    回归测试

    • 已追加 MOTR #247 head 7cf0ef9247e49492575d69bee43b1952ecdd19f7:suites/14_issue_regression/t/issue_30318_external_csv_physical_lines.test、配对 r/*.result 与两份固定 CSV fixture。独立物理行号 oracle 为 2/4/3,不从产品错值生成 golden。
    • 定向 --repeat 3:3/3,10/10 语句每轮通过;完整 14_issue_regression 本轮该用例通过,但全套 649 项为 604 通过、17 失败、27 跳过、1 中止,故未声称整套通过。失败包括本机陈旧 GOROOT 导致部分 Go 脚本无法启动、缺少 SQLAlchemy、其他既有用例差异及一个 Fulltext2 等待中止;均非本文件。PR 的 4 项离线检查成功,MOTR 集成 CI 未运行。

    测试证据

    • 回归源码与 oracle:test、result;离线检查。本地定向运行记录为 MOTR run-1792377878771615000,整套记录为 run-1792377944778733000。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions