Use jstream to serialize records to JSON format in S3Select (#7318)

- Also, switch to jstream to generate internal record representation
  from CSV/JSON readers

- This fixes a bug in which JSON output objects have their keys
  reversed from the order they are specified in the Select columns.

- Also includes a fix for tests.
This commit is contained in:
Aditya Manthramurthy
2019-03-07 00:20:10 -08:00
committed by Harshavardhana
parent f97a33a63f
commit f4879ed96d
4 changed files with 68 additions and 50 deletions

View File

@@ -19,10 +19,11 @@ package csv
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"github.com/bcicen/jstream"
"github.com/minio/minio/pkg/s3select/sql"
"github.com/tidwall/sjson"
)
// Record - is CSV record.
@@ -78,20 +79,11 @@ func (r *Record) MarshalCSV(fieldDelimiter rune) ([]byte, error) {
// MarshalJSON - encodes to JSON data.
func (r *Record) MarshalJSON() ([]byte, error) {
data := "{}"
var err error
for i := len(r.columnNames) - 1; i >= 0; i-- {
if i >= len(r.csvRecord) {
continue
}
if data, err = sjson.Set(data, r.columnNames[i], r.csvRecord[i]); err != nil {
return nil, err
}
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]}
}
return []byte(data), nil
return json.Marshal(kvs)
}
// NewRecord - creates new CSV record.