2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-01-08 19:53:04 -05:00
|
|
|
|
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2019-03-07 03:20:10 -05:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-01-08 19:53:04 -05:00
|
|
|
"fmt"
|
2019-08-31 03:07:40 -04:00
|
|
|
"io"
|
2020-02-13 17:03:52 -05:00
|
|
|
"math"
|
|
|
|
"strconv"
|
2019-01-08 19:53:04 -05:00
|
|
|
"strings"
|
|
|
|
|
2019-03-07 03:20:10 -05:00
|
|
|
"github.com/bcicen/jstream"
|
2021-04-27 12:49:26 -04:00
|
|
|
csv "github.com/minio/csvparser"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/s3select/sql"
|
2019-01-08 19:53:04 -05:00
|
|
|
)
|
|
|
|
|
2019-03-07 03:20:10 -05:00
|
|
|
// RawJSON is a byte-slice that contains valid JSON
|
|
|
|
type RawJSON []byte
|
|
|
|
|
|
|
|
// MarshalJSON instance for []byte that assumes that byte-slice is
|
|
|
|
// already serialized JSON
|
|
|
|
func (b RawJSON) MarshalJSON() ([]byte, error) {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// Record - is JSON record.
|
|
|
|
type Record struct {
|
2019-03-07 03:20:10 -05:00
|
|
|
// Used in Set(), Marshal*()
|
2019-03-09 11:13:37 -05:00
|
|
|
KVS jstream.KVS
|
|
|
|
|
|
|
|
SelectFormat sql.SelectObjectFormat
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get - gets the value for a column name.
|
|
|
|
func (r *Record) Get(name string) (*sql.Value, error) {
|
2019-03-09 11:13:37 -05:00
|
|
|
// Get is implemented directly in the sql package.
|
|
|
|
return nil, errors.New("not implemented here")
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:27 -04:00
|
|
|
// Reset the record.
|
|
|
|
func (r *Record) Reset() {
|
|
|
|
if len(r.KVS) > 0 {
|
|
|
|
r.KVS = r.KVS[:0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:33:14 -04:00
|
|
|
// Clone the record and if possible use the destination provided.
|
|
|
|
func (r *Record) Clone(dst sql.Record) sql.Record {
|
|
|
|
other, ok := dst.(*Record)
|
2019-09-16 20:26:27 -04:00
|
|
|
if !ok {
|
2019-09-27 15:33:14 -04:00
|
|
|
other = &Record{}
|
2019-09-16 20:26:27 -04:00
|
|
|
}
|
2019-09-27 15:33:14 -04:00
|
|
|
if len(other.KVS) > 0 {
|
|
|
|
other.KVS = other.KVS[:0]
|
|
|
|
}
|
|
|
|
other.KVS = append(other.KVS, r.KVS...)
|
|
|
|
return other
|
2019-09-16 20:26:27 -04:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// Set - sets the value for a column name.
|
2020-02-13 17:03:52 -05:00
|
|
|
func (r *Record) Set(name string, value *sql.Value) (sql.Record, error) {
|
2019-01-08 19:53:04 -05:00
|
|
|
var v interface{}
|
2019-01-28 20:59:48 -05:00
|
|
|
if b, ok := value.ToBool(); ok {
|
|
|
|
v = b
|
|
|
|
} else if f, ok := value.ToFloat(); ok {
|
|
|
|
v = f
|
|
|
|
} else if i, ok := value.ToInt(); ok {
|
|
|
|
v = i
|
2019-02-04 23:54:45 -05:00
|
|
|
} else if t, ok := value.ToTimestamp(); ok {
|
|
|
|
v = sql.FormatSQLTimestamp(t)
|
2019-01-28 20:59:48 -05:00
|
|
|
} else if s, ok := value.ToString(); ok {
|
|
|
|
v = s
|
|
|
|
} else if value.IsNull() {
|
|
|
|
v = nil
|
|
|
|
} else if b, ok := value.ToBytes(); ok {
|
2019-11-09 12:10:35 -05:00
|
|
|
// This can either be raw json or a CSV value.
|
|
|
|
// Only treat objects and arrays as JSON.
|
|
|
|
if len(b) > 0 && (b[0] == '{' || b[0] == '[') {
|
|
|
|
v = RawJSON(b)
|
|
|
|
} else {
|
|
|
|
v = string(b)
|
|
|
|
}
|
2019-10-06 10:52:45 -04:00
|
|
|
} else if arr, ok := value.ToArray(); ok {
|
|
|
|
v = arr
|
2019-01-28 20:59:48 -05:00
|
|
|
} else {
|
2020-02-13 17:03:52 -05:00
|
|
|
return nil, fmt.Errorf("unsupported sql value %v and type %v", value, value.GetTypeString())
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
name = strings.Replace(name, "*", "__ALL__", -1)
|
2019-03-09 11:13:37 -05:00
|
|
|
r.KVS = append(r.KVS, jstream.KV{Key: name, Value: v})
|
2020-02-13 17:03:52 -05:00
|
|
|
return r, nil
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2019-08-31 03:07:40 -04:00
|
|
|
// WriteCSV - encodes to CSV data.
|
2020-04-01 18:39:34 -04:00
|
|
|
func (r *Record) WriteCSV(writer io.Writer, opts sql.WriteCSVOpts) error {
|
2019-01-08 19:53:04 -05:00
|
|
|
var csvRecord []string
|
2019-03-09 11:13:37 -05:00
|
|
|
for _, kv := range r.KVS {
|
2019-03-07 03:20:10 -05:00
|
|
|
var columnValue string
|
|
|
|
switch val := kv.Value.(type) {
|
2020-02-13 17:03:52 -05:00
|
|
|
case float64:
|
|
|
|
columnValue = jsonFloat(val)
|
|
|
|
case string:
|
|
|
|
columnValue = val
|
|
|
|
case bool, int64:
|
2019-03-07 03:20:10 -05:00
|
|
|
columnValue = fmt.Sprintf("%v", val)
|
|
|
|
case nil:
|
|
|
|
columnValue = ""
|
|
|
|
case RawJSON:
|
|
|
|
columnValue = string([]byte(val))
|
2019-10-06 10:52:45 -04:00
|
|
|
case []interface{}:
|
|
|
|
b, err := json.Marshal(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
columnValue = string(b)
|
2019-03-07 03:20:10 -05:00
|
|
|
default:
|
2019-10-06 10:52:45 -04:00
|
|
|
return fmt.Errorf("Cannot marshal unhandled type: %T", kv.Value)
|
2019-03-07 03:20:10 -05:00
|
|
|
}
|
|
|
|
csvRecord = append(csvRecord, columnValue)
|
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
|
2019-08-31 03:07:40 -04:00
|
|
|
w := csv.NewWriter(writer)
|
2020-04-01 18:39:34 -04:00
|
|
|
w.Comma = opts.FieldDelimiter
|
|
|
|
w.Quote = opts.Quote
|
|
|
|
w.AlwaysQuote = opts.AlwaysQuote
|
|
|
|
w.QuoteEscape = opts.QuoteEscape
|
2019-01-08 19:53:04 -05:00
|
|
|
if err := w.Write(csvRecord); err != nil {
|
2019-08-31 03:07:40 -04:00
|
|
|
return err
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
w.Flush()
|
2021-05-25 17:17:33 -04:00
|
|
|
return w.Error()
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2019-03-09 11:13:37 -05:00
|
|
|
// Raw - returns the underlying representation.
|
|
|
|
func (r *Record) Raw() (sql.SelectObjectFormat, interface{}) {
|
|
|
|
return r.SelectFormat, r.KVS
|
|
|
|
}
|
|
|
|
|
2019-08-31 03:07:40 -04:00
|
|
|
// WriteJSON - encodes to JSON data.
|
|
|
|
func (r *Record) WriteJSON(writer io.Writer) error {
|
|
|
|
return json.NewEncoder(writer).Encode(r.KVS)
|
2019-03-09 11:13:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the underlying buffer of json data.
|
2020-02-13 17:03:52 -05:00
|
|
|
func (r *Record) Replace(k interface{}) error {
|
|
|
|
v, ok := k.(jstream.KVS)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("cannot replace internal data in json record with type %T", k)
|
|
|
|
}
|
|
|
|
r.KVS = v
|
2019-03-09 11:13:37 -05:00
|
|
|
return nil
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRecord - creates new empty JSON record.
|
2019-03-09 11:13:37 -05:00
|
|
|
func NewRecord(f sql.SelectObjectFormat) *Record {
|
2019-01-08 19:53:04 -05:00
|
|
|
return &Record{
|
2019-03-09 11:13:37 -05:00
|
|
|
KVS: jstream.KVS{},
|
|
|
|
SelectFormat: f,
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 17:03:52 -05:00
|
|
|
|
|
|
|
// jsonFloat converts a float to string similar to Go stdlib formats json floats.
|
|
|
|
func jsonFloat(f float64) string {
|
|
|
|
var tmp [32]byte
|
|
|
|
dst := tmp[:0]
|
|
|
|
|
|
|
|
// Convert as if by ES6 number to string conversion.
|
|
|
|
// This matches most other JSON generators.
|
|
|
|
// See golang.org/issue/6384 and golang.org/issue/14135.
|
|
|
|
// Like fmt %g, but the exponent cutoffs are different
|
|
|
|
// and exponents themselves are not padded to two digits.
|
|
|
|
abs := math.Abs(f)
|
|
|
|
fmt := byte('f')
|
|
|
|
if abs != 0 {
|
|
|
|
if abs < 1e-6 || abs >= 1e21 {
|
|
|
|
fmt = 'e'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst = strconv.AppendFloat(dst, f, fmt, -1, 64)
|
|
|
|
if fmt == 'e' {
|
|
|
|
// clean up e-09 to e-9
|
|
|
|
n := len(dst)
|
|
|
|
if n >= 4 && dst[n-4] == 'e' && dst[n-3] == '-' && dst[n-2] == '0' {
|
|
|
|
dst[n-2] = dst[n-1]
|
|
|
|
dst = dst[:n-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(dst)
|
|
|
|
}
|