From 7051f59204b8b77c61e60398dd785879cc28075b Mon Sep 17 00:00:00 2001 From: Matthew Nibecker Date: Fri, 17 Nov 2023 15:13:32 -0700 Subject: [PATCH] CSV Writer: Allow different types in output Adjust the csvio.Writer so that it can handle records with the same field names but different types. Closes #4781 --- zio/csvio/writer.go | 14 +++++++++++++- zio/csvio/ztests/different-types.yaml | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 zio/csvio/ztests/different-types.yaml diff --git a/zio/csvio/writer.go b/zio/csvio/writer.go index 56a9a50c19..161295e075 100644 --- a/zio/csvio/writer.go +++ b/zio/csvio/writer.go @@ -62,7 +62,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] @@ -96,3 +96,15 @@ func formatValue(typ zed.Type, bytes zcode.Bytes) string { } return zson.FormatValue(zed.NewValue(typ, bytes)) } + +func fieldsEqual(a, b []zed.Field) bool { + if len(a) != len(b) { + return false + } + for i, f := range a { + if f.Name != b[i].Name { + return false + } + } + return true +} diff --git a/zio/csvio/ztests/different-types.yaml b/zio/csvio/ztests/different-types.yaml new file mode 100644 index 0000000000..da59907576 --- /dev/null +++ b/zio/csvio/ztests/different-types.yaml @@ -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