Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

mydumper: do not remove more than 1 sep if trim last sep is true #535

Merged
merged 2 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lightning/mydump/csv_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,12 @@ func (parser *CSVParser) ReadRow() error {
return errors.Trace(err)
}
parser.lastRecord = records
// remove trailing empty values
// remove the last empty value
if parser.cfg.TrimLastSep {
var i int
for i = len(records); i > 0 && len(records[i-1]) == 0; i-- {
i := len(records) - 1
if i >= 0 && len(records[i]) == 0 {
records = records[:i]
}
records = records[:i]
}

row.Row = parser.acquireDatumSlice()
Expand Down
20 changes: 20 additions & 0 deletions lightning/mydump/csv_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,26 @@ func (s *testMydumpCSVParserSuite) TestSyntaxErrorLog(c *C) {
)
}

// TestTrimLastSep checks that set `TrimLastSep` to true trim only the last empty filed.
func (s *testMydumpCSVParserSuite) TestTrimLastSep(c *C) {
cfg := config.CSVConfig{
Separator: ",",
Delimiter: `"`,
TrimLastSep: true,
}
parser := mydump.NewCSVParser(
&cfg,
mydump.NewStringReader("123,456,789,\r\na,b,,\r\n,,,\r\n\"a\",\"\",\"\",\r\n"),
int64(config.ReadBlockSize),
s.ioWorkers,
false,
)
for i := 0; i < 4; i++ {
c.Assert(parser.ReadRow(), IsNil)
c.Assert(len(parser.LastRow().Row), Equals, 3)
}
}

// Run `go test github.com/pingcap/tidb-lightning/lightning/mydump -check.b -check.bmem -test.v` to get benchmark result.
// Please ensure your temporary storage has (c.N / 2) KiB of free space.

Expand Down