Skip to content

Commit

Permalink
GH-36181: [Go] add methods AppendNulls and AppendEmptyValues for …
Browse files Browse the repository at this point in the history
…all builders (#36145)

### Rationale for this change
For convenience, so as not to call the method `AppendNull` or `AppendEmptyValue` many times.

### What changes are included in this PR?

### Are these changes tested?
Yes

### Are there any user-facing changes?
Yes

* Closes: #36181

Authored-by: izveigor <izveigor@gmail.com>
Signed-off-by: Matt Topol <zotthewizard@gmail.com>
  • Loading branch information
izveigor committed Jun 21, 2023
1 parent 5854d04 commit 5a491a7
Show file tree
Hide file tree
Showing 21 changed files with 411 additions and 11 deletions.
12 changes: 12 additions & 0 deletions go/arrow/array/binarybuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,24 @@ func (b *BinaryBuilder) AppendNull() {
b.UnsafeAppendBoolToBitmap(false)
}

func (b *BinaryBuilder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *BinaryBuilder) AppendEmptyValue() {
b.Reserve(1)
b.appendNextOffset()
b.UnsafeAppendBoolToBitmap(true)
}

func (b *BinaryBuilder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

// AppendValues will append the values in the v slice. The valid slice determines which values
// in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty,
// all values in v are appended and considered valid.
Expand Down
12 changes: 12 additions & 0 deletions go/arrow/array/booleanbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,23 @@ func (b *BooleanBuilder) AppendNull() {
b.UnsafeAppendBoolToBitmap(false)
}

func (b *BooleanBuilder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *BooleanBuilder) AppendEmptyValue() {
b.Reserve(1)
b.UnsafeAppend(false)
}

func (b *BooleanBuilder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

func (b *BooleanBuilder) AppendValueFromString(s string) error {
if s == NullValueStr {
b.AppendNull()
Expand Down
6 changes: 6 additions & 0 deletions go/arrow/array/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ type Builder interface {
// AppendNull adds a new null value to the array being built.
AppendNull()

// AppendNulls adds new n null values to the array being built.
AppendNulls(n int)

// AppendEmptyValue adds a new zero value of the appropriate type
AppendEmptyValue()

// AppendEmptyValues adds new n zero values of the appropriate type
AppendEmptyValues(n int)

// AppendValueFromString adds a new value from a string. Inverse of array.ValueStr(i int) string
AppendValueFromString(string) error

Expand Down
12 changes: 12 additions & 0 deletions go/arrow/array/decimal128.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,22 @@ func (b *Decimal128Builder) AppendNull() {
b.UnsafeAppendBoolToBitmap(false)
}

func (b *Decimal128Builder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *Decimal128Builder) AppendEmptyValue() {
b.Append(decimal128.Num{})
}

func (b *Decimal128Builder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

func (b *Decimal128Builder) UnsafeAppendBoolToBitmap(isValid bool) {
if isValid {
bitutil.SetBit(b.nullBitmap.Bytes(), b.length)
Expand Down
12 changes: 12 additions & 0 deletions go/arrow/array/decimal256.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,22 @@ func (b *Decimal256Builder) AppendNull() {
b.UnsafeAppendBoolToBitmap(false)
}

func (b *Decimal256Builder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *Decimal256Builder) AppendEmptyValue() {
b.Append(decimal256.Num{})
}

func (b *Decimal256Builder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

func (b *Decimal256Builder) Type() arrow.DataType { return b.dtype }

func (b *Decimal256Builder) UnsafeAppendBoolToBitmap(isValid bool) {
Expand Down
12 changes: 12 additions & 0 deletions go/arrow/array/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,23 @@ func (b *dictionaryBuilder) AppendNull() {
b.idxBuilder.AppendNull()
}

func (b *dictionaryBuilder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *dictionaryBuilder) AppendEmptyValue() {
b.length += 1
b.idxBuilder.AppendEmptyValue()
}

func (b *dictionaryBuilder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

func (b *dictionaryBuilder) Reserve(n int) {
b.idxBuilder.Reserve(n)
}
Expand Down
4 changes: 1 addition & 3 deletions go/arrow/array/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,9 +964,7 @@ func TestNullDictionaryBuilderBasic(t *testing.T) {
defer bldr.Release()

builder := bldr.(*array.NullDictionaryBuilder)
builder.AppendNull()
builder.AppendNull()
builder.AppendNull()
builder.AppendNulls(3)
assert.Equal(t, 3, builder.Len())
assert.Equal(t, 3, builder.NullN())

Expand Down
14 changes: 14 additions & 0 deletions go/arrow/array/encoded.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,25 +355,35 @@ func (b *RunEndEncodedBuilder) finishRun() {
}

func (b *RunEndEncodedBuilder) ValueBuilder() Builder { return b.values }

func (b *RunEndEncodedBuilder) Append(n uint64) {
b.finishRun()
b.addLength(n)
}

func (b *RunEndEncodedBuilder) AppendRuns(runs []uint64) {
for _, r := range runs {
b.finishRun()
b.addLength(r)
}
}

func (b *RunEndEncodedBuilder) ContinueRun(n uint64) {
b.addLength(n)
}

func (b *RunEndEncodedBuilder) AppendNull() {
b.finishRun()
b.values.AppendNull()
b.addLength(1)
}

func (b *RunEndEncodedBuilder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *RunEndEncodedBuilder) NullN() int {
return UnknownNullCount
}
Expand All @@ -382,6 +392,10 @@ func (b *RunEndEncodedBuilder) AppendEmptyValue() {
b.AppendNull()
}

func (b *RunEndEncodedBuilder) AppendEmptyValues(n int) {
b.AppendNulls(n)
}

func (b *RunEndEncodedBuilder) Reserve(n int) {
b.values.Reserve(n)
b.runEnds.Reserve(n)
Expand Down
13 changes: 13 additions & 0 deletions go/arrow/array/fixed_size_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,26 @@ func (b *FixedSizeListBuilder) AppendNull() {
}
}

// AppendNulls will append n null values to the underlying values by itself
func (b *FixedSizeListBuilder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *FixedSizeListBuilder) AppendEmptyValue() {
b.Append(true)
for i := int32(0); i < b.n; i++ {
b.values.AppendEmptyValue()
}
}

func (b *FixedSizeListBuilder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

func (b *FixedSizeListBuilder) AppendValues(valid []bool) {
b.Reserve(len(valid))
b.builder.unsafeAppendBoolsToBitmap(valid, len(valid))
Expand Down
12 changes: 12 additions & 0 deletions go/arrow/array/fixedsize_binarybuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,24 @@ func (b *FixedSizeBinaryBuilder) AppendNull() {
b.UnsafeAppendBoolToBitmap(false)
}

func (b *FixedSizeBinaryBuilder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *FixedSizeBinaryBuilder) AppendEmptyValue() {
b.Reserve(1)
b.values.Advance(b.dtype.ByteWidth)
b.UnsafeAppendBoolToBitmap(true)
}

func (b *FixedSizeBinaryBuilder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

func (b *FixedSizeBinaryBuilder) UnsafeAppend(v []byte) {
b.values.unsafeAppend(v)
b.UnsafeAppendBoolToBitmap(true)
Expand Down
12 changes: 12 additions & 0 deletions go/arrow/array/float16_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,23 @@ func (b *Float16Builder) AppendNull() {
b.UnsafeAppendBoolToBitmap(false)
}

func (b *Float16Builder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *Float16Builder) AppendEmptyValue() {
b.Reserve(1)
b.UnsafeAppend(float16.Num{})
}

func (b *Float16Builder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

func (b *Float16Builder) UnsafeAppendBoolToBitmap(isValid bool) {
if isValid {
bitutil.SetBit(b.nullBitmap.Bytes(), b.length)
Expand Down
36 changes: 36 additions & 0 deletions go/arrow/array/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,22 @@ func (b *MonthIntervalBuilder) AppendNull() {
b.UnsafeAppendBoolToBitmap(false)
}

func (b *MonthIntervalBuilder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *MonthIntervalBuilder) AppendEmptyValue() {
b.Append(arrow.MonthInterval(0))
}

func (b *MonthIntervalBuilder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

func (b *MonthIntervalBuilder) UnsafeAppend(v arrow.MonthInterval) {
bitutil.SetBit(b.nullBitmap.Bytes(), b.length)
b.rawData[b.length] = v
Expand Down Expand Up @@ -461,10 +473,22 @@ func (b *DayTimeIntervalBuilder) AppendNull() {
b.UnsafeAppendBoolToBitmap(false)
}

func (b *DayTimeIntervalBuilder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *DayTimeIntervalBuilder) AppendEmptyValue() {
b.Append(arrow.DayTimeInterval{})
}

func (b *DayTimeIntervalBuilder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

func (b *DayTimeIntervalBuilder) UnsafeAppend(v arrow.DayTimeInterval) {
bitutil.SetBit(b.nullBitmap.Bytes(), b.length)
b.rawData[b.length] = v
Expand Down Expand Up @@ -752,10 +776,22 @@ func (b *MonthDayNanoIntervalBuilder) AppendNull() {
b.UnsafeAppendBoolToBitmap(false)
}

func (b *MonthDayNanoIntervalBuilder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *MonthDayNanoIntervalBuilder) AppendEmptyValue() {
b.Append(arrow.MonthDayNanoInterval{})
}

func (b *MonthDayNanoIntervalBuilder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

func (b *MonthDayNanoIntervalBuilder) UnsafeAppend(v arrow.MonthDayNanoInterval) {
bitutil.SetBit(b.nullBitmap.Bytes(), b.length)
b.rawData[b.length] = v
Expand Down
12 changes: 12 additions & 0 deletions go/arrow/array/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,22 @@ func (b *baseListBuilder) AppendNull() {
b.appendNextOffset()
}

func (b *baseListBuilder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *baseListBuilder) AppendEmptyValue() {
b.Append(true)
}

func (b *baseListBuilder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

func (b *ListBuilder) AppendValues(offsets []int32, valid []bool) {
b.Reserve(len(valid))
b.offsets.(*Int32Builder).AppendValues(offsets, nil)
Expand Down
13 changes: 13 additions & 0 deletions go/arrow/array/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,23 @@ func (b *MapBuilder) AppendNull() {
b.Append(false)
}

// AppendNulls adds null map entry to the array.
func (b *MapBuilder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}

func (b *MapBuilder) AppendEmptyValue() {
b.Append(true)
}

func (b *MapBuilder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}

// Reserve enough space for n maps
func (b *MapBuilder) Reserve(n int) { b.listBuilder.Reserve(n) }

Expand Down

0 comments on commit 5a491a7

Please sign in to comment.