Skip to content
Draft
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
2 changes: 2 additions & 0 deletions arrow/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type ArrayData interface {
Dictionary() ArrayData
// SizeInBytes returns the size of the ArrayData buffers and any children and/or dictionary in bytes.
SizeInBytes() uint64

Equal(ArrayData) bool
}

// Array represents an immutable sequence of values using the Arrow in-memory format.
Expand Down
277 changes: 142 additions & 135 deletions arrow/array/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package array_test

import (
"runtime"
"testing"

"github.com/apache/arrow-go/v18/arrow"
Expand Down Expand Up @@ -202,147 +203,153 @@ func TestArraySlice(t *testing.T) {
vs = []float64{1, 2, 3, 0, 4, 5}
)

b := array.NewFloat64Builder(pool)
defer b.Release()

for _, tc := range []struct {
i, j int
panics bool
len int
}{
{i: 0, j: len(valids), panics: false, len: len(valids)},
{i: len(valids), j: len(valids), panics: false, len: 0},
{i: 0, j: 1, panics: false, len: 1},
{i: 1, j: 1, panics: false, len: 0},
{i: 0, j: len(valids) + 1, panics: true},
{i: 2, j: 1, panics: true},
{i: len(valids) + 1, j: len(valids) + 1, panics: true},
} {
t.Run("", func(t *testing.T) {
b.AppendValues(vs, valids)

arr := b.NewFloat64Array()
defer arr.Release()

if got, want := arr.Len(), len(valids); got != want {
t.Fatalf("got=%d, want=%d", got, want)
}

if tc.panics {
defer func() {
e := recover()
if e == nil {
t.Fatalf("this should have panicked, but did not")
}
}()
}

slice := array.NewSlice(arr, int64(tc.i), int64(tc.j)).(*array.Float64)
defer slice.Release()

if got, want := slice.Len(), tc.len; got != want {
t.Fatalf("invalid slice length: got=%d, want=%d", got, want)
}
})
{
b := array.NewFloat64Builder(pool)
defer b.Release()

for _, tc := range []struct {
i, j int
panics bool
len int
}{
{i: 0, j: len(valids), panics: false, len: len(valids)},
{i: len(valids), j: len(valids), panics: false, len: 0},
{i: 0, j: 1, panics: false, len: 1},
{i: 1, j: 1, panics: false, len: 0},
{i: 0, j: len(valids) + 1, panics: true},
{i: 2, j: 1, panics: true},
{i: len(valids) + 1, j: len(valids) + 1, panics: true},
} {
t.Run("", func(t *testing.T) {
b.AppendValues(vs, valids)

arr := b.NewFloat64Array()
defer arr.Release()

if got, want := arr.Len(), len(valids); got != want {
t.Fatalf("got=%d, want=%d", got, want)
}

if tc.panics {
defer func() {
e := recover()
if e == nil {
t.Fatalf("this should have panicked, but did not")
}
}()
}

slice := array.NewSlice(arr, int64(tc.i), int64(tc.j)).(*array.Float64)
defer slice.Release()

if got, want := slice.Len(), tc.len; got != want {
t.Fatalf("invalid slice length: got=%d, want=%d", got, want)
}
})
}
}
runtime.GC()
}

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

valids := []bool{true, true, true, false, true, true}

for _, tc := range []struct {
values interface{}
builder array.Builder
append func(b array.Builder, vs interface{})
}{
{
values: []bool{true, false, true, false, true, false},
builder: array.NewBooleanBuilder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.BooleanBuilder).AppendValues(vs.([]bool), valids) },
},
{
values: []uint8{1, 2, 3, 0, 4, 5},
builder: array.NewUint8Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Uint8Builder).AppendValues(vs.([]uint8), valids) },
},
{
values: []uint16{1, 2, 3, 0, 4, 5},
builder: array.NewUint16Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Uint16Builder).AppendValues(vs.([]uint16), valids) },
},
{
values: []uint32{1, 2, 3, 0, 4, 5},
builder: array.NewUint32Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Uint32Builder).AppendValues(vs.([]uint32), valids) },
},
{
values: []uint64{1, 2, 3, 0, 4, 5},
builder: array.NewUint64Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Uint64Builder).AppendValues(vs.([]uint64), valids) },
},
{
values: []int8{1, 2, 3, 0, 4, 5},
builder: array.NewInt8Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Int8Builder).AppendValues(vs.([]int8), valids) },
},
{
values: []int16{1, 2, 3, 0, 4, 5},
builder: array.NewInt16Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Int16Builder).AppendValues(vs.([]int16), valids) },
},
{
values: []int32{1, 2, 3, 0, 4, 5},
builder: array.NewInt32Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Int32Builder).AppendValues(vs.([]int32), valids) },
},
{
values: []int64{1, 2, 3, 0, 4, 5},
builder: array.NewInt64Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Int64Builder).AppendValues(vs.([]int64), valids) },
},
{
values: []float32{1, 2, 3, 0, 4, 5},
builder: array.NewFloat32Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Float32Builder).AppendValues(vs.([]float32), valids) },
},
{
values: []float64{1, 2, 3, 0, 4, 5},
builder: array.NewFloat64Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Float64Builder).AppendValues(vs.([]float64), valids) },
},
} {
t.Run("", func(t *testing.T) {
defer tc.builder.Release()

b := tc.builder
tc.append(b, tc.values)

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

if got, want := arr.Len(), len(valids); got != want {
t.Fatalf("invalid length: got=%d, want=%d", got, want)
}

slice := array.NewSlice(arr, 2, 5)
defer slice.Release()

if got, want := slice.Len(), 3; got != want {
t.Fatalf("invalid slice length: got=%d, want=%d", got, want)
}

shortSlice := array.NewSlice(arr, 2, 3)
defer shortSlice.Release()

sliceOfShortSlice := array.NewSlice(shortSlice, 0, 1)
defer sliceOfShortSlice.Release()

if got, want := sliceOfShortSlice.Len(), 1; got != want {
t.Fatalf("invalid short slice length: got=%d, want=%d", got, want)
}
})
{
valids := []bool{true, true, true, false, true, true}

for _, tc := range []struct {
values interface{}
builder array.Builder
append func(b array.Builder, vs interface{})
}{
{
values: []bool{true, false, true, false, true, false},
builder: array.NewBooleanBuilder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.BooleanBuilder).AppendValues(vs.([]bool), valids) },
},
{
values: []uint8{1, 2, 3, 0, 4, 5},
builder: array.NewUint8Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Uint8Builder).AppendValues(vs.([]uint8), valids) },
},
{
values: []uint16{1, 2, 3, 0, 4, 5},
builder: array.NewUint16Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Uint16Builder).AppendValues(vs.([]uint16), valids) },
},
{
values: []uint32{1, 2, 3, 0, 4, 5},
builder: array.NewUint32Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Uint32Builder).AppendValues(vs.([]uint32), valids) },
},
{
values: []uint64{1, 2, 3, 0, 4, 5},
builder: array.NewUint64Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Uint64Builder).AppendValues(vs.([]uint64), valids) },
},
{
values: []int8{1, 2, 3, 0, 4, 5},
builder: array.NewInt8Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Int8Builder).AppendValues(vs.([]int8), valids) },
},
{
values: []int16{1, 2, 3, 0, 4, 5},
builder: array.NewInt16Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Int16Builder).AppendValues(vs.([]int16), valids) },
},
{
values: []int32{1, 2, 3, 0, 4, 5},
builder: array.NewInt32Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Int32Builder).AppendValues(vs.([]int32), valids) },
},
{
values: []int64{1, 2, 3, 0, 4, 5},
builder: array.NewInt64Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Int64Builder).AppendValues(vs.([]int64), valids) },
},
{
values: []float32{1, 2, 3, 0, 4, 5},
builder: array.NewFloat32Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Float32Builder).AppendValues(vs.([]float32), valids) },
},
{
values: []float64{1, 2, 3, 0, 4, 5},
builder: array.NewFloat64Builder(pool),
append: func(b array.Builder, vs interface{}) { b.(*array.Float64Builder).AppendValues(vs.([]float64), valids) },
},
} {
t.Run("", func(t *testing.T) {
defer tc.builder.Release()

b := tc.builder
tc.append(b, tc.values)

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

if got, want := arr.Len(), len(valids); got != want {
t.Fatalf("invalid length: got=%d, want=%d", got, want)
}

slice := array.NewSlice(arr, 2, 5)
defer slice.Release()

if got, want := slice.Len(), 3; got != want {
t.Fatalf("invalid slice length: got=%d, want=%d", got, want)
}

shortSlice := array.NewSlice(arr, 2, 3)
defer shortSlice.Release()

sliceOfShortSlice := array.NewSlice(shortSlice, 0, 1)
defer sliceOfShortSlice.Release()

if got, want := sliceOfShortSlice.Len(), 1; got != want {
t.Fatalf("invalid short slice length: got=%d, want=%d", got, want)
}
})
}
}
runtime.GC()
}
73 changes: 38 additions & 35 deletions arrow/array/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package array

