-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt.go
364 lines (330 loc) · 9.88 KB
/
stmt.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package gohive
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"strings"
"sync"
"github.com/jellybean4/gohive/hive_service/rpc"
log "github.com/sirupsen/logrus"
)
// HiveStatement is a prepared statement. It is bound to a Conn and not
// used by multiple goroutines concurrently.
type HiveStatement struct {
connection *HiveConn
rawQuery string
argsCount int
lock *sync.Mutex
parts []string
sessionHandle *rpc.TSessionHandle
}
type hiveExecContext struct {
session *rpc.TSessionHandle
operation *rpc.TOperationHandle
}
// NewHiveStatement create a new driver.stmt with the given conn
func NewHiveStatement(conn *HiveConn, query string) (*HiveStatement, error) {
parts := SplitWith(query, '?')
stmt := &HiveStatement{
connection: conn,
rawQuery: query,
argsCount: len(parts) - 1,
lock: &sync.Mutex{},
parts: parts,
sessionHandle: conn.SessionHandle,
}
return stmt, nil
}
// Close closes the statement.
//
// As of Go 1.1, a Stmt will not be closed if it's in use
// by any queries.
func (stmt *HiveStatement) Close() error {
stmt.connection = nil
stmt = nil
return nil
}
// NumInput returns the number of placeholder parameters.
//
// If NumInput returns >= 0, the sql package will sanity check
// argument counts from callers and return errors to the caller
// before the statement's Exec or Query methods are called.
//
// NumInput may also return -1, if the driver doesn't know
// its number of placeholders. In that case, the sql package
// will not sanity check Exec or Query argument counts.
func (stmt *HiveStatement) NumInput() int {
return stmt.argsCount
}
// Exec executes a query that doesn't return rows, such
// as an INSERT or UPDATE.
//
// Deprecated: Drivers should implement StmtExecContext instead (or additionally).
func (stmt *HiveStatement) Exec(args []driver.Value) (driver.Result, error) {
stmt.lock.Lock()
ctx := &hiveExecContext{session: stmt.sessionHandle}
stmt.lock.Unlock()
defer func() {
stmt.closeOperation(ctx.operation)
}()
if err := stmt.exec(ctx, args); err != nil {
return nil, err
} else {
return stmt.buildHiveResult(ctx), nil
}
}
// ExecContext executes a query that doesn't return rows, such
// as an INSERT or UPDATE.
//
// ExecContext must honor the context timeout and return when it is canceled.
func (stmt *HiveStatement) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
stmt.lock.Lock()
hiveCtx := &hiveExecContext{session: stmt.sessionHandle}
stmt.lock.Unlock()
defer func() {
stmt.closeOperation(hiveCtx.operation)
}()
recv := make(chan error)
go func() {
recv <- stmt.exec(hiveCtx, NamedValueToValue(args))
}()
select {
case <-ctx.Done():
log.WithFields(log.Fields{
"query": stmt.rawQuery,
}).Info("Hive: Execute canceled")
return nil, errors.New("execute canceled")
case err := <-recv:
if err != nil {
return nil, err
}
return stmt.buildHiveResult(hiveCtx), nil
}
}
// Query executes a query that may return rows, such as a
// SELECT.
//
// Deprecated: Drivers should implement StmtQueryContext instead (or additionally).
func (stmt *HiveStatement) Query(args []driver.Value) (rows driver.Rows, _ error) {
stmt.lock.Lock()
ctx := &hiveExecContext{session: stmt.sessionHandle}
stmt.lock.Unlock()
defer func() {
if rows == nil {
stmt.closeOperation(ctx.operation)
}
}()
if err := stmt.exec(ctx, args); err != nil {
return nil, err
} else if !ctx.operation.HasResultSet {
log.WithFields(log.Fields{
"query": stmt.processQuery,
}).Error("Hive: Query finished without result set")
return nil, errors.New("query finished without result set")
} else if r, err := stmt.buildHiveRows(ctx); err != nil {
return nil, err
} else {
rows = r
return rows, nil
}
}
// QueryContext executes a query that may return rows, such as a
// SELECT.
//
// QueryContext must honor the context timeout and return when it is canceled.
func (stmt *HiveStatement) QueryContext(ctx context.Context, args []driver.NamedValue) (rows driver.Rows, _ error) {
stmt.lock.Lock()
hiveCtx := &hiveExecContext{session: stmt.sessionHandle}
stmt.lock.Unlock()
defer func() {
if rows == nil {
stmt.closeOperation(hiveCtx.operation)
}
}()
recv := make(chan error)
go func() {
recv <- stmt.exec(hiveCtx, NamedValueToValue(args))
}()
select {
case <-ctx.Done():
log.WithFields(log.Fields{
"query": stmt.rawQuery,
}).Info("Hive: Query canceled")
return nil, errors.New("execute canceled")
case err := <-recv:
if err != nil {
return nil, err
} else if !hiveCtx.operation.HasResultSet {
log.WithFields(log.Fields{
"query": stmt.processQuery,
}).Error("Hive: Query finished without result set")
return nil, errors.New("query finished without result set")
} else if r, err := stmt.buildHiveRows(hiveCtx); err != nil {
return nil, err
} else {
rows = r
return rows, nil
}
}
}
// CheckNamedValue is called before passing arguments to the driver
// and is called in place of any ColumnConverter. CheckNamedValue must do type
// validation and conversion as appropriate for the driver.
func (stmt *HiveStatement) CheckNamedValue(value *driver.NamedValue) error {
if value.Name != "" {
return errors.New("named value not supported")
} else if _, ok := value.Value.(string); !ok {
return errors.New("only string type value supported")
}
return nil
}
func (stmt *HiveStatement) exec(ctx *hiveExecContext, args []driver.Value) error {
if stmt.rawQuery == "" {
return ERROR_PREPARED_QUERY_NOT_FOUND
} else if processedQuery, err := stmt.processQuery(args); err != nil {
return err
} else if err := stmt.hiveExec(ctx, processedQuery); err != nil {
return err
} else if err := stmt.waitResponse(ctx.operation); err != nil {
return err
} else {
return nil
}
}
func (stmt *HiveStatement) hiveExec(ctx *hiveExecContext, query string) error {
stmtReq := &rpc.TExecuteStatementReq{
SessionHandle: ctx.session,
Statement: query,
RunAsync: stmt.connection.Config.RunAsync,
QueryTimeout: int64(stmt.connection.Config.ExecTimeout),
}
client := stmt.connection.Client
if resp, err := client.ExecuteStatement(stmtReq); err != nil {
log.WithFields(log.Fields{
"reason": err,
"query": query,
}).Error("Hive: Execute query failed")
return err
} else if err := VerifySuccess(resp.GetStatus(), true); err != nil {
log.WithFields(log.Fields{
"query": query,
}).Error("Hive: Execute query failed")
return err
} else if handle := resp.GetOperationHandle(); handle == nil {
log.WithFields(log.Fields{
"query": query,
}).Error("Hive: Execute query response handler null")
return errors.New("execute query response handler null")
} else {
ctx.operation = handle
return nil
}
}
func (stmt *HiveStatement) waitResponse(handle *rpc.TOperationHandle) error {
statusReq := &rpc.TGetOperationStatusReq{OperationHandle: handle}
client := stmt.connection.Client
for {
if resp, err := client.GetOperationStatus(statusReq); err != nil {
log.WithFields(log.Fields{
"reason": err,
}).Error("Hive: Query execute status failed")
return err
} else if err = VerifySuccess(resp.GetStatus(), true); err != nil {
log.WithFields(log.Fields{
"reason": err,
}).Error("Hive: Query execute status failed")
return err
} else if complete, err := stmt.checkRespStatus(resp); err != nil {
return err
} else if complete {
break
}
}
return nil
}
func (stmt *HiveStatement) buildHiveRows(ctx *hiveExecContext) (*HiveRows, error) {
rows := &HiveRows{
stmt: stmt,
client: stmt.connection.Client,
sessionHandle: ctx.session,
stmtHandle: ctx.operation,
}
if err := rows.init(1000); err != nil {
return nil, err
} else {
return rows, nil
}
}
func (stmt *HiveStatement) buildHiveResult(ctx *hiveExecContext) *HiveResult {
result := &HiveResult{
stmt: stmt,
client: stmt.connection.Client,
sessionHandle: ctx.session,
stmtHandle: ctx.operation,
}
result.init()
return result
}
func (stmt *HiveStatement) checkRespStatus(resp *rpc.TGetOperationStatusResp) (bool, error) {
if resp.IsSetOperationState() {
switch resp.GetOperationState() {
case rpc.TOperationState_CLOSED_STATE, rpc.TOperationState_FINISHED_STATE:
return true, nil
case rpc.TOperationState_CANCELED_STATE:
log.Info("Hive: Query canceled")
return false, errors.New("query canceled")
case rpc.TOperationState_ERROR_STATE:
return false, fmt.Errorf("query failed with message: %s, state: %s, code: %d",
resp.GetErrorMessage(), resp.GetSqlState(), resp.GetErrorCode())
case rpc.TOperationState_UKNOWN_STATE:
return false, errors.New("query state unknown")
case rpc.TOperationState_INITIALIZED_STATE, rpc.TOperationState_PENDING_STATE, rpc.TOperationState_RUNNING_STATE:
return false, nil
}
}
return false, nil
}
func (stmt *HiveStatement) processQuery(args []driver.Value) (string, error) {
query := stmt.rawQuery
if stmt.argsCount != len(args) {
log.WithFields(log.Fields{
"query": query,
"argsCount": len(args),
"expected": stmt.NumInput(),
}).Error("Hive: Args count not match placeholder")
return "", errors.New("args count not match placeholder")
}
// quick return
if stmt.argsCount == 0 {
return query, nil
}
// make sure all value is string
values := make([]string, len(args))
for _, v := range args {
strV, ok := v.(string)
if !ok {
return "", errors.New("Only string value is supported")
}
values = append(values, strV)
}
return strings.Join(stmt.parts, " "), nil
}
func (stmt *HiveStatement) closeOperation(operation *rpc.TOperationHandle) error {
if operation != nil {
req := &rpc.TCloseOperationReq{
OperationHandle: operation,
}
_, err := stmt.connection.Client.CloseOperation(req)
if err != nil {
log.WithFields(log.Fields{
"reason": err,
}).Error("Hive: close operation failed")
return err
}
return nil
} else {
log.Debug("Hive: close operation handler success")
return nil
}
}