forked from segmentio/go-athena
-
Notifications
You must be signed in to change notification settings - Fork 3
/
driver.go
228 lines (197 loc) · 6.04 KB
/
driver.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
package athena
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"github.com/aws/aws-sdk-go/service/athena/athenaiface"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/athena"
)
var (
openFromSessionMutex sync.Mutex
openFromSessionCount int
)
const (
// timeOutLimitDefault athena's timeout limit
timeOutLimitDefault uint = 1800
)
// Driver is a sql.Driver. It's intended for db/sql.Open().
type Driver struct {
cfg *Config
}
// NewDriver allows you to register your own driver with `sql.Register`.
// It's useful for more complex use cases. Read more in PR #3.
// https://github.com/segmentio/go-athena/pull/3
//
// Generally, sql.Open() or athena.Open() should suffice.
func NewDriver(cfg *Config) *Driver {
return &Driver{cfg}
}
func init() {
var drv driver.Driver = &Driver{}
sql.Register("athena", drv)
}
// Open should be used via `db/sql.Open("athena", "<params>")`.
// The following parameters are supported in URI query format (k=v&k2=v2&...)
//
// - `db` (required)
// This is the Athena database name. In the UI, this defaults to "default",
// but the driver requires it regardless.
//
// - `output_location` (required)
// This is the S3 location Athena will dump query results in the format
// "s3://bucket/and/so/forth". In the AWS UI, this defaults to
// "s3://aws-athena-query-results-<ACCOUNTID>-<REGION>", but the driver requires it.
//
// - `poll_frequency` (optional)
// Athena's API requires polling to retrieve query results. This is the frequency at
// which the driver will poll for results. It should be a time/Duration.String().
// A completely arbitrary default of "5s" was chosen.
//
// - `region` (optional)
// Override AWS region. Useful if it is not set with environment variable.
//
// - `workgroup` (optional)
// Athena's workgroup. This defaults to "primary".
//
// Credentials must be accessible via the SDK's Default Credential Provider Chain.
// For more advanced AWS credentials/session/config management, please supply
// a custom AWS session directly via `athena.Open()`.
func (d *Driver) Open(connStr string) (driver.Conn, error) {
cfg := d.cfg
if cfg == nil {
var err error
cfg, err = configFromConnectionString(connStr)
if err != nil {
return nil, err
}
}
if cfg.PollFrequency == 0 {
cfg.PollFrequency = 5 * time.Second
}
// athena client
athenaClient := athena.New(cfg.Session)
// output location (with empty value)
if checkOutputLocation(cfg.ResultMode, cfg.OutputLocation) {
var err error
cfg.OutputLocation, err = getOutputLocation(athenaClient, cfg.WorkGroup)
if err != nil {
return nil, err
}
}
return &conn{
athena: athenaClient,
db: cfg.Database,
OutputLocation: cfg.OutputLocation,
pollFrequency: cfg.PollFrequency,
workgroup: cfg.WorkGroup,
resultMode: cfg.ResultMode,
session: cfg.Session,
timeout: cfg.Timeout,
catalog: cfg.Catalog,
}, nil
}
// Open is a more robust version of `db.Open`, as it accepts a raw aws.Session.
// This is useful if you have a complex AWS session since the driver doesn't
// currently attempt to serialize all options into a string.
func Open(cfg Config) (*sql.DB, error) {
if cfg.Database == "" {
return nil, errors.New("db is required")
}
if cfg.Session == nil {
return nil, errors.New("session is required")
}
if cfg.WorkGroup == "" {
cfg.WorkGroup = "primary"
}
// This hack was copied from jackc/pgx. Sorry :(
// https://github.com/jackc/pgx/blob/70a284f4f33a9cc28fd1223f6b83fb00deecfe33/stdlib/sql.go#L130-L136
openFromSessionMutex.Lock()
openFromSessionCount++
name := fmt.Sprintf("athena-%d", openFromSessionCount)
openFromSessionMutex.Unlock()
sql.Register(name, &Driver{&cfg})
return sql.Open(name, "")
}
// Config is the input to Open().
type Config struct {
Session *session.Session
Database string
OutputLocation string
WorkGroup string
PollFrequency time.Duration
ResultMode ResultMode
Timeout uint
Catalog string
}
func configFromConnectionString(connStr string) (*Config, error) {
args, err := url.ParseQuery(connStr)
if err != nil {
return nil, err
}
var cfg Config
var acfg []*aws.Config
if region := args.Get("region"); region != "" {
acfg = append(acfg, &aws.Config{Region: aws.String(region)})
}
cfg.Session, err = session.NewSession(acfg...)
if err != nil {
return nil, err
}
cfg.Database = args.Get("db")
cfg.OutputLocation = args.Get("output_location")
cfg.WorkGroup = args.Get("workgroup")
if cfg.WorkGroup == "" {
cfg.WorkGroup = "primary"
}
frequencyStr := args.Get("poll_frequency")
if frequencyStr != "" {
cfg.PollFrequency, err = time.ParseDuration(frequencyStr)
if err != nil {
return nil, fmt.Errorf("invalid poll_frequency parameter: %s", frequencyStr)
}
}
cfg.ResultMode = ResultModeAPI
modeValue := strings.ToLower(args.Get("result_mode"))
switch {
case modeValue == "dl" || modeValue == "download":
cfg.ResultMode = ResultModeDL
case modeValue == "gzip":
cfg.ResultMode = ResultModeGzipDL
}
cfg.Timeout = timeOutLimitDefault
if tm := args.Get("timeout"); tm != "" {
if timeout, err := strconv.ParseUint(tm, 10, 32); err != nil {
cfg.Timeout = uint(timeout)
}
}
cfg.Catalog = CATALOG_AWS_DATA_CATALOG
if ct := args.Get("catalog"); ct != "" {
cfg.Catalog = ct
}
return &cfg, nil
}
// checkOutputLocation is to check if outputLocation should be obtained from workgroup.
func checkOutputLocation(resultMode ResultMode, outputLocation string) bool {
return resultMode != ResultModeAPI && outputLocation == ""
}
// getOutputLocation is for getting output location value from workgroup when location value is empty.
func getOutputLocation(athenaClient athenaiface.AthenaAPI, workGroup string) (string, error) {
var outputLocation string
output, err := athenaClient.GetWorkGroup(
&athena.GetWorkGroupInput{
WorkGroup: aws.String(workGroup),
},
)
if err == nil {
outputLocation = *output.WorkGroup.Configuration.ResultConfiguration.OutputLocation
}
return outputLocation, err
}