Skip to content

Commit

Permalink
ARROW-17804: [Go][CSV] Add Date32 and Time32 parsers (#14192)
Browse files Browse the repository at this point in the history
Given the recent addition of inferring schemas for CSVs, it should be able to parse all the types that can be inferred

Authored-by: Matt Topol <[email protected]>
Signed-off-by: Matt Topol <[email protected]>
  • Loading branch information
zeroshade authored Sep 21, 2022
1 parent 87d102e commit cf66aa0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go/arrow/csv/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func WithNullWriter(null string) Option {
}
}

// WithBoolWriter override the default bool formatter with a fucntion that returns
// WithBoolWriter override the default bool formatter with a function that returns
// a string representaton of bool states. i.e. True, False, 1, 0
func WithBoolWriter(fmtr func(bool) string) Option {
return func(cfg config) {
Expand Down
39 changes: 38 additions & 1 deletion go/arrow/csv/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,14 @@ func (r *Reader) initFieldConverter(bldr array.Builder) func(string) {
return func(str string) {
r.parseTimestamp(bldr, str, dt.Unit)
}

case *arrow.Date32Type:
return func(str string) {
r.parseDate32(bldr, str)
}
case *arrow.Time32Type:
return func(str string) {
r.parseTime32(bldr, str, dt.Unit)
}
default:
panic(fmt.Errorf("arrow/csv: unhandled field type %T", bldr.Type()))
}
Expand Down Expand Up @@ -644,6 +651,36 @@ func (r *Reader) parseTimestamp(field array.Builder, str string, unit arrow.Time
field.(*array.TimestampBuilder).Append(v)
}

func (r *Reader) parseDate32(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}

tm, err := time.Parse("2006-01-02", str)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Date32Builder).Append(arrow.Date32FromTime(tm))
}

func (r *Reader) parseTime32(field array.Builder, str string, unit arrow.TimeUnit) {
if r.isNull(str) {
field.AppendNull()
return
}

val, err := arrow.Time32FromString(str, unit)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Time32Builder).Append(val)
}

// Retain increases the reference count by 1.
// Retain may be called simultaneously from multiple goroutines.
func (r *Reader) Retain() {
Expand Down

0 comments on commit cf66aa0

Please sign in to comment.