import (
"reflect"
"runtime"
"testing"

"github.com/apache/arrow-go/v18/arrow"
Expand All @@ -29,43 +30,45 @@ import (
func TestBinary(t *testing.T) {
mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
defer mem.AssertSize(t, 0)
{
b := NewBinaryBuilder(mem, arrow.BinaryTypes.Binary)

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

values := [][]byte{
[]byte("AAA"),
nil,
[]byte("BBBB"),
values := [][]byte{
[]byte("AAA"),
nil,
[]byte("BBBB"),
}
valid := []bool{true, false, true}
b.AppendValues(values, valid)

b.Retain()
b.Release()

a := b.NewBinaryArray()
assert.Equal(t, 3, a.Len())
assert.Equal(t, 1, a.NullN())
assert.Equal(t, []byte("AAA"), a.Value(0))
assert.Equal(t, []byte{}, a.Value(1))
assert.Equal(t, []byte("BBBB"), a.Value(2))
assert.Equal(t, "QUFB", a.ValueStr(0))
assert.Equal(t, NullValueStr, a.ValueStr(1))
a.Release()

// Test builder reset and NewArray API.
b.AppendValues(values, valid)
a = b.NewArray().(*Binary)
assert.Equal(t, 3, a.Len())
assert.Equal(t, 1, a.NullN())
assert.Equal(t, []byte("AAA"), a.Value(0))
assert.Equal(t, []byte{}, a.Value(1))
assert.Equal(t, []byte("BBBB"), a.Value(2))
assert.Equal(t, "QUFB", a.ValueStr(0))
assert.Equal(t, NullValueStr, a.ValueStr(1))
a.Release()

b.Release()
}
valid := []bool{true, false, true}
b.AppendValues(values, valid)

b.Retain()
b.Release()

a := b.NewBinaryArray()
assert.Equal(t, 3, a.Len())
assert.Equal(t, 1, a.NullN())
assert.Equal(t, []byte("AAA"), a.Value(0))
assert.Equal(t, []byte{}, a.Value(1))
assert.Equal(t, []byte("BBBB"), a.Value(2))
assert.Equal(t, "QUFB", a.ValueStr(0))
assert.Equal(t, NullValueStr, a.ValueStr(1))
a.Release()

// Test builder reset and NewArray API.
b.AppendValues(values, valid)
a = b.NewArray().(*Binary)
assert.Equal(t, 3, a.Len())
assert.Equal(t, 1, a.NullN())
assert.Equal(t, []byte("AAA"), a.Value(0))
assert.Equal(t, []byte{}, a.Value(1))
assert.Equal(t, []byte("BBBB"), a.Value(2))
assert.Equal(t, "QUFB", a.ValueStr(0))
assert.Equal(t, NullValueStr, a.ValueStr(1))
a.Release()

b.Release()
runtime.GC()
}

func TestLargeBinary(t *testing.T) {
Expand Down
Loading
Loading