Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions go/arrow/array/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package array

import (
"fmt"
"strings"
"unsafe"

"github.com/apache/arrow/go/arrow"
Expand Down Expand Up @@ -80,6 +82,24 @@ func (a *Binary) ValueBytes() []byte {
return a.valueBytes[a.valueOffsets[beg]:a.valueOffsets[end]]
}

func (a *Binary) String() string {
o := new(strings.Builder)
o.WriteString("[")
for i := 0; i < a.Len(); i++ {
if i > 0 {
o.WriteString(" ")
}
switch {
case a.IsNull(i):
o.WriteString("(null)")
default:
fmt.Fprintf(o, "%q", a.ValueString(i))
}
}
o.WriteString("]")
return o.String()
}

func (a *Binary) setData(data *Data) {
if len(data.buffers) != 3 {
panic("len(data.buffers) != 3")
Expand Down
23 changes: 23 additions & 0 deletions go/arrow/array/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,26 @@ func TestBinaryValueBytes(t *testing.T) {

assert.Equal(t, []byte{'h', 'i', 'j', 'k', 'l', 'm', 'o', 'p', 'q'}, slice.ValueBytes())
}

func TestBinaryStringer(t *testing.T) {
mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
defer mem.AssertSize(t, 0)

values := []string{"a", "bc", "", "é", "", "hijk", "lm", "", "opq", "", "tu"}
valids := []bool{true, true, false, true, false, true, true, true, true, false, true}

b := NewBinaryBuilder(mem, arrow.BinaryTypes.Binary)
defer b.Release()

b.AppendStringValues(values, valids)

arr := b.NewArray().(*Binary)
defer arr.Release()

got := arr.String()
want := `["a" "bc" (null) "é" (null) "hijk" "lm" "" "opq" (null) "tu"]`

if got != want {
t.Fatalf("invalid stringer:\ngot= %s\nwant=%s\n", got, want)
}
}
5 changes: 5 additions & 0 deletions go/arrow/array/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (a *List) Len() int { return a.array.Len() }

func (a *List) Offsets() []int32 { return a.offsets }

func (a *List) Retain() {
a.array.Retain()
a.values.Retain()
}

func (a *List) Release() {
a.array.Release()
a.values.Release()
Expand Down
3 changes: 3 additions & 0 deletions go/arrow/array/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func TestListArray(t *testing.T) {
arr := lb.NewArray().(*array.List)
defer arr.Release()

arr.Retain()
arr.Release()

if got, want := arr.DataType().ID(), arrow.LIST; got != want {
t.Fatalf("got=%v, want=%v", got, want)
}
Expand Down
3 changes: 2 additions & 1 deletion go/arrow/array/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func NewStringData(data *Data) *String {
}

// Value returns the slice at index i. This value should not be mutated.
func (a *String) Value(i int) string { return a.values[a.offsets[i]:a.offsets[i+1]] }
func (a *String) Value(i int) string { return a.values[a.offsets[i]:a.offsets[i+1]] }
func (a *String) ValueOffset(i int) int { return int(a.offsets[i]) }

func (a *String) String() string {
o := new(strings.Builder)
Expand Down
12 changes: 10 additions & 2 deletions go/arrow/array/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func TestStringArray(t *testing.T) {
defer mem.AssertSize(t, 0)

var (
want = []string{"hello", "世界", "", "bye"}
valids = []bool{true, true, false, true}
want = []string{"hello", "世界", "", "bye"}
valids = []bool{true, true, false, true}
offsets = []int{0, 5, 11, 11, 14}
)

sb := array.NewStringBuilder(mem)
Expand Down Expand Up @@ -79,6 +80,13 @@ func TestStringArray(t *testing.T) {
t.Fatalf("arr[%d]: got=%q, want=%q", i, got, want[i])
}
}

if got, want := arr.ValueOffset(i), offsets[i]; got != want {
t.Fatalf("arr-offset-beg[%d]: got=%d, want=%d", i, got, want)
}
if got, want := arr.ValueOffset(i+1), offsets[i+1]; got != want {
t.Fatalf("arr-offset-end[%d]: got=%d, want=%d", i+1, got, want)
}
}

sub := array.MakeFromData(arr.Data())
Expand Down
7 changes: 7 additions & 0 deletions go/arrow/array/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func (a *Struct) setData(data *Data) {
}
}

func (a *Struct) Retain() {
a.array.Retain()
for _, f := range a.fields {
f.Retain()
}
}

func (a *Struct) Release() {
a.array.Release()
for _, f := range a.fields {
Expand Down
3 changes: 3 additions & 0 deletions go/arrow/array/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func TestStructArray(t *testing.T) {
arr := sb.NewArray().(*array.Struct)
defer arr.Release()

arr.Retain()
arr.Release()

if got, want := arr.DataType().ID(), arrow.STRUCT; got != want {
t.Fatalf("got=%v, want=%v", got, want)
}
Expand Down
Loading