Skip to content

[Bug]: LINES STARTING BY silently corrupts rows when a quoted field contains a newline #30319

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

LOAD DATA ... LINES STARTING BY 'xxx' silently loads a corrupted row when a quoted field contains an embedded newline. Given file sb.csv (bytes: "a\nxxxb",1\nxxxc,2\n):

create table t_sb(a varchar(50), b varchar(50));
load data infile '/tmp/loom_sb.csv' into table t_sb
  fields terminated by ',' enclosed by '"' lines starting by 'xxx';
select * from t_sb order by b;

Result (LOAD reports success, no error, no warning):

a  b
b" 1
c  2

The first logical record (a<newline>xxxb, 1) is destroyed and replaced by a garbage row (b", 1).

Expected Behavior

MySQL 8.0 checks the STARTING BY prefix at the start of each logical row; a newline inside an enclosed field is part of the row, not a row boundary, so the whole quoted record "a\nxxxb",1 must load intact as (a\nxxxb, 1) followed by (c, 2). Control run on the same server WITHOUT lines starting by loads exactly that (see evidence). At minimum the input must not be silently corrupted into garbage rows.

Steps to Reproduce

printf '"a\nxxxb",1\nxxxc,2\n' > /tmp/loom_sb.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 t_sb;
create table t_sb(a varchar(50), b varchar(50));
load data infile '/tmp/loom_sb.csv' into table t_sb
  fields terminated by ',' enclosed by '"' lines starting by 'xxx';
select * from t_sb order by b;
EOF

Parser-level equivalent (no server needed): NewCSVParser with LinesStartingBy: "xxx", FieldsEnclosedBy: "\"" on "\"a\nxxxb\",1\nxxxc,2\n" returns row 0 = [b", 1] instead of [a\nxxxb, 1].

Additional information

Root cause: the STARTING BY pre-scan in CSVParser.readRecord (pkg/sql/util/csvparser/csv_parser.go:630-655) calls readUntilTerminator, which splits on the line terminator even when it occurs inside a quoted field (the comment at lines 627-629 acknowledges this). For the input above, the lookahead consumes "a\n, finds no xxx, and discards the whole chunk including the opening quote and half the field; the remainder xxxb",1 is then re-parsed as a fresh line, and the stray " is absorbed literally because LOAD always sets UnescapedQuote: true (pkg/sql/colexec/external/types.go, newCSVParserFromReader). The corruption is silent: makeBatchRows (pkg/sql/colexec/external/reader_csv.go) receives a well-formed-looking row and inserts it. Related closed issues #963/#7747 cover different STARTING BY symptoms (prefix not stripped / option ignored), not quoted-newline corruption; PR #15111 only added test cases. Impact: silent wrong data in loaded tables whenever STARTING BY meets multi-line quoted fields.

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. iamlinjunhong commented on Oct 16, 2026

    @iamlinjunhong
    ContributorAuthor

    I checked the reported input against MySQL Community Server 8.0.45 on macOS arm64, using the same non-LOCAL LOAD DATA statement and field/line settings.

    Exact input bytes (hex): 22610a78787862222c310a787878632c320a, equivalent to:

    printf '"a\nxxxb",1\nxxxc,2\n' > sb.csv

    With FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES STARTING BY 'xxx', MySQL returns:

    SELECT HEX(a), b FROM t_sb ORDER BY b;
    HEX(a)  b
    6222    1
    63      2
    

    That is (b", 1) and (c, 2), with no warnings from SHOW WARNINGS immediately after the load. This matches the MatrixOne output reported in this issue, rather than the stated MySQL expectation.

    The controls on that same MySQL instance also distinguish the behavior:

    Input/settings Result (\n denotes an actual newline in the stored value)
    Same input without LINES STARTING BY (a\nxxxb, 1), (xxxc, 2)
    Prefix before the opening quote: xxx"a\nxxxb",1\nxxxc,2\n (a\nxxxb, 1), (c, 2)
    First quoted record contains no prefix: "a\nb",1\nxxxc,2\n Only (c, 2)

    This is consistent with MySQL's implementation: READ_INFO::read_field() searches for the line prefix before inspecting the enclosing quote, and find_start_of_fields() scans raw bytes for that prefix. A prefix found after the embedded newline therefore does not preserve the earlier opening quote or field content.

    For this exact case, the reported MatrixOne behavior is compatible with the tested MySQL version. Preserving the complete first record would require a deliberate change in semantics. No code change or PR is proposed on that basis. Please confirm the reference behavior before treating this as a compatibility bug.

    Scope of verification: MySQL was executed directly; the MatrixOne comparison uses the issue's reported output and inspected source, not a new MatrixOne runtime execution. This does not establish equivalence for other inputs or all MySQL versions.

  3. iamlinjunhong commented on Oct 19, 2026

    @iamlinjunhong
    ContributorAuthor

    重复issue

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