diff --git a/lightning/mydump/csv_parser.go b/lightning/mydump/csv_parser.go index d89eac0cd..fb512572a 100644 --- a/lightning/mydump/csv_parser.go +++ b/lightning/mydump/csv_parser.go @@ -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() diff --git a/lightning/mydump/csv_parser_test.go b/lightning/mydump/csv_parser_test.go index 50050551e..2eb7d2126 100644 --- a/lightning/mydump/csv_parser_test.go +++ b/lightning/mydump/csv_parser_test.go @@ -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.