This is an automated email from the ASF dual-hosted git repository.

zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 670cf3b820 GH-37694: [Go] Add SetNull to array builders (#37695)
670cf3b820 is described below

commit 670cf3b820ade8f553a81ba5b3346b74734cf972
Author: Matthias Loibl <[email protected]>
AuthorDate: Thu Sep 14 18:12:57 2023 +0100

    GH-37694: [Go] Add SetNull to array builders (#37695)
    
    
    
    ### Rationale for this change
    
    We are proposing to add a `SetNull(i int)` to the `Builder` interface.
    The underlying `builder` type implements this for most types.
    
    ### What changes are included in this PR?
    
    ### Are these changes tested?
    
    ### Are there any user-facing changes?
    
    The SetNull is added to the Builders type.
    
    * Closes: #37694
    
    Authored-by: Matthias Loibl <[email protected]>
    Signed-off-by: Matt Topol <[email protected]>
---
 go/arrow/array/builder.go      | 10 ++++++++++
 go/arrow/array/builder_test.go | 24 ++++++++++++++++++++++++
 go/arrow/array/map.go          |  4 ++++
 go/arrow/array/map_test.go     | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 73 insertions(+)

diff --git a/go/arrow/array/builder.go b/go/arrow/array/builder.go
index aada095d09..58d4a0f4b8 100644
--- a/go/arrow/array/builder.go
+++ b/go/arrow/array/builder.go
@@ -86,6 +86,9 @@ type Builder interface {
        // IsNull returns if a previously appended value at a given index is 
null or not.
        IsNull(i int) bool
 
+       // SetNull sets the value at index i to null.
+       SetNull(i int)
+
        UnsafeAppendBoolToBitmap(bool)
 
        init(capacity int)
@@ -126,6 +129,13 @@ func (b *builder) IsNull(i int) bool {
        return b.nullBitmap.Len() != 0 && 
bitutil.BitIsNotSet(b.nullBitmap.Bytes(), i)
 }
 
+func (b *builder) SetNull(i int) {
+       if i < 0 || i >= b.length {
+               panic("arrow/array: index out of range")
+       }
+       bitutil.ClearBit(b.nullBitmap.Bytes(), i)
+}
+
 func (b *builder) init(capacity int) {
        toAlloc := bitutil.CeilByte(capacity) / 8
        b.nullBitmap = memory.NewResizableBuffer(b.mem)
diff --git a/go/arrow/array/builder_test.go b/go/arrow/array/builder_test.go
index eeb7a2ac46..3cacb54f72 100644
--- a/go/arrow/array/builder_test.go
+++ b/go/arrow/array/builder_test.go
@@ -97,3 +97,27 @@ func TestBuilder_IsNull(t *testing.T) {
                assert.Equal(t, i%2 != 0, b.IsNull(i))
        }
 }
+
+func TestBuilder_SetNull(t *testing.T) {
+       b := &builder{mem: memory.NewGoAllocator()}
+       n := 32
+       b.init(n)
+
+       for i := 0; i < n; i++ {
+               // Set everything to true
+               b.UnsafeAppendBoolToBitmap(true)
+       }
+       for i := 0; i < n; i++ {
+               if i%2 == 0 { // Set all even numbers to null
+                       b.SetNull(i)
+               }
+       }
+
+       for i := 0; i < n; i++ {
+               if i%2 == 0 {
+                       assert.True(t, b.IsNull(i))
+               } else {
+                       assert.False(t, b.IsNull(i))
+               }
+       }
+}
diff --git a/go/arrow/array/map.go b/go/arrow/array/map.go
index 84c1b55a0c..4fe860f26e 100644
--- a/go/arrow/array/map.go
+++ b/go/arrow/array/map.go
@@ -234,6 +234,10 @@ func (b *MapBuilder) AppendNulls(n int) {
        }
 }
 
+func (b *MapBuilder) SetNull(i int) {
+       b.listBuilder.SetNull(i)
+}
+
 func (b *MapBuilder) AppendEmptyValue() {
        b.Append(true)
 }
diff --git a/go/arrow/array/map_test.go b/go/arrow/array/map_test.go
index cfb1cac87b..3fe78549ec 100644
--- a/go/arrow/array/map_test.go
+++ b/go/arrow/array/map_test.go
@@ -217,3 +217,38 @@ func TestMapStringRoundTrip(t *testing.T) {
 
        assert.True(t, array.Equal(arr, arr1))
 }
+
+func TestMapBuilder_SetNull(t *testing.T) {
+       pool := memory.NewCheckedAllocator(memory.NewGoAllocator())
+       defer pool.AssertSize(t, 0)
+
+       var (
+               arr          *array.Map
+               equalValid   = []bool{true, true, true, true, true, true, true}
+               equalOffsets = []int32{0, 1, 2, 5, 6, 7, 8, 10}
+               equalKeys    = []string{"a", "a", "a", "b", "c", "a", "a", "a", 
"a", "b"}
+               equalValues  = []int32{1, 2, 3, 4, 5, 2, 2, 2, 5, 6}
+       )
+
+       bldr := array.NewMapBuilder(pool, arrow.BinaryTypes.String, 
arrow.PrimitiveTypes.Int32, false)
+       defer bldr.Release()
+
+       kb := bldr.KeyBuilder().(*array.StringBuilder)
+       ib := bldr.ItemBuilder().(*array.Int32Builder)
+
+       bldr.AppendValues(equalOffsets, equalValid)
+       for _, k := range equalKeys {
+               kb.Append(k)
+       }
+       ib.AppendValues(equalValues, nil)
+
+       bldr.SetNull(0)
+       bldr.SetNull(3)
+
+       arr = bldr.NewMapArray()
+       defer arr.Release()
+
+       assert.True(t, arr.IsNull(0))
+       assert.True(t, arr.IsValid(1))
+       assert.True(t, arr.IsNull(3))
+}

Reply via email to