forked from segmentio/go-athena
/
conn.go
145 lines (123 loc) · 3.41 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
package athena
import (
"context"
"database/sql/driver"
"errors"
"time"
"github.com/akrennmair/go-athena/presto"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/athena"
"github.com/aws/aws-sdk-go/service/athena/athenaiface"
)
type conn struct {
athena athenaiface.AthenaAPI
db string
OutputLocation string
workgroup string
pollFrequency time.Duration
}
var (
errTxnsUnsupported = errors.New("transactions are unsupported by Athena")
)
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
if len(args) > 0 {
var err error
query, err = presto.ValidateAndFormatSql(query, convertNamedValues(args)...)
if err != nil {
return nil, err
}
}
rows, err := c.runQuery(ctx, query)
return rows, err
}
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
if len(args) > 0 {
var err error
query, err = presto.ValidateAndFormatSql(query, convertNamedValues(args)...)
if err != nil {
return nil, err
}
}
_, err := c.runQuery(ctx, query)
return nil, err
}
func (c *conn) runQuery(ctx context.Context, query string) (driver.Rows, error) {
queryID, err := c.startQuery(query)
if err != nil {
return nil, err
}
if err := c.waitOnQuery(ctx, queryID); err != nil {
return nil, err
}
return newRows(rowsConfig{
Athena: c.athena,
QueryID: queryID,
// todo add check for ddl queries to not skip header(#10)
SkipHeader: true,
})
}
// startQuery starts an Athena query and returns its ID.
func (c *conn) startQuery(query string) (string, error) {
resp, err := c.athena.StartQueryExecution(&athena.StartQueryExecutionInput{
QueryString: aws.String(query),
QueryExecutionContext: &athena.QueryExecutionContext{
Database: aws.String(c.db),
},
ResultConfiguration: &athena.ResultConfiguration{
OutputLocation: aws.String(c.OutputLocation),
},
WorkGroup: aws.String(c.workgroup),
})
if err != nil {
return "", err
}
return *resp.QueryExecutionId, nil
}
// waitOnQuery blocks until a query finishes, returning an error if it failed.
func (c *conn) waitOnQuery(ctx context.Context, queryID string) error {
for {
statusResp, err := c.athena.GetQueryExecutionWithContext(ctx, &athena.GetQueryExecutionInput{
QueryExecutionId: aws.String(queryID),
})
if err != nil {
return err
}
switch *statusResp.QueryExecution.Status.State {
case athena.QueryExecutionStateCancelled:
return context.Canceled
case athena.QueryExecutionStateFailed:
reason := *statusResp.QueryExecution.Status.StateChangeReason
return errors.New(reason)
case athena.QueryExecutionStateSucceeded:
return nil
case athena.QueryExecutionStateQueued:
case athena.QueryExecutionStateRunning:
}
select {
case <-ctx.Done():
if _, err := c.athena.StopQueryExecution(&athena.StopQueryExecutionInput{
QueryExecutionId: aws.String(queryID),
}); err != nil {
return err
}
return ctx.Err()
case <-time.After(c.pollFrequency):
continue
}
}
}
func (c *conn) Prepare(query string) (driver.Stmt, error) {
prep, err := presto.ParseSQL(query)
if err != nil {
return nil, err
}
return &stmt{prep: prep, c: c}, nil
}
func (c *conn) Begin() (driver.Tx, error) {
return nil, errTxnsUnsupported
}
func (c *conn) Close() error {
return nil
}
var _ driver.QueryerContext = (*conn)(nil)
var _ driver.ExecerContext = (*conn)(nil)