You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by sb...@apache.org on 2019/03/18 07:24:07 UTC

[arrow] branch master updated: ARROW-4081: [Go] Sum methods panic when the array is empty

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 699878d  ARROW-4081: [Go] Sum methods panic when the array is empty
699878d is described below

commit 699878dfcac4a14811537135cf94adff962b1e52
Author: alexandreyc <al...@gmail.com>
AuthorDate: Mon Mar 18 08:23:54 2019 +0100

    ARROW-4081: [Go] Sum methods panic when the array is empty
    
    Nothing fundamental but more idiomatic and coherent.
    
    Author: alexandreyc <al...@gmail.com>
    
    Closes #3906 from alexandreyc/master and squashes the following commits:
    
    e9142353 <alexandreyc> Fix memory releasing
    0b2c110c <alexandreyc> Fix ARROW-4081
    3e3136a5 <alexandreyc> Merge remote-tracking branch 'upstream/master'
    71ce16e9 <alexandreyc> Update ifs to switch for Schema.equal
    22c75f9d <alexandreyc> Fix receiver name for Metadata.clone()
---
 go/arrow/math/float64.go        |  3 +++
 go/arrow/math/float64_test.go   | 25 +++++++++++++++++++++----
 go/arrow/math/int64.go          |  3 +++
 go/arrow/math/int64_test.go     | 25 +++++++++++++++++++++----
 go/arrow/math/type.go.tmpl      |  3 +++
 go/arrow/math/type_test.go.tmpl | 25 +++++++++++++++++++++----
 go/arrow/math/uint64.go         |  3 +++
 go/arrow/math/uint64_test.go    | 25 +++++++++++++++++++++----
 go/arrow/schema.go              | 21 ++++++++++-----------
 9 files changed, 106 insertions(+), 27 deletions(-)

