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

Commit

Permalink
mydumper: do not remove more than 1 sep if trim last sep is true (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
glorv authored Dec 29, 2020
1 parent 1c04266 commit 1f02abc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
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

0 comments on commit 1f02abc

Please sign in to comment.