forked from mdoan-stripe/prestgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
495 lines (422 loc) · 11.9 KB
/
conn.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package prestgo
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net/http"
"net/url"
"strings"
"time"
)
// Name of the driver to use when calling `sql.Open`
const DriverName = "prestgo"
// Default data source parameters
const (
DefaultPort = "8080"
DefaultCatalog = "hive"
DefaultSchema = "default"
DefaultUsername = "prestgo"
TimestampFormat = "2006-01-02 15:04:05.000"
DateFormat = "2006-01-02"
)
var (
// ErrNotSupported is returned when an unsupported feature is requested.
ErrNotSupported = errors.New(DriverName + ": not supported")
// ErrQueryFailed indicates that a network or server failure prevented the driver obtaining a query result.
ErrQueryFailed = errors.New(DriverName + ": query failed")
// ErrQueryCanceled indicates that a query was canceled before results could be retrieved.
ErrQueryCanceled = errors.New(DriverName + ": query canceled")
)
func init() {
sql.Register(DriverName, &drv{})
}
type drv struct{}
func (*drv) Open(name string) (driver.Conn, error) {
return Open(name)
}
// Open creates a connection to the specified data source name which should be
// of the form "presto://hostname:port/catalog/schema?source=x&session=y". http.DefaultClient will
// be used for communicating with the Presto server.
func Open(name string) (driver.Conn, error) {
return ClientOpen(http.DefaultClient, name)
}
// ClientOpen creates a connection to the specified data source name using the supplied
// HTTP client. The data source name should be of the form
// "presto://hostname:port/catalog/schema?source=x&session=y".
func ClientOpen(client *http.Client, name string) (driver.Conn, error) {
conf := make(config)
conf.parseDataSource(name)
cn := &conn{
client: client,
addr: conf["addr"],
catalog: conf["catalog"],
schema: conf["schema"],
user: conf["user"],
source: conf["source"],
session: conf["session"],
}
return cn, nil
}
type conn struct {
client *http.Client
addr string
catalog string
schema string
user string
source string
session string
}
var _ driver.Conn = &conn{}
func (c *conn) Prepare(query string) (driver.Stmt, error) {
st := &stmt{
conn: c,
query: query,
}
return st, nil
}
func (c *conn) Close() error {
return nil
}
func (c *conn) Begin() (driver.Tx, error) {
return nil, ErrNotSupported
}
type stmt struct {
conn *conn
query string
}
var _ driver.Stmt = &stmt{}
func (s *stmt) Close() error {
return nil
}
func (s *stmt) NumInput() int {
return -1 // TODO: parse query for parameters
}
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
return nil, ErrNotSupported
}
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
// TODO: support query argument substitution
if len(args) > 0 {
return nil, ErrNotSupported
}
queryURL := fmt.Sprintf("http://%s/v1/statement", s.conn.addr)
req, err := http.NewRequest("POST", queryURL, strings.NewReader(s.query))
if err != nil {
return nil, err
}
req.Header.Add("X-Presto-User", s.conn.user)
req.Header.Add("X-Presto-Catalog", s.conn.catalog)
req.Header.Add("X-Presto-Schema", s.conn.schema)
if s.conn.source != "" {
req.Header.Add("X-Presto-Source", s.conn.source)
}
if s.conn.session != "" {
req.Header.Add("X-Presto-Session", s.conn.session)
}
resp, err := s.conn.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Presto doesn't use the http response code, parse errors come back as 200
if resp.StatusCode != 200 {
return nil, ErrQueryFailed
}
var sresp stmtResponse
err = json.NewDecoder(resp.Body).Decode(&sresp)
if err != nil {
return nil, err
}
if sresp.Stats.State == "FAILED" {
return nil, sresp.Error
}
time.Sleep(500 * time.Millisecond)
r := &rows{
conn: s.conn,
nextURI: sresp.NextURI,
}
return r, nil
}
type rows struct {
conn *conn
nextURI string
fetched bool
rowindex int
columns []string
types []driver.ValueConverter
data []queryData
}
var _ driver.Rows = &rows{}
func (r *rows) fetch() error {
// TODO: timeout
for {
qresp, gotData, err := r.waitForData()
if err != nil {
return err
}
if !gotData {
time.Sleep(800 * time.Millisecond) // TODO: make this interval configurable
continue
}
r.rowindex = 0
r.data = qresp.Data
// Note: qresp.Stats.State will be FINISHED when last page is retrieved
r.nextURI = qresp.NextURI
if !r.fetched {
r.columns = make([]string, len(qresp.Columns))
r.types = make([]driver.ValueConverter, len(qresp.Columns))
for i, col := range qresp.Columns {
r.columns[i] = col.Name
switch {
case strings.HasPrefix(col.Type, Row):
// If the column is an unflattened struct, interpret as a string.
r.types[i] = rowConverter{Type: col.Type}
case strings.HasPrefix(col.Type, VarChar), strings.HasPrefix(col.Type, Char):
r.types[i] = stringConverter
case col.Type == JSON:
// use string for json
r.types[i] = stringConverter
case col.Type == BigInt, col.Type == Integer, col.Type == Smallint, col.Type == Tinyint:
r.types[i] = bigIntConverter
case col.Type == Boolean:
r.types[i] = boolConverter
case col.Type == Double, col.Type == Real:
r.types[i] = doubleConverter
case strings.HasPrefix(col.Type, Decimal):
// use string converter for this so that we keep our preciseness
r.types[i] = stringConverter
case col.Type == Date:
r.types[i] = dateConverter
case col.Type == Time:
// use string here, having no date makes timestamps weird
r.types[i] = stringConverter
case col.Type == TimeWithTimezone:
// use string here, having no date makes timestamps weird
r.types[i] = stringConverter
case col.Type == Timestamp:
r.types[i] = timestampConverter
case col.Type == TimestampWithTimezone:
r.types[i] = timestampWithTimezoneConverter
default:
r.types[i] = stringConverter
fmt.Println(fmt.Sprintf("unsupported column type: %s", col.Type))
}
}
r.fetched = true
}
if len(qresp.Data) == 0 {
return io.EOF
}
return nil
}
}
func (r *rows) waitForData() (*queryResponse, bool, error) {
nextReq, err := http.NewRequest("GET", r.nextURI, nil)
if err != nil {
return nil, false, err
}
nextResp, err := r.conn.client.Do(nextReq)
if err != nil {
return nil, false, err
}
if nextResp.StatusCode != 200 {
nextResp.Body.Close()
return nil, false, ErrQueryFailed
}
var qresp queryResponse
err = json.NewDecoder(nextResp.Body).Decode(&qresp)
nextResp.Body.Close()
if err != nil {
return nil, false, err
}
switch qresp.Stats.State {
case QueryStateFailed:
return nil, false, qresp.Error
case QueryStateCanceled:
return nil, false, ErrQueryCanceled
case QueryStatePlanning, QueryStateQueued, QueryStateRunning, QueryStateStarting:
if len(qresp.Data) == 0 {
r.nextURI = qresp.NextURI
return nil, false, nil
}
}
return &qresp, true, nil
}
func (r *rows) Columns() []string {
if !r.fetched {
if err := r.fetch(); err != nil {
return []string{}
}
}
return r.columns
}
func (r *rows) Close() error {
return nil
}
func (r *rows) Next(dest []driver.Value) error {
if !r.fetched || r.rowindex >= len(r.data) {
if r.nextURI == "" {
return io.EOF
}
if err := r.fetch(); err != nil {
return err
}
}
for i, v := range r.types {
val, err := v.ConvertValue(r.data[r.rowindex][i])
if err != nil {
return err // TODO: more context in error
}
dest[i] = val
}
r.rowindex++
return nil
}
type config map[string]string
func (c config) parseDataSource(ds string) error {
u, err := url.Parse(ds)
if err != nil {
return err
}
if u.User != nil {
c["user"] = u.User.Username()
} else {
c["user"] = DefaultUsername
}
if strings.IndexRune(u.Host, ':') == -1 {
c["addr"] = u.Host + ":" + DefaultPort
} else {
c["addr"] = u.Host
}
c["catalog"] = DefaultCatalog
c["schema"] = DefaultSchema
pathSegments := strings.FieldsFunc(u.Path, func(c rune) bool { return c == '/' })
if len(pathSegments) > 0 {
c["catalog"] = pathSegments[0]
}
if len(pathSegments) > 1 {
c["schema"] = pathSegments[1]
}
m, _ := url.ParseQuery(u.RawQuery)
for k, v := range m {
c[k] = strings.Join(v, ",")
}
return nil
}
type valueConverterFunc func(v interface{}) (driver.Value, error)
func (fn valueConverterFunc) ConvertValue(v interface{}) (driver.Value, error) {
return fn(v)
}
/** Stripe's (Data Platform) custom row converter
* Hack: We introduce a custom class that converts unflattened structs in Presto into a JSON string.
*/
type rowConverter struct {
Type string
}
func (rc rowConverter) ConvertValue(v interface{}) (driver.Value, error) {
if v == nil {
return nil, nil
}
// TODO: Write a custom parser to combine "rc.Type" and "v" into something like:
// {_id="dp_9uVcPMp305RgYo",created=1484119972.0129445,open=false,...}
return v, nil
}
var stringConverter = valueConverterFunc(func(val interface{}) (driver.Value, error) {
if val == nil {
return nil, nil
}
return driver.String.ConvertValue(val)
})
var boolConverter = valueConverterFunc(func(val interface{}) (driver.Value, error) {
if val == nil {
return nil, nil
}
return driver.Bool.ConvertValue(val)
})
// bigIntConverter converts a value from the underlying json response into an int64.
// The Go JSON decoder uses float64 for generic numeric values
var bigIntConverter = valueConverterFunc(func(val interface{}) (driver.Value, error) {
if val == nil {
return nil, nil
}
if vv, ok := val.(float64); ok {
return int64(vv), nil
}
return nil, fmt.Errorf("%s: failed to convert %v (%T) into type int64", DriverName, val, val)
})
// doubleConverter converts a value from the underlying json response into an int64.
// The Go JSON decoder uses float64 for generic numeric values
var doubleConverter = valueConverterFunc(func(val interface{}) (driver.Value, error) {
if val == nil {
return nil, nil
}
switch vv := val.(type) {
case float64:
return vv, nil
case string:
switch vv {
case "Infinity":
return math.Inf(1), nil
case "-Infinity":
return math.Inf(-1), nil
case "NaN":
return math.NaN(), nil
}
}
return nil, fmt.Errorf("%s: failed to convert %v (%T) into type float64", DriverName, val, val)
})
// dateConverter converts a value from the underlying json response into a time.Time.
var dateConverter = valueConverterFunc(func(val interface{}) (driver.Value, error) {
if val == nil {
return nil, nil
}
if vv, ok := val.(string); ok {
// BUG: should parse using session time zone.
if ts, err := time.ParseInLocation(DateFormat, vv, time.UTC); err == nil {
return ts, nil
}
}
return nil, fmt.Errorf("%s: failed to convert %v (%T) into type time.Time", DriverName, val, val)
})
// timestampConverter converts a value from the underlying json response into a time.Time.
var timestampConverter = valueConverterFunc(func(val interface{}) (driver.Value, error) {
if val == nil {
return nil, nil
}
if vv, ok := val.(string); ok {
// BUG: should parse using session time zone.
if ts, err := time.ParseInLocation(TimestampFormat, vv, time.UTC); err == nil {
return ts, nil
}
}
return nil, fmt.Errorf("%s: failed to convert %v (%T) into type time.Time", DriverName, val, val)
})
// timestampWithTimezoneConverter converts a value from the underlying json response into a time.Time including timezone.
var timestampWithTimezoneConverter = valueConverterFunc(func(val interface{}) (driver.Value, error) {
if val == nil {
return nil, nil
}
if vv, ok := val.(string); ok {
if len(vv) <= len(TimestampFormat) {
return timestampConverter(val)
}
tzOffset := strings.LastIndex(vv, " ")
if tzOffset == -1 {
return timestampConverter(val)
}
tz, err := time.LoadLocation(strings.TrimSpace(vv[tzOffset:]))
if err != nil {
return nil, err
}
ts, err := time.ParseInLocation(TimestampFormat, vv[:tzOffset], tz)
if err != nil {
return nil, err
}
return ts, nil
}
return nil, fmt.Errorf("%s: failed to convert %v (%T) into type time.Time", DriverName, val, val)
})