Repository navigation
Description
Is there an existing issue for the same bug?
Checked #30378 (parallel non-strict CSV LOAD splits inside a quoted field — that rework covered the non-strict planner only; getTailSizeStrict is a separate code path and still picks a boundary inside a quoted field on current main, and the very same fixture now loads correctly with STRICT 'FALSE' per the control test below), #30329 (CSV consolidation tracker scoped to blank-line/enclosure/line-terminator semantics, not the strict parallel boundary resync), #30341 (non-strict with a non-'\n' line terminator), #31326 (probe performance on malformed CSV), #31432 (JSONLINE sharding), #31285 (row-size cap), #15084 / #16794 / #30462 (unterminated-quote errors on serial loads or alternate terminators). None covers the strict-mode boundary resync.
Branch Name
main
Commit ID
Other Environment Information
- Hardware parameters: Apple M-series (arm64), 32 GB
- OS type: macOS (repo unit test; drives the production
ReadFileOffsetplanner and the productionNewCsvReader/ReadBatchover an in-memory ETL filesystem, no mo-service needed) - Others: go1.27.x,
CGO_CFLAGS="-I$PWD/cgo -I$PWD/thirdparties/install/include"
Actual Behavior
A strict-mode (STRICT 'TRUE') parallel LOAD of a CSV file whose records contain a quoted field with an embedded newline (RFC-style CSV) aborts with invalid input: csvParser error: unterminated quoted field on a file that loads correctly in every other mode: the range planner chose a chunk boundary INSIDE the quoted field.
pkg/sql/compile/compile.go:5071collectsvisibleColswhenparam.Strictand callsexternal.ReadFileOffset(param, parallelSize, fileSize, visibleCols)atcompile.go:5123; readers are parallel by default (one per 128 MiB,GetExternParallelSize,compile.go:3637-3649).- In
ReadFileOffset(pkg/sql/colexec/external/external.go:677) the strict path callsgetTailSizeStrict(external.go:1010). When the even byte split (guess= round(fileSize/mcpu)) lands in the middle of a quoted field,getTailSizeStrictstarts a BRAND-NEW CSV parser at that raw offset (newCSVParserFromReader(param, bufR),external.go:1034) with no recovery of the "inside a quoted field" state. The only in-quote protection is the leading-quote skip loop (external.go:1013-1026), which handles a guess that lands exactly on quote bytes, not a mid-field landing. - The fresh parser therefore treats the newline embedded in the quoted field as a record terminator, and
isLegalLine(external.go:1053) accepts the fabricated fields whenever the columns can hold them (varchar/text columns accept any text). The boundary is advanced to just past that embedded newline — still inside the record. - Minimal fixture (40 bytes, in the test below):
ReadFileOffset(param, 2, 40, cols)returns offsets[0, 27, 27, -1]. Byte 20 (the guess) and byte 27 are both inside the quoted field"aaa,999\nmore,xyz"of the record that starts at byte 16. Range[0,27)ends with an unterminated quoted field and errors in the productionCsvReader; range[27,EOF)starts mid-field and parses the rowmore,xyz"which exists nowhere in the file (shown by TestLoomStrictParallelSecondChunkFabricatesRow). The record starting at byte 16 is silently lost at reader level.
The reader-level test reproduces through the production NewCsvReader + ReadBatch path, the same calls compilation makes.
Expected Behavior
A strict-mode parallel LOAD must not split inside a quoted field: every range must stay parseable and the parallel result must reproduce the serial parse of the same file. That is the behavior the repo already states for the non-strict planner — issue #30378: "A parallel CSV LOAD must not split inside a quoted field. It should keep every range parseable" — enforced by the boundary rework that followed it; on current main the non-strict planner keeps the exact fixture below in parseable ranges (control test passes). The strict planner must apply the same quote-state-aware resynchronization (like getNonStrictBoundary, external.go:919, which restarts from a true record start) instead of starting a fresh parser at the raw guess offset.
Steps to Reproduce
- Save the Go test below as
pkg/sql/colexec/external/loom_test_parallel_boundary_test.go(packageexternal). - From the repo root run:
export CGO_CFLAGS="-I$PWD/cgo -I$PWD/thirdparties/install/include"
go test ./pkg/sql/colexec/external/ -run 'TestLoomStrictParallel|TestLoomControl' -count=1 -v
- TestLoomStrictParallelSplitsMultilineQuotedRecord, TestLoomStrictParallelLoadReaderMultilineQuotedRecord and TestLoomStrictParallelSecondChunkFabricatesRow FAIL (boundary 27 inside the quoted field / parallel load error / fabricated row). The controls TestLoomControlNonStrictParallelSameFile (same file, non-strict planner) and TestLoomControlStrictSingleReaderSameFile (strict, single reader) PASS — proving neither the file nor strict mode per se is the problem, only the strict-mode parallel boundary resync.
Full test source:
// Copyright 2026 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package external
import (
"bytes"
"context"
"io"
"strings"
"testing"
"github.com/matrixorigin/matrixone/pkg/container/batch"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/container/vector"
"github.com/matrixorigin/matrixone/pkg/fileservice"
"github.com/matrixorigin/matrixone/pkg/pb/pipeline"
"github.com/matrixorigin/matrixone/pkg/pb/plan"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
"github.com/matrixorigin/matrixone/pkg/sql/util/csvparser"
"github.com/matrixorigin/matrixone/pkg/testutil"
"github.com/stretchr/testify/require"
)
// 40 bytes total. With mcpu=2, ReadFileOffset's guess lands at byte 20 --
// inside the quoted field "aaa,999\nmore,xyz" of the record that starts at
// byte 16. A strict-mode parallel split must not break that record: the
// ranges must reproduce the serial parse exactly.
const loomStrictMultilineCSV = "1,a\n1,a\n1,a\n1,a\n" +
"2,\"aaa,999\nmore,xyz\"\n" +
"3,c"
// loomVarcharCols returns two varchar columns: any text field is a legal
// value for them, which is what makes the mid-record resync in
// getTailSizeStrict accept a boundary that is not a real record start.
func loomVarcharCols() []*plan.ColDef {
return []*plan.ColDef{
{Name: "id", Typ: plan.Type{Id: int32(types.T_varchar)}},
{Name: "v", Typ: plan.Type{Id: int32(types.T_varchar)}},
}
}
func loomWriteFile(t *testing.T, path string, data []byte) (fileservice.FileService, context.Context) {
t.Helper()
ctx := fileservice.WithSQLFileAccess(t.Context(), "sys", "moadmin")
fs := testutil.NewFS(t)
t.Cleanup(func() { fs.Close(ctx) })
require.NoError(t, fs.Write(ctx, fileservice.IOVector{
FilePath: path,
Entries: []fileservice.IOEntry{{Offset: 0, Size: int64(len(data)), ReaderForWrite: bytes.NewReader(data)}},
}))
return fs, ctx
}
// loomParseRange parses data[start,end) with the production CSV parser and
// returns the rows as "field,field,..." strings, or the parse error.
func loomParseRange(t *testing.T, param *tree.ExternParam, data []byte, start, end int64) ([]string, error) {
t.Helper()
if end == -1 {
end = int64(len(data))
}
parser, err := newCSVParserFromReader(param, bytes.NewReader(data[start:end]))
if err != nil {
return nil, err
}
var got []string
var fields []csvparser.Field
for {
fields, err = parser.Read(fields)
if err == io.EOF {
return got, nil
}
if err != nil {
return got, err
}
row := ""
for i, f := range fields {
if i > 0 {
row += ","
}
// Field.Val may alias the parser's internal buffer; clone before
// retaining it past the next Read.
row += strings.Clone(f.Val)
}
got = append(got, row)
}
}
func loomStrictExtern(fs fileservice.FileService, ctx context.Context, path string) *tree.ExternParam {
return &tree.ExternParam{
ExParamConst: tree.ExParamConst{
Filepath: path, FileStartOff: 0, Format: tree.CSV, ScanType: tree.INFILE,
CompressType: tree.NOCOMPRESS, Tail: &tree.TailParameter{},
},
ExParam: tree.ExParam{
FileService: fs, Ctx: ctx, Parallel: true, Strict: true,
ExternType: int32(plan.ExternType_LOAD),
},
}
}
// TestLoomStrictParallelSplitsMultilineQuotedRecord proves that the strict
// parallel LOAD DATA range planner selects a byte boundary INSIDE a quoted
// multiline field: the first chunk then ends with an unterminated quoted
// field and no longer parses, while the second chunk starts mid-field and
// emits a bogus row. The serial parse of the same file has no such problem.
func TestLoomStrictParallelSplitsMultilineQuotedRecord(t *testing.T) {
data := []byte(loomStrictMultilineCSV)
require.Equal(t, 40, len(data), "fixture must stay 40 bytes so the guess lands inside the quoted field")
require.Equal(t, byte('a'), data[20], "guess (mcpu=2) must land inside the quoted field")
const path = "etl:/loom-strict-multiline.csv"
fs, ctx := loomWriteFile(t, path, data)
param := loomStrictExtern(fs, ctx, path)
serial, err := loomParseRange(t, param, data, 0, -1)
require.NoError(t, err)
require.Equal(t,
[]string{"1,a", "1,a", "1,a", "1,a", "2,aaa,999\nmore,xyz", "3,c"},
serial, "sanity: the serial parse is well-formed")
offsets, err := ReadFileOffset(param, 2, int64(len(data)), loomVarcharCols())
require.NoError(t, err)
require.Equal(t, int64(0), offsets[0])
require.Equal(t, int64(-1), offsets[len(offsets)-1])
var got []string
for i := 0; i < len(offsets); i += 2 {
rows, perr := loomParseRange(t, param, data, offsets[i], offsets[i+1])
require.NoErrorf(t, perr,
"strict parallel range [%d,%d) must stay parseable; offsets=%v (file parses fine serially)",
offsets[i], offsets[i+1], offsets)
got = append(got, rows...)
}
require.Equal(t, serial, got, "strict parallel split must reproduce the serial parse")
}
// loomReadRanges drives the production CsvReader (Open/ReadBatch) over the
// given ranges, the same way compileExternScanParallelReadWrite does.
func loomReadRanges(t *testing.T, extern *tree.ExternParam, path string, data []byte, offsets []int64) ([]string, error) {
t.Helper()
proc := testutil.NewProcess(t, testutil.WithFileService(extern.FileService))
proc.Base.SessionInfo.Account = "sys"
proc.Base.SessionInfo.Role = "moadmin"
defer proc.Free()
var rows []string
for idx := 0; 2*idx < len(offsets); idx++ {
rangeOffsets := offsets[2*idx : 2*idx+2]
if err := func() error {
param := &ExternalParam{
ExParamConst: ExParamConst{
Idx: idx, Extern: extern, ColumnListLen: 2, maxBatchSize: 1 << 20,
Attrs: []plan.ExternAttr{
{ColName: "id", ColIndex: 0, ColFieldIndex: 0},
{ColName: "v", ColIndex: 1, ColFieldIndex: 1},
},
Cols: loomVarcharCols(),
FileSize: []int64{int64(len(data))},
FileOffsetTotal: []*pipeline.FileOffset{{Offset: rangeOffsets}},
},
ExParam: ExParam{Fileparam: &ExFileparam{FileIndex: 1, FileCnt: 1, Filepath: path}},
}
reader, err := NewCsvReader(param, proc)
if err != nil {
return err
}
defer reader.Close()
empty, err := reader.Open(param, proc)
if err != nil {
return err
}
if empty {
return nil
}
for {
bat := batch.NewWithSize(2)
bat.Vecs[0] = vector.NewVec(types.T_varchar.ToType())
bat.Vecs[1] = vector.NewVec(types.T_varchar.ToType())
finished, err := reader.ReadBatch(proc.Ctx, bat, proc, nil)
if err != nil {
bat.Clean(proc.Mp())
return err
}
for row := 0; row < bat.RowCount(); row++ {
rows = append(rows,
bat.Vecs[0].GetStringAt(row)+","+bat.Vecs[1].GetStringAt(row))
}
bat.Clean(proc.Mp())
if finished {
return nil
}
}
}(); err != nil {
return rows, err
}
}
return rows, nil
}
// TestLoomStrictParallelLoadReaderMultilineQuotedRecord is the reader-level
// reproduction: the same valid file loads fine through one serial CsvReader
// but the strict parallel readers either fail with "unterminated quoted
// field" (first chunk) or ingest a fabricated row (second chunk).
func TestLoomStrictParallelLoadReaderMultilineQuotedRecord(t *testing.T) {
data := []byte(loomStrictMultilineCSV)
const path = "etl:/loom-strict-multiline-reader.csv"
fs, ctx := loomWriteFile(t, path, data)
extern := loomStrictExtern(fs, ctx, path)
serial, err := loomReadRanges(t, extern, path, data, []int64{0, -1})
require.NoError(t, err)
require.Equal(t,
[]string{"1,a", "1,a", "1,a", "1,a", "2,aaa,999\nmore,xyz", "3,c"},
serial, "sanity: a single (serial) reader loads the file")
offsets, err := ReadFileOffset(extern, 2, int64(len(data)), loomVarcharCols())
require.NoError(t, err)
parallel, err := loomReadRanges(t, extern, path, data, offsets)
require.NoErrorf(t, err,
"strict parallel LOAD must load a file the serial reader accepts; offsets=%v, partial rows=%q",
offsets, parallel)
require.Equal(t, serial, parallel,
"strict parallel LOAD must reproduce the serial result; offsets=%v, parallel rows=%q", offsets, parallel)
}
// TestLoomStrictParallelSecondChunkFabricatesRow documents what the second
// chunk of the broken split ingests: it starts mid-field at byte 27, so the
// reader fabricates a row ("more,xyz\"") that exists nowhere in the file,
// while the real record that starts at byte 16 is lost.
func TestLoomStrictParallelSecondChunkFabricatesRow(t *testing.T) {
data := []byte(loomStrictMultilineCSV)
const path = "etl:/loom-strict-second-chunk.csv"
fs, ctx := loomWriteFile(t, path, data)
param := loomStrictExtern(fs, ctx, path)
offsets, err := ReadFileOffset(param, 2, int64(len(data)), loomVarcharCols())
require.NoError(t, err)
require.Greater(t, len(offsets), 3, "the strict split must have produced a second range")
secondStart := offsets[2]
rows, err := loomParseRange(t, param, data, secondStart, offsets[3])
require.NoError(t, err, "the fabricated-row demonstration must not error")
serial, err := loomParseRange(t, param, data, 0, -1)
require.NoError(t, err)
for _, r := range rows {
require.Contains(t, serial, r,
"every row the parallel second chunk ingests must exist in the serial parse (secondStart=%d)", secondStart)
}
}
// Control: the very same file with the same two-way byte split is handled
// correctly by the non-strict planner (it resynchronizes from the previous
// real record start), so the divergence above is specific to strict mode's
// mid-record resync.
func TestLoomControlNonStrictParallelSameFile(t *testing.T) {
data := []byte(loomStrictMultilineCSV)
const path = "etl:/loom-control-nonstrict.csv"
fs, ctx := loomWriteFile(t, path, data)
param := loomStrictExtern(fs, ctx, path)
param.Strict = false
serial, err := loomParseRange(t, param, data, 0, -1)
require.NoError(t, err)
require.Equal(t, 6, len(serial))
offsets, err := ReadFileOffset(param, 2, int64(len(data)), nil)
require.NoError(t, err)
require.Len(t, offsets, 4)
var got []string
for i := 0; i < len(offsets); i += 2 {
rows, perr := loomParseRange(t, param, data, offsets[i], offsets[i+1])
require.NoErrorf(t, perr, "non-strict range [%d,%d) must stay parseable; offsets=%v", offsets[i], offsets[i+1], offsets)
got = append(got, rows...)
}
require.Equal(t, serial, got)
}
// Control: strict mode with a single reader keeps the whole file in one
// range, so the file itself is not the problem.
func TestLoomControlStrictSingleReaderSameFile(t *testing.T) {
data := []byte(loomStrictMultilineCSV)
const path = "etl:/loom-control-strict-serial.csv"
fs, ctx := loomWriteFile(t, path, data)
param := loomStrictExtern(fs, ctx, path)
serial, err := loomParseRange(t, param, data, 0, -1)
require.NoError(t, err)
offsets, err := ReadFileOffset(param, 1, int64(len(data)), loomVarcharCols())
require.NoError(t, err)
require.Equal(t, []int64{0, -1}, offsets)
got, err := loomParseRange(t, param, data, offsets[0], offsets[1])
require.NoError(t, err)
require.Equal(t, serial, got)
}Additional information
Root-cause locations: strict resync without quote state (external.go:1010, fresh parser at :1034), strict gather/call chain (compile.go:5071, compile.go:5123).
Trigger conditions: STRICT 'TRUE' + more than one parallel reader + a file whose size/mcpu split lands inside a quoted field that contains an embedded newline + column types that make the fabricated resync fields "legal" (e.g. all-varchar load tables).
Workaround: STRICT 'FALSE' (the non-strict planner is quote-state aware on current main) or a single reader, which is why severity is s1; when the first range errors the load aborts and no partial data is committed, but the reader-level second-range parse also shows rows would be fabricated from mid-field bytes if range 1 is not what fails first.
Fix suggestion: make getTailSizeStrict resynchronize the way the fixed non-strict planner does — determine the true record start that contains the guess offset (scan back to the previous record boundary carrying quote state, or parse forward from that record start) and accept only boundaries at real record starts; keep the current varchar fast path behind that validation.
Full reproduction, exactly as Loom ran it
# Save the following Go test as: pkg/sql/colexec/external/loom_test_parallel_boundary_test.go (package external)
# From the repo root run:
# export CGO_CFLAGS="-I$PWD/cgo -I$PWD/thirdparties/install/include"
# go test ./pkg/sql/colexec/external/ -run 'TestLoomStrictParallel|TestLoomControl' -count=1 -v
// Copyright 2026 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package external
import (
"bytes"
"context"
"io"
"strings"
"testing"
"github.com/matrixorigin/matrixone/pkg/container/batch"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/container/vector"
"github.com/matrixorigin/matrixone/pkg/fileservice"
"github.com/matrixorigin/matrixone/pkg/pb/pipeline"
"github.com/matrixorigin/matrixone/pkg/pb/plan"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
"github.com/matrixorigin/matrixone/pkg/sql/util/csvparser"
"github.com/matrixorigin/matrixone/pkg/testutil"
"github.com/stretchr/testify/require"
)
// 40 bytes total. With mcpu=2, ReadFileOffset's guess lands at byte 20 --
// inside the quoted field "aaa,999\nmore,xyz" of the record that starts at
// byte 16. A strict-mode parallel split must not break that record: the
// ranges must reproduce the serial parse exactly.
const loomStrictMultilineCSV = "1,a\n1,a\n1,a\n1,a\n" +
"2,\"aaa,999\nmore,xyz\"\n" +
"3,c"
// loomVarcharCols returns two varchar columns: any text field is a legal
// value for them, which is what makes the mid-record resync in
// getTailSizeStrict accept a boundary that is not a real record start.
func loomVarcharCols() []*plan.ColDef {
return []*plan.ColDef{
{Name: "id", Typ: plan.Type{Id: int32(types.T_varchar)}},
{Name: "v", Typ: plan.Type{Id: int32(types.T_varchar)}},
}
}
func loomWriteFile(t *testing.T, path string, data []byte) (fileservice.FileService, context.Context) {
t.Helper()
ctx := fileservice.WithSQLFileAccess(t.Context(), "sys", "moadmin")
fs := testutil.NewFS(t)
t.Cleanup(func() { fs.Close(ctx) })
require.NoError(t, fs.Write(ctx, fileservice.IOVector{
FilePath: path,
Entries: []fileservice.IOEntry{{Offset: 0, Size: int64(len(data)), ReaderForWrite: bytes.NewReader(data)}},
}))
return fs, ctx
}
// loomParseRange parses data[start,end) with the production CSV parser and
// returns the rows as "field,field,..." strings, or the parse error.
func loomParseRange(t *testing.T, param *tree.ExternParam, data []byte, start, end int64) ([]string, error) {
t.Helper()
if end == -1 {
end = int64(len(data))
}
parser, err := newCSVParserFromReader(param, bytes.NewReader(data[start:end]))
if err != nil {
return nil, err
}
var got []string
var fields []csvparser.Field
for {
fields, err = parser.Read(fields)
if err == io.EOF {
return got, nil
}
if err != nil {
return got, err
}
row := ""
for i, f := range fields {
if i > 0 {
row += ","
}
// Field.Val may alias the parser's internal buffer; clone before
// retaining it past the next Read.
row += strings.Clone(f.Val)
}
got = append(got, row)
}
}
func loomStrictExtern(fs fileservice.FileService, ctx context.Context, path string) *tree.ExternParam {
return &tree.ExternParam{
ExParamConst: tree.ExParamConst{
Filepath: path, FileStartOff: 0, Format: tree.CSV, ScanType: tree.INFILE,
CompressType: tree.NOCOMPRESS, Tail: &tree.TailParameter{},
},
ExParam: tree.ExParam{
FileService: fs, Ctx: ctx, Parallel: true, Strict: true,
ExternType: int32(plan.ExternType_LOAD),
},
}
}
// TestLoomStrictParallelSplitsMultilineQuotedRecord proves that the strict
// parallel LOAD DATA range planner selects a byte boundary INSIDE a quoted
// multiline field: the first chunk then ends with an unterminated quoted
// field and no longer parses, while the second chunk starts mid-field and
// emits a bogus row. The serial parse of the same file has no such problem.
func TestLoomStrictParallelSplitsMultilineQuotedRecord(t *testing.T) {
data := []byte(loomStrictMultilineCSV)
require.Equal(t, 40, len(data), "fixture must stay 40 bytes so the guess lands inside the quoted field")
require.Equal(t, byte('a'), data[20], "guess (mcpu=2) must land inside the quoted field")
const path = "etl:/loom-strict-multiline.csv"
fs, ctx := loomWriteFile(t, path, data)
param := loomStrictExtern(fs, ctx, path)
serial, err := loomParseRange(t, param, data, 0, -1)
require.NoError(t, err)
require.Equal(t,
[]string{"1,a", "1,a", "1,a", "1,a", "2,aaa,999\nmore,xyz", "3,c"},
serial, "sanity: the serial parse is well-formed")
offsets, err := ReadFileOffset(param, 2, int64(len(data)), loomVarcharCols())
require.NoError(t, err)
require.Equal(t, int64(0), offsets[0])
require.Equal(t, int64(-1), offsets[len(offsets)-1])
var got []string
for i := 0; i < len(offsets); i += 2 {
rows, perr := loomParseRange(t, param, data, offsets[i], offsets[i+1])
require.NoErrorf(t, perr,
"strict parallel range [%d,%d) must stay parseable; offsets=%v (file parses fine serially)",
offsets[i], offsets[i+1], offsets)
got = append(got, rows...)
}
require.Equal(t, serial, got, "strict parallel split must reproduce the serial parse")
}
// loomReadRanges drives the production CsvReader (Open/ReadBatch) over the
// given ranges, the same way compileExternScanParallelReadWrite does.
func loomReadRanges(t *testing.T, extern *tree.ExternParam, path string, data []byte, offsets []int64) ([]string, error) {
t.Helper()
proc := testutil.NewProcess(t, testutil.WithFileService(extern.FileService))
proc.Base.SessionInfo.Account = "sys"
proc.Base.SessionInfo.Role = "moadmin"
defer proc.Free()
var rows []string
for idx := 0; 2*idx < len(offsets); idx++ {
rangeOffsets := offsets[2*idx : 2*idx+2]
if err := func() error {
param := &ExternalParam{
ExParamConst: ExParamConst{
Idx: idx, Extern: extern, ColumnListLen: 2, maxBatchSize: 1 << 20,
Attrs: []plan.ExternAttr{
{ColName: "id", ColIndex: 0, ColFieldIndex: 0},
{ColName: "v", ColIndex: 1, ColFieldIndex: 1},
},
Cols: loomVarcharCols(),
FileSize: []int64{int64(len(data))},
FileOffsetTotal: []*pipeline.FileOffset{{Offset: rangeOffsets}},
},
ExParam: ExParam{Fileparam: &ExFileparam{FileIndex: 1, FileCnt: 1, Filepath: path}},
}
reader, err := NewCsvReader(param, proc)
if err != nil {
return err
}
defer reader.Close()
empty, err := reader.Open(param, proc)
if err != nil {
return err
}
if empty {
return nil
}
for {
bat := batch.NewWithSize(2)
bat.Vecs[0] = vector.NewVec(types.T_varchar.ToType())
bat.Vecs[1] = vector.NewVec(types.T_varchar.ToType())
finished, err := reader.ReadBatch(proc.Ctx, bat, proc, nil)
if err != nil {
bat.Clean(proc.Mp())
return err
}
for row := 0; row < bat.RowCount(); row++ {
rows = append(rows,
bat.Vecs[0].GetStringAt(row)+","+bat.Vecs[1].GetStringAt(row))
}
bat.Clean(proc.Mp())
if finished {
return nil
}
}
}(); err != nil {
return rows, err
}
}
return rows, nil
}
// TestLoomStrictParallelLoadReaderMultilineQuotedRecord is the reader-level
// reproduction: the same valid file loads fine through one serial CsvReader
// but the strict parallel readers either fail with "unterminated quoted
// field" (first chunk) or ingest a fabricated row (second chunk).
func TestLoomStrictParallelLoadReaderMultilineQuotedRecord(t *testing.T) {
data := []byte(loomStrictMultilineCSV)
const path = "etl:/loom-strict-multiline-reader.csv"
fs, ctx := loomWriteFile(t, path, data)
extern := loomStrictExtern(fs, ctx, path)
serial, err := loomReadRanges(t, extern, path, data, []int64{0, -1})
require.NoError(t, err)
require.Equal(t,
[]string{"1,a", "1,a", "1,a", "1,a", "2,aaa,999\nmore,xyz", "3,c"},
serial, "sanity: a single (serial) reader loads the file")
offsets, err := ReadFileOffset(extern, 2, int64(len(data)), loomVarcharCols())
require.NoError(t, err)
parallel, err := loomReadRanges(t, extern, path, data, offsets)
require.NoErrorf(t, err,
"strict parallel LOAD must load a file the serial reader accepts; offsets=%v, partial rows=%q",
offsets, parallel)
require.Equal(t, serial, parallel,
"strict parallel LOAD must reproduce the serial result; offsets=%v, parallel rows=%q", offsets, parallel)
}
// TestLoomStrictParallelSecondChunkFabricatesRow documents what the second
// chunk of the broken split ingests: it starts mid-field at byte 27, so the
// reader fabricates a row ("more,xyz\"") that exists nowhere in the file,
// while the real record that starts at byte 16 is lost.
func TestLoomStrictParallelSecondChunkFabricatesRow(t *testing.T) {
data := []byte(loomStrictMultilineCSV)
const path = "etl:/loom-strict-second-chunk.csv"
fs, ctx := loomWriteFile(t, path, data)
param := loomStrictExtern(fs, ctx, path)
offsets, err := ReadFileOffset(param, 2, int64(len(data)), loomVarcharCols())
require.NoError(t, err)
require.Greater(t, len(offsets), 3, "the strict split must have produced a second range")
secondStart := offsets[2]
rows, err := loomParseRange(t, param, data, secondStart, offsets[3])
require.NoError(t, err, "the fabricated-row demonstration must not error")
serial, err := loomParseRange(t, param, data, 0, -1)
require.NoError(t, err)
for _, r := range rows {
require.Contains(t, serial, r,
"every row the parallel second chunk ingests must exist in the serial parse (secondStart=%d)", secondStart)
}
}
// Control: the very same file with the same two-way byte split is handled
// correctly by the non-strict planner (it resynchronizes from the previous
// real record start), so the divergence above is specific to strict mode's
// mid-record resync.
func TestLoomControlNonStrictParallelSameFile(t *testing.T) {
data := []byte(loomStrictMultilineCSV)
const path = "etl:/loom-control-nonstrict.csv"
fs, ctx := loomWriteFile(t, path, data)
param := loomStrictExtern(fs, ctx, path)
param.Strict = false
serial, err := loomParseRange(t, param, data, 0, -1)
require.NoError(t, err)
require.Equal(t, 6, len(serial))
offsets, err := ReadFileOffset(param, 2, int64(len(data)), nil)
require.NoError(t, err)
require.Len(t, offsets, 4)
var got []string
for i := 0; i < len(offsets); i += 2 {
rows, perr := loomParseRange(t, param, data, offsets[i], offsets[i+1])
require.NoErrorf(t, perr, "non-strict range [%d,%d) must stay parseable; offsets=%v", offsets[i], offsets[i+1], offsets)
got = append(got, rows...)
}
require.Equal(t, serial, got)
}
// Control: strict mode with a single reader keeps the whole file in one
// range, so the file itself is not the problem.
func TestLoomControlStrictSingleReaderSameFile(t *testing.T) {
data := []byte(loomStrictMultilineCSV)
const path = "etl:/loom-control-strict-serial.csv"
fs, ctx := loomWriteFile(t, path, data)
param := loomStrictExtern(fs, ctx, path)
serial, err := loomParseRange(t, param, data, 0, -1)
require.NoError(t, err)
offsets, err := ReadFileOffset(param, 1, int64(len(data)), loomVarcharCols())
require.NoError(t, err)
require.Equal(t, []int64{0, -1}, offsets)
got, err := loomParseRange(t, param, data, offsets[0], offsets[1])
require.NoError(t, err)
require.Equal(t, serial, got)
}Found by Loom bug hunt (module: custom-de41a00730; tests: unit, white-box, concurrency; severity: S1)
Activity