Repository: pingcap/tidb
File: pkg/util/chunk/column.go (Go)

Code before:
	newCol := &Column{length: c.length}
	newCol.nullBitmap = append(newCol.nullBitmap, c.nullBitmap...)
	newCol.offsets = append(newCol.offsets, c.offsets...)
	newCol.data = append(newCol.data, c.data...)
	newCol.elemBuf = append(newCol.elemBuf, c.elemBuf...)
	return newCol
}

// AppendNullBitmap append a null/notnull value to the column's null map
func (c *Column) AppendNullBitmap(notNull bool) {
	c.appendNullBitmap(notNull)
}

func (c *Column) appendNullBitmap(notNull bool) {
	idx := c.length >> 3
	if idx >= len(c.nullBitmap) {
		c.nullBitmap = append(c.nullBitmap, 0)
	}
	if notNull {
		pos := uint(c.length) & 7
		c.nullBitmap[idx] |= byte(1 << pos)
	}
}

// Reserve allocates some memory for the column
func (c *Column) Reserve(nullBitmapExpectedCap int64, dataExpectedCap int64, offsetExpectedCap int64) {
	currentNullBitmapLen := int64(len(c.nullBitmap))
	nullBitmapExpectedCap += currentNullBitmapLen
	if int64(cap(c.nullBitmap)) < nullBitmapExpectedCap {
		c.nullBitmap = slices.Grow(c.nullBitmap, int(nullBitmapExpectedCap))
	}

	currentDataLen := int64(len(c.data))
	dataExpectedCap += currentDataLen
	if int64(cap(c.data)) < dataExpectedCap {
		c.data = slices.Grow(c.data, int(dataExpectedCap))
	}

	currentOffsetLen := int64(len(c.offsets))
	offsetExpectedCap += currentOffsetLen
	if int64(cap(c.offsets)) < offsetExpectedCap {
		c.offsets = slices.Grow(c.offsets, int(offsetExpectedCap))
	}
}

func (c *Column) calculateLenDeltaForAppendCellNTimesForNullBitMap(times int) int64 {
	nullBitMapLenDelta := int64(0)
	if times == 1 {
		if c.length>>3 >= len(c.nullBitmap) {
			nullBitMapLenDelta++
		}

Review comment (suggestion):
It looks to me all the arguments are the extra needed cap, `XXXExpectedCap` is not a good name in this case.

Code after:
	return nullByte&(1<<(uint(rowIdx)&7)) == 0
}

// CopyConstruct copies this Column to dst.
// If dst is nil, it creates a new Column and returns it.
func (c *Column) CopyConstruct(dst *Column) *Column {
	if dst != nil {
		dst.length = c.length
		dst.nullBitmap = append(dst.nullBitmap[:0], c.nullBitmap...)
		dst.offsets = append(dst.offsets[:0], c.offsets...)
		dst.data = append(dst.data[:0], c.data...)
		dst.elemBuf = append(dst.elemBuf[:0], c.elemBuf...)
		return dst
	}
	newCol := &Column{length: c.length}
	newCol.nullBitmap = append(newCol.nullBitmap, c.nullBitmap...)
	newCol.offsets = append(newCol.offsets, c.offsets...)
	newCol.data = append(newCol.data, c.data...)
	newCol.elemBuf = append(newCol.elemBuf, c.elemBuf...)
	return newCol
}

// AppendNullBitmap append a null/notnull value to the column's null map
func (c *Column) AppendNullBitmap(notNull bool) {
	c.appendNullBitmap(notNull)
}

func (c *Column) appendNullBitmap(notNull bool) {
	idx := c.length >> 3
	if idx >= len(c.nullBitmap) {
		c.nullBitmap = append(c.nullBitmap, 0)
	}
	if notNull {
		pos := uint(c.length) & 7
		c.nullBitmap[idx] |= byte(1 << pos)
	}
}

// Reserve allocates some memory for the column
func (c *Column) Reserve(moreBytesNumNullBitmapNeed int64, moreBytesNumDataNeed int64, moreBytesNumOffsetNeed int64) {
	currentNullBitmapLen := int64(len(c.nullBitmap))
	nullBitmapExpectedCap := currentNullBitmapLen + moreBytesNumNullBitmapNeed
	if int64(cap(c.nullBitmap)) < nullBitmapExpectedCap {
		c.nullBitmap = slices.Grow(c.nullBitmap, int(nullBitmapExpectedCap))
	}

	currentDataLen := int64(len(c.data))
	dataExpectedCap := currentDataLen + moreBytesNumDataNeed
	if int64(cap(c.data)) < dataExpectedCap {
		c.data = slices.Grow(c.data, int(dataExpectedCap))
	}