Skip to content

Commit

Permalink
CSV Writer: Allow different types in output
Browse files Browse the repository at this point in the history
Adjust the csvio.Writer so that it can handle records with the same
field names but different types.

Closes #4781
  • Loading branch information
mattnibs committed Nov 17, 2023
1 parent bedcc95 commit 9269e8e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion zio/csvio/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"slices"
"strings"

"github.com/brimdata/zed"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (w *Writer) Write(rec *zed.Value) error {
if err := w.encoder.Write(hdr); err != nil {
return err
}
} else if rec.Type != w.first {
} else if rec.Type != w.first && !fieldsEqual(rec.Fields(), w.first.Fields) {
return ErrNotDataFrame
}
w.strings = w.strings[:0]
Expand Down Expand Up @@ -96,3 +97,9 @@ func formatValue(typ zed.Type, bytes zcode.Bytes) string {
}
return zson.FormatValue(zed.NewValue(typ, bytes))
}

func fieldsEqual(a, b []zed.Field) bool {
return slices.EqualFunc(a, b, func(a, b zed.Field) bool {
return a.Name == b.Name
})
}
14 changes: 14 additions & 0 deletions zio/csvio/ztests/different-types.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
zed: '*'

input: |
{a:0}
{a:"foo"}
{a:127.0.0.1}
output-flags: -f csv

output: |
a
0
foo
127.0.0.1

0 comments on commit 9269e8e

Please sign in to comment.