forked from FeatureBaseDB/featurebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwire_response.go
295 lines (263 loc) · 7.93 KB
/
wire_response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package pilosa
import (
"bytes"
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/featurebasedb/featurebase/v3/dax"
"github.com/featurebasedb/featurebase/v3/pql"
"github.com/pkg/errors"
)
// WireQueryResponse is the standard featurebase response type which can be
// serialized and sent over the wire.
type WireQueryResponse struct {
Schema WireQuerySchema `json:"schema"`
Data [][]interface{} `json:"data"`
Error string `json:"error"`
Warnings []string `json:"warnings"`
QueryPlan map[string]interface{} `json:"query-plan"`
ExecutionTime int64 `json:"execution-time"`
}
// WireQuerySchema is a list of Fields which map to the data columns in the
// Response.
type WireQuerySchema struct {
Fields []*WireQueryField `json:"fields"`
}
// WireQueryField is a field name along with a supported BaseType and type
// information.
type WireQueryField struct {
Name dax.FieldName `json:"name"`
Type string `json:"type"` // human readable display (e.g. "decimal(2)")
BaseType dax.BaseType `json:"base-type"` // for programmatic switching on type (e.g. "decimal")
TypeInfo map[string]interface{} `json:"type-info"` // type modifiers (like scale), but not constraints (like min/max)
}
// UnmarshalJSON is a custom unmarshaller for the SQLResponse that converts the
// value types in `Data` based on the types in `Schema`.
func (s *WireQueryResponse) UnmarshalJSON(in []byte) error {
return s.UnmarshalJSONTyped(in, false)
}
// UnmarshalJSONTyped is a temporary until we send typed values back in sql
// responses. At that point, we can get rid of the typed=false path. In order to
// do that, we need sql3 to return typed values, and we need the sql3/test/defs
// to define results as typed values (like `IDSet`) instead of (for example)
// `[]int64`.
func (s *WireQueryResponse) UnmarshalJSONTyped(in []byte, typed bool) error {
type Alias WireQueryResponse
var aux Alias
dec := json.NewDecoder(bytes.NewReader(in))
dec.UseNumber()
err := dec.Decode(&aux)
if err != nil {
return err
}
*s = WireQueryResponse(aux)
// If the SQLResponse contains an error, don't bother doing any conversions
// on the data.
if s.Error != "" {
return nil
}
// Try to convert the types in the TypeInfo map for each field in the
// schema.
for _, fld := range s.Schema.Fields {
// TODO(tlt): we can remove these two "ToLower" calls once sql3 is
// returning dax.FieldType (i.e. lowercase).
fld.Type = strings.ToLower(fld.Type)
fld.BaseType = dax.BaseType(strings.ToLower(string(fld.BaseType)))
for k, v := range fld.TypeInfo {
switch k {
case "scale":
switch n := v.(type) {
case float64:
fld.TypeInfo[k] = int64(n)
case json.Number:
fld.TypeInfo[k], _ = n.Int64()
}
}
}
}
// Try to convert the data types based on the headers.
for i := range s.Data {
for j, hdr := range s.Schema.Fields {
switch hdr.BaseType {
case dax.BaseTypeID, dax.BaseTypeInt:
jn := s.Data[i][j]
if v, ok := jn.(json.Number); ok {
if x, err := v.Int64(); err == nil {
s.Data[i][j] = x
} else {
return errors.Wrap(err, "can't be decoded as int64")
}
}
case dax.BaseTypeIDSet:
if src, ok := s.Data[i][j].([]interface{}); ok {
if typed {
val := make(IDSet, len(src))
for k := range src {
v := src[k].(json.Number)
if x, err := v.Int64(); err == nil {
val[k] = x
} else {
return errors.Wrap(err, "can't be decoded as int64")
}
}
s.Data[i][j] = val
} else {
val := make([]int64, len(src))
for k := range src {
v := src[k].(json.Number)
if x, err := v.Int64(); err == nil {
val[k] = x
} else {
return errors.Wrap(err, "can't be decoded as int64")
}
}
s.Data[i][j] = val
}
}
case dax.BaseTypeDecimal:
if jn, ok := s.Data[i][j].(json.Number); ok {
var scale int64
if scaleVal, ok := hdr.TypeInfo["scale"]; !ok {
return errors.New("decimal does not have a scale")
} else if scaleInt64, ok := scaleVal.(int64); !ok {
return errors.New("scale can't be cast to int64")
} else {
scale = scaleInt64
}
format := fmt.Sprintf("%%.%df", scale)
f, err := jn.Float64()
if err != nil {
return errors.Wrap(err, "parsing decimal")
}
dec, err := pql.ParseDecimal(fmt.Sprintf(format, f))
if err != nil {
return errors.Wrap(err, "parsing decimal")
}
if dec.Scale != scale {
dec = pql.NewDecimal(dec.ToInt64(scale), scale)
}
s.Data[i][j] = dec
}
case dax.BaseTypeStringSet:
if src, ok := s.Data[i][j].([]interface{}); ok {
if typed {
val := make(StringSet, len(src))
for k := range src {
val[k] = src[k].(string)
}
s.Data[i][j] = val
} else {
val := make([]string, len(src))
for k := range src {
val[k] = src[k].(string)
}
s.Data[i][j] = val
}
}
case dax.BaseTypeTimestamp:
if src, ok := s.Data[i][j].(string); ok && src != "" {
val, err := time.ParseInLocation(time.RFC3339Nano, src, time.UTC)
if err != nil {
return errors.Wrap(err, "parsing timestamp")
}
s.Data[i][j] = val
}
case dax.BaseTypeBool, dax.BaseTypeString:
// no need to convert
default:
log.Printf("WARNING: unimplemented: %s", hdr.BaseType)
}
}
}
return nil
}
// IDSet is a return type specific to SQLResponse types.
type IDSet []int64
func (ii IDSet) String() string {
var sb strings.Builder
sb.WriteString("[")
for i := range ii {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(fmt.Sprintf("%d", ii[i]))
}
sb.WriteString("]")
return sb.String()
}
// StringSet is a return type specific to SQLResponse types.
type StringSet []string
func (ss StringSet) String() string {
var sb strings.Builder
sb.WriteString("[")
for i := range ss {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString("'" + ss[i] + "'")
}
sb.WriteString("]")
return sb.String()
}
// ShowColumnsResponse returns a structure which is specific to a `SHOW COLUMNS`
// statement, derived from the results in the WireQueryResponse. This is kind of
// a crude way to unmarshal a WireQueryResponse into a type which is specific to
// the sql operation.
// TODO(tlt): see if we can standardize on this logic, because it would be useful to
// have the same thing for SHOW DATABASES and SHOW TABLES.
// TODO(tlt): the fields unmarshalled in this method are a subset of the actual
// columns available; at the moment, we only handled the ones we need in the
// CLI.
func (s *WireQueryResponse) ShowColumnsResponse() (*ShowColumnsResponse, error) {
// Make a map of header names to index position. We do this to avoid
// breaking things if for some reason the format of the SHOW COLUMNS
// response defined in the sql3 package changes.
m := make(map[string]int)
for i, fld := range s.Schema.Fields {
m[string(fld.Name)] = i
}
flds := make([]*dax.Field, 0, len(s.Data))
for _, row := range s.Data {
// name
var name dax.FieldName
if val, ok := row[m["name"]].(string); ok {
name = dax.FieldName(val)
}
// type
var typ dax.BaseType
if val, ok := row[m["type"]].(string); ok {
typ = dax.BaseType(val)
}
// scale
var scale int64
if val, ok := row[m["scale"]].(int64); ok {
scale = val
}
flds = append(flds, &dax.Field{
Name: name,
Type: typ,
Options: dax.FieldOptions{
Scale: scale,
},
})
}
return &ShowColumnsResponse{
Fields: flds,
}, nil
}
// ShowColumnsResponse is a type used to marshal the results of a `SHOW COLUMNS`
// statement.
type ShowColumnsResponse struct {
Fields []*dax.Field
}
// Field returns the field by name. If the field is not found, nil is returned.
func (s *ShowColumnsResponse) Field(name dax.FieldName) *dax.Field {
for i := range s.Fields {
if s.Fields[i].Name == name {
return s.Fields[i]
}
}
return nil
}