-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatafusion.go
More file actions
286 lines (245 loc) · 6.88 KB
/
datafusion.go
File metadata and controls
286 lines (245 loc) · 6.88 KB
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
package datafusion
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"net/url"
"strings"
"sync"
"github.com/datafusion-contrib/datafusion-go/internal/native"
)
func init() {
sql.Register("datafusion", Driver{})
}
// Driver implements database/sql/driver.Driver for DataFusion.
type Driver struct{}
func (d Driver) Open(dsn string) (driver.Conn, error) {
connector, err := d.OpenConnector(dsn)
if err != nil {
return nil, err
}
conn, err := connector.Connect(context.Background())
if err != nil {
if closer, ok := connector.(interface{ Close() error }); ok {
_ = closer.Close()
}
return nil, err
}
return conn, nil
}
func (Driver) OpenConnector(dsn string) (driver.Connector, error) {
return NewConnector(dsn, nil)
}
// ConnectorOption configures a DataFusion connector.
type ConnectorOption func(*connectorOptions)
type connectorOptions struct {
sharedSession bool
}
func defaultConnectorOptions() connectorOptions {
return connectorOptions{sharedSession: true}
}
// WithSharedSession controls whether connections from one Connector share a DataFusion SessionContext.
func WithSharedSession(shared bool) ConnectorOption {
return func(opts *connectorOptions) {
opts.sharedSession = shared
}
}
// Connector owns the native DataFusion database handle used to open pooled connections.
type Connector struct {
db *native.Database
initFn func(context.Context, driver.ExecerContext) error
sharedSession bool
mu sync.Mutex
sharedInitMu sync.Mutex
ddlMu sync.Mutex
closed bool
sharedInitialized bool
}
// NewConnector creates a DataFusion connector for database/sql.
func NewConnector(dsn string, initFn func(driver.ExecerContext) error) (*Connector, error) {
if initFn == nil {
return NewConnectorWithInitContext(dsn, nil)
}
return NewConnectorWithInitContext(dsn, func(_ context.Context, exec driver.ExecerContext) error {
return initFn(exec)
})
}
// NewConnectorWithInitContext creates a DataFusion connector with an optional
// initialization callback and connector options.
func NewConnectorWithInitContext(dsn string, initFn func(context.Context, driver.ExecerContext) error, options ...ConnectorOption) (*Connector, error) {
opts := defaultConnectorOptions()
for _, option := range options {
if option != nil {
option(&opts)
}
}
normalized, err := normalizeDSN(dsn, &opts)
if err != nil {
return nil, driverError(ErrorConnect, "could not parse datafusion DSN", err)
}
db, err := native.OpenDatabase(normalized)
if err != nil {
return nil, driverError(ErrorConnect, "could not open DataFusion database", err)
}
return &Connector{
db: db,
initFn: initFn,
sharedSession: opts.sharedSession,
}, nil
}
// Driver returns the database/sql driver used by the connector.
func (c *Connector) Driver() driver.Driver {
return Driver{}
}
// Connect opens a new DataFusion connection.
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
nc, err := c.connectNative(ctx)
if err != nil {
return nil, err
}
conn := newConn(nc, c)
if c.initFn != nil {
if c.sharedSession {
if err := c.initializeShared(ctx, conn); err != nil {
_ = conn.Close()
return nil, err
}
} else {
if err := c.initFn(ctx, conn); err != nil {
_ = conn.Close()
return nil, err
}
}
}
return conn, nil
}
func (c *Connector) connectNative(ctx context.Context) (*native.Connection, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return nil, driverError(ErrorClosed, "datafusion connector is closed", nil)
}
nc, err := c.db.Connect(c.sharedSession)
if err != nil {
return nil, driverError(ErrorConnect, "could not connect to DataFusion database", err)
}
return nc, nil
}
func (c *Connector) initializeShared(ctx context.Context, conn driver.ExecerContext) error {
if err := ctx.Err(); err != nil {
return err
}
c.sharedInitMu.Lock()
defer c.sharedInitMu.Unlock()
if c.sharedInitialized {
return nil
}
if err := ctx.Err(); err != nil {
return err
}
c.mu.Lock()
if c.closed {
c.mu.Unlock()
return driverError(ErrorClosed, "datafusion connector is closed", nil)
}
c.mu.Unlock()
if err := c.initFn(ctx, conn); err != nil {
return err
}
c.sharedInitialized = true
return nil
}
func (c *Connector) lockSerializedStatement(ctx context.Context, serializes bool) (func(), error) {
if c == nil || !serializes {
return nil, nil
}
c.ddlMu.Lock()
if err := ctx.Err(); err != nil {
c.ddlMu.Unlock()
return nil, err
}
return c.ddlMu.Unlock, nil
}
// Close releases the connector's DataFusion database resources.
func (c *Connector) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return nil
}
c.closed = true
c.db.Close()
return nil
}
func normalizeDSN(dsn string, opts *connectorOptions) (string, error) {
const supportedDSNs = "supported DSNs are empty string, ?<options>, datafusion://, and datafusion://?<options>"
if dsn == "" {
return "", nil
}
if strings.HasPrefix(dsn, "?") {
return normalizeSessionDSN(dsn[1:], opts)
}
if strings.HasPrefix(dsn, "datafusion://") {
parsed, err := url.Parse(dsn)
if err != nil {
return "", err
}
if parsed.Scheme != "datafusion" {
return "", fmt.Errorf("unsupported DataFusion DSN %q; %s", dsn, supportedDSNs)
}
if parsed.Host != "" {
return "", fmt.Errorf("unsupported DataFusion DSN %q; datafusion:// DSNs do not support hosts", dsn)
}
if parsed.Path != "" && parsed.Path != "/" {
return "", fmt.Errorf("unsupported DataFusion DSN %q; datafusion:// DSNs only support an empty or / path", dsn)
}
return normalizeSessionDSN(parsed.RawQuery, opts)
}
return "", fmt.Errorf("unsupported DataFusion DSN %q; %s", dsn, supportedDSNs)
}
func normalizeSessionDSN(rawQuery string, opts *connectorOptions) (string, error) {
values, err := url.ParseQuery(rawQuery)
if err != nil {
return "", err
}
for key, value := range values {
switch key {
case "datafusion.go.shared_session":
shared, err := parseBoolOption(key, value)
if err != nil {
return "", err
}
opts.sharedSession = shared
delete(values, key)
}
}
encoded := values.Encode()
if encoded == "" {
return "", nil
}
return "?" + encoded, nil
}
func parseBoolOption(key string, values []string) (bool, error) {
if len(values) == 0 || values[0] == "" {
return false, fmt.Errorf("%s requires a boolean value", key)
}
if len(values) > 1 {
return false, fmt.Errorf("%s must be specified at most once", key)
}
switch strings.ToLower(values[0]) {
case "1", "t", "true", "y", "yes", "on":
return true, nil
case "0", "f", "false", "n", "no", "off":
return false, nil
default:
return false, fmt.Errorf("%s must be a boolean value, got %q", key, values[0])
}
}
var _ driver.Driver = Driver{}
var _ driver.DriverContext = Driver{}
var _ driver.Connector = (*Connector)(nil)
var _ interface{ Close() error } = (*Connector)(nil)