diff --git a/go/arrow/math/float64.go b/go/arrow/math/float64.go
index a9c5466..ccd2d85 100644
--- a/go/arrow/math/float64.go
+++ b/go/arrow/math/float64.go
@@ -32,6 +32,9 @@ var (
 
 // Sum returns the summation of all elements in a.
 func (f Float64Funcs) Sum(a *array.Float64) float64 {
+	if a.Len() == 0 {
+		return float64(0)
+	}
 	return f.sum(a)
 }
 
diff --git a/go/arrow/math/float64_test.go b/go/arrow/math/float64_test.go
index ad33e0f..9d450ef 100644
--- a/go/arrow/math/float64_test.go
+++ b/go/arrow/math/float64_test.go
@@ -28,13 +28,28 @@ import (
 )
 
 func TestFloat64Funcs_Sum(t *testing.T) {
-	vec := makeArrayFloat64(10000)
+	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+	defer mem.AssertSize(t, 0)
+	vec := makeArrayFloat64(10000, mem)
+	defer vec.Release()
 	res := math.Float64.Sum(vec)
 	assert.Equal(t, res, float64(49995000))
 }
 
-func makeArrayFloat64(l int) *array.Float64 {
-	fb := array.NewFloat64Builder(memory.NewGoAllocator())
+func TestFloat64Funcs_SumEmpty(t *testing.T) {
+	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+	defer mem.AssertSize(t, 0)
+	b := array.NewFloat64Builder(mem)
+	defer b.Release()
+	vec := b.NewFloat64Array()
+	defer vec.Release()
+	res := math.Float64.Sum(vec)
+	assert.Equal(t, res, float64(0))
+}
+
+func makeArrayFloat64(l int, mem memory.Allocator) *array.Float64 {
+	fb := array.NewFloat64Builder(mem)
+	defer fb.Release()
 	fb.Reserve(l)
 	for i := 0; i < l; i++ {
 		fb.Append(float64(i))
@@ -43,7 +58,9 @@ func makeArrayFloat64(l int) *array.Float64 {
 }
 
 func benchmarkFloat64Funcs_Sum(b *testing.B, n int) {
-	vec := makeArrayFloat64(n)
+	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+	defer mem.AssertSize(b, 0)
+	vec := makeArrayFloat64(n, mem)
 	b.SetBytes(int64(vec.Len() * 8))
 	b.ResetTimer()
 	for i := 0; i < b.N; i++ {
diff --git a/go/arrow/math/int64.go b/go/arrow/math/int64.go
index 4f70d2e..a618319 100644
--- a/go/arrow/math/int64.go
+++ b/go/arrow/math/int64.go
@@ -32,6 +32,9 @@ var (
 
 // Sum returns the summation of all elements in a.
 func (f Int64Funcs) Sum(a *array.Int64) int64 {
+	if a.Len() == 0 {
+		return int64(0)
+	}
 	return f.sum(a)
 }
 
diff --git a/go/arrow/math/int64_test.go b/go/arrow/math/int64_test.go
index 1972282..56d3e55 100644
--- a/go/arrow/math/int64_test.go
+++ b/go/arrow/math/int64_test.go
@@ -28,13 +28,28 @@ import (
 )
 
 func TestInt64Funcs_Sum(t *testing.T) {
-	vec := makeArrayInt64(10000)
+	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+	defer mem.AssertSize(t, 0)
+	vec := makeArrayInt64(10000, mem)
+	defer vec.Release()
 	res := math.Int64.Sum(vec)
 	assert.Equal(t, res, int64(49995000))
 }
 
-func makeArrayInt64(l int) *array.Int64 {
-	fb := array.NewInt64Builder(memory.NewGoAllocator())
+func TestInt64Funcs_SumEmpty(t *testing.T) {
+	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+	defer mem.AssertSize(t, 0)
+	b := array.NewInt64Builder(mem)
+	defer b.Release()
+	vec := b.NewInt64Array()
+	defer vec.Release()
+	res := math.Int64.Sum(vec)
+	assert.Equal(t, res, int64(0))
+}
+
+func makeArrayInt64(l int, mem memory.Allocator) *array.Int64 {
+	fb := array.NewInt64Builder(mem)
+	defer fb.Release()
 	fb.Reserve(l)
 	for i := 0; i < l; i++ {
 		fb.Append(int64(i))
@@ -43,7 +58,9 @@ func makeArrayInt64(l int) *array.Int64 {
 }
 
 func benchmarkInt64Funcs_Sum(b *testing.B, n int) {
-	vec := makeArrayInt64(n)
+	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+	defer mem.AssertSize(b, 0)
+	vec := makeArrayInt64(n, mem)
 	b.SetBytes(int64(vec.Len() * 8))
 	b.ResetTimer()
 	for i := 0; i < b.N; i++ {
diff --git a/go/arrow/math/type.go.tmpl b/go/arrow/math/type.go.tmpl
index 1b2dd69..71a11cb 100644
--- a/go/arrow/math/type.go.tmpl
+++ b/go/arrow/math/type.go.tmpl
@@ -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)
 }
 
diff --git a/go/arrow/math/type_test.go.tmpl b/go/arrow/math/type_test.go.tmpl
index f1b744c..4c6a49f 100644
--- a/go/arrow/math/type_test.go.tmpl
+++ b/go/arrow/math/type_test.go.tmpl
@@ -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))
@@ -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++ {
diff --git a/go/arrow/math/uint64.go b/go/arrow/math/uint64.go
index 875e821..6918707 100644
--- a/go/arrow/math/uint64.go
+++ b/go/arrow/math/uint64.go
@@ -32,6 +32,9 @@ var (
 
 // Sum returns the summation of all elements in a.
 func (f Uint64Funcs) Sum(a *array.Uint64) uint64 {
+	if a.Len() == 0 {
+		return uint64(0)
+	}
 	return f.sum(a)
 }
 
diff --git a/go/arrow/math/uint64_test.go b/go/arrow/math/uint64_test.go
index 7b8a165..fb242a0 100644
--- a/go/arrow/math/uint64_test.go
+++ b/go/arrow/math/uint64_test.go
@@ -28,13 +28,28 @@ import (
 )
 
 func TestUint64Funcs_Sum(t *testing.T) {
-	vec := makeArrayUint64(10000)
+	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+	defer mem.AssertSize(t, 0)
+	vec := makeArrayUint64(10000, mem)
+	defer vec.Release()
 	res := math.Uint64.Sum(vec)
 	assert.Equal(t, res, uint64(49995000))
 }
 
-func makeArrayUint64(l int) *array.Uint64 {
-	fb := array.NewUint64Builder(memory.NewGoAllocator())
+func TestUint64Funcs_SumEmpty(t *testing.T) {
+	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+	defer mem.AssertSize(t, 0)
+	b := array.NewUint64Builder(mem)
+	defer b.Release()
+	vec := b.NewUint64Array()
+	defer vec.Release()
+	res := math.Uint64.Sum(vec)
+	assert.Equal(t, res, uint64(0))
+}
+
+func makeArrayUint64(l int, mem memory.Allocator) *array.Uint64 {
+	fb := array.NewUint64Builder(mem)
+	defer fb.Release()
 	fb.Reserve(l)
 	for i := 0; i < l; i++ {
 		fb.Append(uint64(i))
@@ -43,7 +58,9 @@ func makeArrayUint64(l int) *array.Uint64 {
 }
 
 func benchmarkUint64Funcs_Sum(b *testing.B, n int) {
-	vec := makeArrayUint64(n)
+	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+	defer mem.AssertSize(b, 0)
+	vec := makeArrayUint64(n, mem)
 	b.SetBytes(int64(vec.Len() * 8))
 	b.ResetTimer()
 	for i := 0; i < b.N; i++ {
diff --git a/go/arrow/schema.go b/go/arrow/schema.go
index 8ae5fc1..a0cccc2 100644
--- a/go/arrow/schema.go
+++ b/go/arrow/schema.go
@@ -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
 }
@@ -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
 	}