Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-4081: [Go] Sum methods panic when the array is empty #3906

Closed
wants to merge 5 commits into from
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
3 changes: 3 additions & 0 deletions go/arrow/math/float64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions go/arrow/math/float64_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions go/arrow/math/int64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions go/arrow/math/int64_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions go/arrow/math/type.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ var (

// Sum returns the summation of all elements in a.
func (f {{.Name}}Funcs) Sum(a *array.{{.Name}}) {{.Type}} {
if a.Len() == 0 {
return {{.Type}}(0)
}
return f.sum(a)
}

Expand Down
25 changes: 21 additions & 4 deletions go/arrow/math/type_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,28 @@ import (
{{$name := printf "%s_%s" .In.Type .D.arch}}
{{with .In}}
func Test{{.Name}}Funcs_Sum(t *testing.T) {
vec := makeArray{{.Name}}(10000)
mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
defer mem.AssertSize(t, 0)
vec := makeArray{{.Name}}(10000, mem)
defer vec.Release()
res := math.{{.Name}}.Sum(vec)
assert.Equal(t, res, {{.Type}}(49995000))
}

func makeArray{{.Name}}(l int) *array.{{.Name}} {
fb := array.New{{.Name}}Builder(memory.NewGoAllocator())
func Test{{.Name}}Funcs_SumEmpty(t *testing.T) {
mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
defer mem.AssertSize(t, 0)
b := array.New{{.Name}}Builder(mem)
defer b.Release()
vec := b.New{{.Name}}Array()
defer vec.Release()
res := math.{{.Name}}.Sum(vec)
assert.Equal(t, res, {{.Type}}(0))
}

func makeArray{{.Name}}(l int, mem memory.Allocator) *array.{{.Name}} {
fb := array.New{{.Name}}Builder(mem)
defer fb.Release()
fb.Reserve(l)
for i := 0; i < l; i++ {
fb.Append({{.Type}}(i))
Expand All @@ -43,7 +58,9 @@ func makeArray{{.Name}}(l int) *array.{{.Name}} {
}

func benchmark{{.Name}}Funcs_Sum(b *testing.B, n int) {
vec := makeArray{{.Name}}(n)
mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
defer mem.AssertSize(b, 0)
vec := makeArray{{.Name}}(n, mem)
b.SetBytes(int64(vec.Len() * 8))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand Down
3 changes: 3 additions & 0 deletions go/arrow/math/uint64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions go/arrow/math/uint64_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 10 additions & 11 deletions go/arrow/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ func (md Metadata) Len() int { return len(md.keys) }
func (md Metadata) Keys() []string { return md.keys }
func (md Metadata) Values() []string { return md.values }

func (kv Metadata) clone() Metadata {
if len(kv.keys) == 0 {
func (md Metadata) clone() Metadata {
if len(md.keys) == 0 {
return Metadata{}
}

o := Metadata{
keys: make([]string, len(kv.keys)),
values: make([]string, len(kv.values)),
keys: make([]string, len(md.keys)),
values: make([]string, len(md.values)),
}
copy(o.keys, kv.keys)
copy(o.values, kv.values)
copy(o.keys, md.keys)
copy(o.values, md.values)

return o
}
Expand Down Expand Up @@ -142,13 +142,12 @@ func (sc *Schema) HasMetadata() bool { return len(sc.meta.keys) > 0 }
// Equal returns whether two schema are equal.
// Equal does not compare the metadata.
func (sc *Schema) Equal(o *Schema) bool {
if sc == o {
switch {
case sc == o:
return true
}
if sc == nil || o == nil {
case sc == nil || o == nil:
return false
}
if len(sc.fields) != len(o.fields) {
case len(sc.fields) != len(o.fields):
return false
}

Expand Down