Skip to content

Commit

Permalink
ARROW-5552: [Go] make Schema, Field and simpleRecord implement Stringer
Browse files Browse the repository at this point in the history
Author: Sebastien Binet <[email protected]>

Closes apache#4516 from sbinet/issue-5552 and squashes the following commits:

dfbb611 <Sebastien Binet> go/arrow/array: make simpleRecord implement Stringer
702b3a3 <Sebastien Binet> ARROW-5552:  make Schema and Field implement Stringer
  • Loading branch information
sbinet committed Jun 11, 2019
1 parent f838c99 commit 084549a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 30 deletions.
12 changes: 12 additions & 0 deletions go/arrow/array/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package array

import (
"fmt"
"strings"
"sync/atomic"

"github.com/apache/arrow/go/arrow"
Expand Down Expand Up @@ -242,6 +243,17 @@ func (rec *simpleRecord) NewSlice(i, j int64) Record {
return NewRecord(rec.schema, arrs, j-i)
}

func (rec *simpleRecord) String() string {
o := new(strings.Builder)
fmt.Fprintf(o, "record:\n %v\n", rec.schema)
fmt.Fprintf(o, " rows: %d\n", rec.rows)
for i, col := range rec.arrs {
fmt.Fprintf(o, " col[%d][%s]: %v\n", i, rec.schema.Field(i).Name, col)
}

return o.String()
}

// RecordBuilder eases the process of building a Record, iteratively, from
// a known Schema.
type RecordBuilder struct {
Expand Down
13 changes: 13 additions & 0 deletions go/arrow/datatype_nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ func (f Field) Equal(o Field) bool {
return reflect.DeepEqual(f, o)
}

func (f Field) String() string {
o := new(strings.Builder)
nullable := ""
if f.Nullable {
nullable = ", nullable"
}
fmt.Fprintf(o, "%s: type=%v%v", f.Name, f.Type, nullable)
if f.HasMetadata() {
fmt.Fprintf(o, "\n%*.smetadata: %v", len(f.Name)+2, "", f.Metadata)
}
return o.String()
}

var (
_ DataType = (*ListType)(nil)
_ DataType = (*StructType)(nil)
Expand Down
29 changes: 2 additions & 27 deletions go/arrow/ipc/cmd/arrow-ls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ import (
"io"
"log"
"os"
"strings"

"github.com/apache/arrow/go/arrow"
"github.com/apache/arrow/go/arrow/ipc"
"github.com/apache/arrow/go/arrow/memory"
"github.com/pkg/errors"
Expand Down Expand Up @@ -99,7 +97,7 @@ func processStream(w io.Writer, rin io.Reader) error {
}
defer r.Release()

fmt.Fprintf(w, "schema:\n%v", displaySchema(r.Schema()))
fmt.Fprintf(w, "%v\n", r.Schema())

nrecs := 0
for r.Next() {
Expand Down Expand Up @@ -153,35 +151,12 @@ func processFile(w io.Writer, fname string) error {
defer r.Close()

fmt.Fprintf(w, "version: %v\n", r.Version())
fmt.Fprintf(w, "schema:\n%v", displaySchema(r.Schema()))
fmt.Fprintf(w, "%v\n", r.Schema())
fmt.Fprintf(w, "records: %d\n", r.NumRecords())

return nil
}

func displaySchema(s *arrow.Schema) string {
o := new(strings.Builder)
fmt.Fprintf(o, "%*.sfields: %d\n", 2, "", len(s.Fields()))
for _, f := range s.Fields() {
displayField(o, f, 4)
}
if meta := s.Metadata(); meta.Len() > 0 {
fmt.Fprintf(o, "metadata: %v\n", meta)
}
return o.String()
}

func displayField(o io.Writer, field arrow.Field, inc int) {
nullable := ""
if field.Nullable {
nullable = ", nullable"
}
fmt.Fprintf(o, "%*.s- %s: type=%v%v\n", inc, "", field.Name, field.Type, nullable)
if field.HasMetadata() {
fmt.Fprintf(o, "%*.smetadata: %v\n", inc, "", field.Metadata)
}
}

func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `Command arrow-ls displays the listing of an Arrow file.
Expand Down
6 changes: 3 additions & 3 deletions go/arrow/ipc/cmd/arrow-ls/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestLsStream(t *testing.T) {
- uint64s: type=uint64, nullable
- float32s: type=float32, nullable
- float64s: type=float64, nullable
metadata: ["k1": "v1", "k2": "v2", "k3": "v3"]
metadata: ["k1": "v1", "k2": "v2", "k3": "v3"]
records: 3
`,
},
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestLsFile(t *testing.T) {
- uint64s: type=uint64, nullable
- float32s: type=float32, nullable
- float64s: type=float64, nullable
metadata: ["k1": "v1", "k2": "v2", "k3": "v3"]
metadata: ["k1": "v1", "k2": "v2", "k3": "v3"]
records: 3
`,
},
Expand All @@ -199,7 +199,7 @@ schema:
- uint64s: type=uint64, nullable
- float32s: type=float32, nullable
- float64s: type=float64, nullable
metadata: ["k1": "v1", "k2": "v2", "k3": "v3"]
metadata: ["k1": "v1", "k2": "v2", "k3": "v3"]
records: 3
`,
},
Expand Down
15 changes: 15 additions & 0 deletions go/arrow/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,18 @@ func (sc *Schema) Equal(o *Schema) bool {
}
return true
}

func (s *Schema) String() string {
o := new(strings.Builder)
fmt.Fprintf(o, "schema:\n fields: %d\n", len(s.Fields()))
for i, f := range s.Fields() {
if i > 0 {
o.WriteString("\n")
}
fmt.Fprintf(o, " - %v", f)
}
if meta := s.Metadata(); meta.Len() > 0 {
fmt.Fprintf(o, "\n metadata: %v", meta)
}
return o.String()
}

0 comments on commit 084549a

Please sign in to comment.