Skip to content

Commit

Permalink
util: fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
SunRunAway committed Aug 22, 2019
1 parent 61974e8 commit 739891d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
24 changes: 15 additions & 9 deletions util/chunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,30 @@ func New(fields []*types.FieldType, cap, maxChunkSize int) *Chunk {
return chk
}

// Renew creates a new Chunk based on an existing Chunk. The newly created Chunk
// has the same data schema with the old Chunk. The capacity of the new Chunk
// might be doubled based on the capacity of the old Chunk and the maxChunkSize.
// chk: old chunk(often used in previous call).
// maxChunkSize: the limit for the max number of rows.
func Renew(chk *Chunk, maxChunkSize int) *Chunk {
// renewWithCapacity creates a new Chunk based on an existing Chunk with capacity. The newly
// created Chunk has the same data schema with the old Chunk.
func renewWithCapacity(chk *Chunk, cap, maxChunkSize int) *Chunk {
newChk := new(Chunk)
if chk.columns == nil {
return newChk
}
newCap := reCalcCapacity(chk, maxChunkSize)
newChk.columns = renewColumns(chk.columns, newCap)
newChk.columns = renewColumns(chk.columns, cap)
newChk.numVirtualRows = 0
newChk.capacity = newCap
newChk.capacity = cap
newChk.requiredRows = maxChunkSize
return newChk
}

// Renew creates a new Chunk based on an existing Chunk. The newly created Chunk
// has the same data schema with the old Chunk. The capacity of the new Chunk
// might be doubled based on the capacity of the old Chunk and the maxChunkSize.
// chk: old chunk(often used in previous call).
// maxChunkSize: the limit for the max number of rows.
func Renew(chk *Chunk, maxChunkSize int) *Chunk {
newCap := reCalcCapacity(chk, maxChunkSize)
return renewWithCapacity(chk, newCap, maxChunkSize)
}

// renewColumns creates the columns of a Chunk. The capacity of the newly
// created columns is equal to cap.
func renewColumns(oldCol []*column, cap int) []*column {
Expand Down
7 changes: 7 additions & 0 deletions util/chunk/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,10 @@ func (r Row) GetDatum(colIdx int, tp *types.FieldType) types.Datum {
func (r Row) IsNull(colIdx int) bool {
return r.c.columns[colIdx].isNull(r.idx)
}

// CopyConstruct creates a new row and copies this row's data into it.
func (r Row) CopyConstruct() Row {
newChk := renewWithCapacity(r.c, 1, 1)
newChk.AppendRow(r)
return newChk.GetRow(0)
}

0 comments on commit 739891d

Please sign in to comment.