Skip to content

Commit

Permalink
util: refine chunk.SwapColumn to re-build the column reference
Browse files Browse the repository at this point in the history
  • Loading branch information
XuHuaiyu committed Oct 8, 2018
1 parent 177c155 commit 8bf6c55
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions util/chunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,27 @@ func (c *Chunk) MakeRef(srcColIdx, dstColIdx int) {

// SwapColumn swaps column "c.columns[colIdx]" with column "other.columns[otherIdx]".
func (c *Chunk) SwapColumn(colIdx int, other *Chunk, otherIdx int) {
// If there exists columns refer to the column to be swapped, we need to
// re-build the reference.
refColsIdx := make([]int, 0, len(c.columns)-colIdx)
for i := colIdx + 1; i < len(c.columns); i++ {
if c.columns[i] == c.columns[colIdx] {
refColsIdx = append(refColsIdx, i)
}
}
refColsIdx4Other := make([]int, 0, len(other.columns)-otherIdx)
for i := otherIdx + 1; i < len(other.columns); i++ {
if other.columns[i] == other.columns[otherIdx] {
refColsIdx4Other = append(refColsIdx4Other, i)
}
}
c.columns[colIdx], other.columns[otherIdx] = other.columns[otherIdx], c.columns[colIdx]
}

// SwapColumns swaps columns with another Chunk.
func (c *Chunk) SwapColumns(other *Chunk) {
c.columns, other.columns = other.columns, c.columns
c.numVirtualRows, other.numVirtualRows = other.numVirtualRows, c.numVirtualRows
for _, i := range refColsIdx {
c.MakeRef(colIdx, i)
}
for _, i := range refColsIdx4Other {
other.MakeRef(otherIdx, i)
}
}

// SetNumVirtualRows sets the virtual row number for a Chunk.
Expand Down

0 comments on commit 8bf6c55

Please sign in to comment.