diff --git a/internal/s3select/csv/record.go b/internal/s3select/csv/record.go
index c0dacc6c2..18e467834 100644
--- a/internal/s3select/csv/record.go
+++ b/internal/s3select/csv/record.go
@@ -125,9 +125,11 @@ func (r *Record) WriteCSV(writer io.Writer, opts sql.WriteCSVOpts) error {
// WriteJSON - encodes to JSON data.
func (r *Record) WriteJSON(writer io.Writer) error {
- var kvs jstream.KVS = make([]jstream.KV, len(r.columnNames))
- for i := 0; i < len(r.columnNames); i++ {
- kvs[i] = jstream.KV{Key: r.columnNames[i], Value: r.csvRecord[i]}
+ var kvs jstream.KVS = make([]jstream.KV, 0, len(r.columnNames))
+ for i, cn := range r.columnNames {
+ if i < len(r.csvRecord) {
+ kvs = append(kvs, jstream.KV{Key: cn, Value: r.csvRecord[i]})
+ }
}
return json.NewEncoder(writer).Encode(kvs)
}
diff --git a/internal/s3select/select_test.go b/internal/s3select/select_test.go
index d8cd85e32..3f2df9ce9 100644
--- a/internal/s3select/select_test.go
+++ b/internal/s3select/select_test.go
@@ -1547,6 +1547,36 @@ func TestCSVRanges(t *testing.T) {
FALSE
+`),
+ },
+ {
+ name: "var-field-count",
+ input: []byte(`id,time,num,num2,text
+1,2010-01-01T,7867786,4565.908123
+2,2017-01-02T03:04Z,-5, 0.765111,Some some
+`),
+ // Since we are doing offset, no headers are used.
+ wantResult: `{"id":"1","time":"2010-01-01T","num":"7867786","num2":"4565.908123"}
+{"id":"2","time":"2017-01-02T03:04Z","num":"-5","num2":" 0.765111","text":"Some some"}`,
+ wantErr: false,
+ requestXML: []byte(`
+
+ SELECT * from s3object
+ SQL
+
+ NONE
+
+ USE
+ "
+
+
+
+
+
+
+
+ FALSE
+
`),
},
{