Skip to content

Commit

Permalink
apacheGH-35296: [Go] Add arrow.Table.String() (apache#35580)
Browse files Browse the repository at this point in the history
This PR fixes apache#35296
* Closes: apache#35296

Authored-by: vibhatha <vibhatha@gmail.com>
Signed-off-by: Matt Topol <zotthewizard@gmail.com>
  • Loading branch information
vibhatha authored Aug 29, 2023
1 parent 439d066 commit f36855a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
20 changes: 20 additions & 0 deletions go/arrow/array/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"math"
"strings"
"sync/atomic"

"github.com/apache/arrow/go/v14/arrow"
Expand Down Expand Up @@ -257,6 +258,25 @@ func (tbl *simpleTable) Release() {
}
}

func (tbl *simpleTable) String() string {
o := new(strings.Builder)
o.WriteString(tbl.Schema().String())
o.WriteString("\n")

for i := 0; i < int(tbl.NumCols()); i++ {
col := tbl.Column(i)
o.WriteString(col.Field().Name + ": [")
for j, chunk := range col.Data().Chunks() {
if j != 0 {
o.WriteString(", ")
}
o.WriteString(chunk.String())
}
o.WriteString("]\n")
}
return o.String()
}

// TableReader is a Record iterator over a (possibly chunked) Table
type TableReader struct {
refCount int64
Expand Down
45 changes: 45 additions & 0 deletions go/arrow/array/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,48 @@ func TestTableReader(t *testing.T) {
})
}
}

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

schema := arrow.NewSchema(
[]arrow.Field{
{Name: "f1-i32", Type: arrow.PrimitiveTypes.Int32},
{Name: "f2-f64", Type: arrow.PrimitiveTypes.Float64},
},
nil,
)

b := array.NewRecordBuilder(mem, schema)
defer b.Release()

b.Field(0).(*array.Int32Builder).AppendValues([]int32{1, 2, 3, 4, 5, 6}, nil)
b.Field(0).(*array.Int32Builder).AppendValues([]int32{7, 8, 9, 10}, []bool{true, true, false, true})
b.Field(1).(*array.Float64Builder).AppendValues([]float64{11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, nil)

rec1 := b.NewRecord()
defer rec1.Release()

b.Field(0).(*array.Int32Builder).AppendValues([]int32{111, 112, 113, 114, 115, 116, 117, 118, 119, 120}, nil)
b.Field(1).(*array.Float64Builder).AppendValues([]float64{211, 212, 213, 214, 215, 216, 217, 218, 219, 220}, nil)

rec2 := b.NewRecord()
defer rec2.Release()

tbl := array.NewTableFromRecords(schema, []arrow.Record{rec1, rec2})
defer tbl.Release()

table_str := tbl.String()
expected_str :=
`schema:
fields: 2
- f1-i32: type=int32
- f2-f64: type=float64
f1-i32: [[1 2 3 4 5 6 7 8 (null) 10], [111 112 113 114 115 116 117 118 119 120]]
f2-f64: [[11 12 13 14 15 16 17 18 19 20], [211 212 213 214 215 216 217 218 219 220]]
`
if got, want := table_str, expected_str; table_str != expected_str {
t.Fatalf("invalid String: got=%#v, want=%#v", got, want)
}
}

0 comments on commit f36855a

Please sign in to comment.