-
Notifications
You must be signed in to change notification settings - Fork 100
/
druid.go
221 lines (181 loc) · 5.54 KB
/
druid.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
package druid
import (
"context"
"errors"
"fmt"
"net/url"
"strings"
"github.com/jmoiron/sqlx"
"github.com/mitchellh/mapstructure"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/pkg/activity"
"go.uber.org/zap"
// Load Druid database/sql driver
_ "github.com/rilldata/rill/runtime/drivers/druid/druidsqldriver"
)
func init() {
drivers.Register("druid", &driver{})
drivers.RegisterAsConnector("druid", &driver{})
}
var spec = drivers.Spec{
DisplayName: "Druid",
Description: "Connect to Apache Druid.",
DocsURL: "https://docs.rilldata.com/reference/olap-engines/druid",
ConfigProperties: []*drivers.PropertySpec{
{
Key: "dsn",
Type: drivers.StringPropertyType,
Required: true,
DisplayName: "Connection string",
Placeholder: "https://example.com/druid/v2/sql/avatica-protobuf?authentication=BASIC&avaticaUser=username&avaticaPassword=password",
Secret: true,
},
},
ImplementsOLAP: true,
}
type driver struct{}
var _ drivers.Driver = &driver{}
type configProperties struct {
// DSN is the connection string
DSN string `mapstructure:"dsn"`
// LogQueries controls whether to log the raw SQL passed to OLAP.Execute.
LogQueries bool `mapstructure:"log_queries"`
}
// Opens a connection to Apache Druid using HTTP API.
// Note that the Druid connection string must have the form "http://user:password@host:port/druid/v2/sql".
func (d driver) Open(instanceID string, config map[string]any, client *activity.Client, logger *zap.Logger) (drivers.Handle, error) {
if instanceID == "" {
return nil, errors.New("druid driver can't be shared")
}
conf := &configProperties{}
err := mapstructure.WeakDecode(config, conf)
if err != nil {
return nil, err
}
if conf.DSN == "" {
return nil, fmt.Errorf("no DSN provided to open the connection")
}
dsn, err := correctURL(conf.DSN)
if err != nil {
return nil, err
}
db, err := sqlx.Open("druid", dsn)
if err != nil {
return nil, err
}
// very roughly approximating num queries required for a typical page load
db.SetMaxOpenConns(20)
err = db.Ping()
if err != nil {
return nil, fmt.Errorf("druid: %w", err)
}
conn := &connection{
db: db,
config: conf,
logger: logger,
}
return conn, nil
}
func (d *driver) Spec() drivers.Spec {
return spec
}
func (d *driver) HasAnonymousSourceAccess(ctx context.Context, src map[string]any, logger *zap.Logger) (bool, error) {
return false, fmt.Errorf("not implemented")
}
func (d *driver) TertiarySourceConnectors(ctx context.Context, src map[string]any, logger *zap.Logger) ([]string, error) {
return nil, fmt.Errorf("not implemented")
}
type connection struct {
db *sqlx.DB
config *configProperties
logger *zap.Logger
}
// Driver implements drivers.Connection.
func (c *connection) Driver() string {
return "druid"
}
// Config used to open the Connection
func (c *connection) Config() map[string]any {
m := make(map[string]any, 0)
_ = mapstructure.Decode(c.config, m)
return m
}
// Close implements drivers.Connection.
func (c *connection) Close() error {
return c.db.Close()
}
// Registry implements drivers.Connection.
func (c *connection) AsRegistry() (drivers.RegistryStore, bool) {
return nil, false
}
// Catalog implements drivers.Connection.
func (c *connection) AsCatalogStore(instanceID string) (drivers.CatalogStore, bool) {
return nil, false
}
// Repo implements drivers.Connection.
func (c *connection) AsRepoStore(instanceID string) (drivers.RepoStore, bool) {
return nil, false
}
// AsAdmin implements drivers.Handle.
func (c *connection) AsAdmin(instanceID string) (drivers.AdminService, bool) {
return nil, false
}
// AsAI implements drivers.Handle.
func (c *connection) AsAI(instanceID string) (drivers.AIService, bool) {
return nil, false
}
// OLAP implements drivers.Connection.
func (c *connection) AsOLAP(instanceID string) (drivers.OLAPStore, bool) {
return c, true
}
// Migrate implements drivers.Connection.
func (c *connection) Migrate(ctx context.Context) (err error) {
return nil
}
// MigrationStatus implements drivers.Connection.
func (c *connection) MigrationStatus(ctx context.Context) (current, desired int, err error) {
return 0, 0, nil
}
// AsObjectStore implements drivers.Connection.
func (c *connection) AsObjectStore() (drivers.ObjectStore, bool) {
return nil, false
}
// AsTransporter implements drivers.Connection.
func (c *connection) AsTransporter(from, to drivers.Handle) (drivers.Transporter, bool) {
return nil, false
}
// AsFileStore implements drivers.Connection.
func (c *connection) AsFileStore() (drivers.FileStore, bool) {
return nil, false
}
// AsSQLStore implements drivers.Connection.
// Use OLAPStore instead.
func (c *connection) AsSQLStore() (drivers.SQLStore, bool) {
return nil, false
}
// AsNotifier implements drivers.Connection.
func (c *connection) AsNotifier(properties map[string]any) (drivers.Notifier, error) {
return nil, drivers.ErrNotNotifier
}
func (c *connection) EstimateSize() (int64, bool) {
return 0, false
}
func (c *connection) AcquireLongRunning(ctx context.Context) (func(), error) {
return func() {}, nil
}
func correctURL(dsn string) (string, error) {
u, err := url.Parse(dsn)
if err != nil {
return "", err
}
if strings.Contains(u.Path, "avatica-protobuf") {
avaticaUser := url.QueryEscape(u.Query().Get("avaticaUser"))
avaticaPassword := url.QueryEscape(u.Query().Get("avaticaPassword"))
if avaticaUser != "" {
dsn = u.Scheme + "://" + avaticaUser + ":" + avaticaPassword + "@" + u.Host + "/druid/v2/sql"
} else {
dsn = u.Scheme + "://" + u.Host + "/druid/v2/sql"
}
}
return dsn, nil
}