forked from segmentio/go-athena
-
Notifications
You must be signed in to change notification settings - Fork 3
/
conn.go
294 lines (245 loc) · 7.28 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
package athena
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"strings"
"time"
uuid "github.com/satori/go.uuid"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"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
resultMode ResultMode
session *session.Session
timeout uint
catalog string
}
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
if len(args) > 0 {
panic("Athena doesn't support prepared statements. Format your own arguments.")
}
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 {
panic("Athena doesn't support prepared statements. Format your own arguments.")
}
_, err := c.runQuery(ctx, query)
return nil, err
}
func (c *conn) runQuery(ctx context.Context, query string) (driver.Rows, error) {
// result mode
isSelect := isSelectQuery(query)
resultMode := c.resultMode
if rmode, ok := getResultMode(ctx); ok {
resultMode = rmode
}
if !isSelect {
resultMode = ResultModeAPI
}
// timeout
timeout := c.timeout
if to, ok := getTimeout(ctx); ok {
timeout = to
}
// catalog
catalog := c.catalog
if cat, ok := getCatalog(ctx); ok {
catalog = cat
}
// output location (with empty value)
if checkOutputLocation(resultMode, c.OutputLocation) {
var err error
c.OutputLocation, err = getOutputLocation(c.athena, c.workgroup)
if err != nil {
return nil, err
}
}
// mode ctas
var ctasTable string
var afterDownload func() error
if isCreatingCTASTable(isSelect, resultMode) {
// Create AS Select
ctasTable = fmt.Sprintf("tmp_ctas_%v", strings.Replace(uuid.NewV4().String(), "-", "", -1))
query = fmt.Sprintf("CREATE TABLE %s WITH (format='TEXTFILE') AS %s", ctasTable, query)
afterDownload = c.dropCTASTable(ctx, ctasTable)
}
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,
SkipHeader: !isDDLQuery(query),
ResultMode: resultMode,
Session: c.session,
OutputLocation: c.OutputLocation,
Timeout: timeout,
AfterDownload: afterDownload,
CTASTable: ctasTable,
DB: c.db,
Catalog: catalog,
})
}
func (c *conn) dropCTASTable(ctx context.Context, table string) func() error {
return func() error {
query := fmt.Sprintf("DROP TABLE %s", table)
queryID, err := c.startQuery(query)
if err != nil {
return err
}
return c.waitOnQuery(ctx, queryID)
}
}
// 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():
c.athena.StopQueryExecution(&athena.StopQueryExecutionInput{
QueryExecutionId: aws.String(queryID),
})
return ctx.Err()
case <-time.After(c.pollFrequency):
continue
}
}
}
func (c *conn) Prepare(query string) (driver.Stmt, error) {
return c.prepareContext(context.Background(), query)
}
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
stmt, err := c.prepareContext(ctx, query)
select {
default:
case <-ctx.Done():
stmt.Close()
return nil, ctx.Err()
}
return stmt, err
}
func (c *conn) prepareContext(ctx context.Context, query string) (driver.Stmt, error) {
// resultMode
isSelect := isSelectQuery(query)
resultMode := c.resultMode
if rmode, ok := getResultMode(ctx); ok {
resultMode = rmode
}
if !isSelect {
resultMode = ResultModeAPI
}
// ctas
var ctasTable string
var afterDownload func() error
if isCreatingCTASTable(isSelect, resultMode) {
// Create AS Select
ctasTable = fmt.Sprintf("tmp_ctas_%v", strings.Replace(uuid.NewV4().String(), "-", "", -1))
query = fmt.Sprintf("CREATE TABLE %s WITH (format='TEXTFILE') AS %s", ctasTable, query)
afterDownload = c.dropCTASTable(ctx, ctasTable)
}
numInput := len(strings.Split(query, "?")) - 1
// prepare
prepareKey := fmt.Sprintf("tmp_prepare_%v", strings.Replace(uuid.NewV4().String(), "-", "", -1))
newQuery := fmt.Sprintf("PREPARE %s FROM %s", prepareKey, query)
queryID, err := c.startQuery(newQuery)
if err != nil {
return nil, err
}
if err := c.waitOnQuery(ctx, queryID); err != nil {
return nil, err
}
return &stmtAthena{
prepareKey: prepareKey,
numInput: numInput,
ctasTable: ctasTable,
afterDownload: afterDownload,
conn: c,
resultMode: resultMode,
}, nil
}
func (c *conn) Begin() (driver.Tx, error) {
panic("Athena doesn't support transactions")
}
func (c *conn) Close() error {
return nil
}
var _ driver.QueryerContext = (*conn)(nil)
var _ driver.ExecerContext = (*conn)(nil)
// HACK(tejasmanohar): database/sql calls Prepare() if your driver doesn't implement
// Queryer. Regardless, db.Query/Exec* calls Query/Exec-Context so I've filed a bug--
// https://github.com/golang/go/issues/22980.
func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
panic("Query() is noop")
}
func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
panic("Exec() is noop")
}
var _ driver.Queryer = (*conn)(nil)
var _ driver.Execer = (*conn)(nil)
// supported DDL statements by Athena
// https://docs.aws.amazon.com/athena/latest/ug/language-reference.html
var ddlQueryRegex = regexp.MustCompile(`(?i)^(ALTER|CREATE|DESCRIBE|DROP|MSCK|SHOW)`)
func isDDLQuery(query string) bool {
return ddlQueryRegex.Match([]byte(query))
}
func isSelectQuery(query string) bool {
return regexp.MustCompile(`(?i)^SELECT`).Match([]byte(query))
}
func isCTASQuery(query string) bool {
return regexp.MustCompile(`(?i)^CREATE.+AS\s+SELECT`).Match([]byte(query))
}
func isCreatingCTASTable(isSelect bool, resultMode ResultMode) bool {
return isSelect && resultMode == ResultModeGzipDL
}