-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrowset.go
349 lines (293 loc) · 8.46 KB
/
rowset.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
package gohive
import (
"context"
"errors"
"fmt"
"log"
"reflect"
"time"
inf "github.com/dazheng/gohive/inf"
"git.apache.org/thrift.git/lib/go/thrift"
)
type rowSet struct {
thrift *inf.TCLIServiceClient
operation *inf.TOperationHandle
options Options
columns []*inf.TColumnDesc
columnStrs []string
offset int
rowSet *inf.TRowSet
hasMore bool
ready bool
resultSet [][]interface{}
nextRow []interface{}
}
// A RowSet represents an asyncronous hive operation. You can
// Reattach to a previously submitted hive operation if you
// have a valid thrift client, and the serialized Handle()
// from the prior operation.
type RowSet interface {
Handle(ctx context.Context) ([]byte, error)
Columns() []string
Next() bool
Scan(dest ...interface{}) error
Poll() (*Status, error)
Wait() (*Status, error)
}
// Represents job status, including success state and time the
// status was updated.
type Status struct {
state *inf.TOperationState
Error error
At time.Time
}
func newRowSet(thrift *inf.TCLIServiceClient, operation *inf.TOperationHandle, options Options) RowSet {
return &rowSet{thrift, operation, options, nil, nil, 0, nil, true, false, nil, nil}
}
// Issue a thrift call to check for the job's current status.
func (r *rowSet) Poll() (*Status, error) {
req := inf.NewTGetOperationStatusReq()
req.OperationHandle = r.operation
resp, err := r.thrift.GetOperationStatus(context.Background(), req)
if err != nil {
return nil, fmt.Errorf("Error getting status: %+v, %v", resp, err)
}
if !isSuccessStatus(resp.Status) {
return nil, fmt.Errorf("GetStatus call failed: %s", resp.Status.String())
}
if resp.OperationState == nil {
return nil, errors.New("No error from GetStatus, but nil status!")
}
return &Status{resp.OperationState, nil, time.Now()}, nil
}
// Wait until the job is complete, one way or another, returning Status and error.
func (r *rowSet) Wait() (*Status, error) {
for {
status, err := r.Poll()
if err != nil {
return nil, err
}
if status.IsComplete() {
if status.IsSuccess() {
// Fetch operation metadata.
metadataReq := inf.NewTGetResultSetMetadataReq()
metadataReq.OperationHandle = r.operation
metadataResp, err := r.thrift.GetResultSetMetadata(context.Background(), metadataReq)
if err != nil {
return nil, err
}
if !isSuccessStatus(metadataResp.Status) {
return nil, fmt.Errorf("GetResultSetMetadata failed: %s", metadataResp.Status.String())
}
r.columns = metadataResp.Schema.Columns
r.ready = true
return status, nil
}
return nil, fmt.Errorf("Query failed execution: %s", status.state.String())
}
time.Sleep(time.Duration(r.options.PollIntervalSeconds) * time.Second)
}
}
func (r *rowSet) waitForSuccess() error {
if !r.ready {
status, err := r.Wait()
if err != nil {
return err
}
if !status.IsSuccess() || !r.ready {
return fmt.Errorf("Unsuccessful query execution: %+v", status)
}
}
return nil
}
func (r *rowSet) fetchAll() bool {
if !r.hasMore {
return false
}
fetchReq := inf.NewTFetchResultsReq()
fetchReq.OperationHandle = r.operation
fetchReq.Orientation = inf.TFetchOrientation_FETCH_NEXT
fetchReq.MaxRows = r.options.BatchSize
resp, err := r.thrift.FetchResults(context.Background(), fetchReq)
if err != nil {
log.Printf("FetchResults failed: %v\n", err)
return false
}
if !isSuccessStatus(resp.Status) {
log.Printf("FetchResults failed: %s\n", resp.Status.String())
return false
}
r.offset = 0
r.rowSet = resp.GetResults()
r.hasMore = *resp.HasMoreRows
rs := r.rowSet.Columns
colLen := len(rs)
r.resultSet = make([][]interface{}, colLen)
// 先列后行
for i := 0; i < colLen; i++ {
v, length := convertColumn(rs[i])
c := make([]interface{}, length)
for j := 0; j < length; j++ {
c[j] = reflect.ValueOf(v).Index(j).Interface()
}
r.resultSet[i] = c
}
return true
}
// Prepares a row for scanning into memory, by reading data from hive if
// the operation is successful, blocking until the operation is
// complete, if necessary.
// Returns true is a row is available to Scan(), and false if the
// results are empty or any other error occurs.
func (r *rowSet) Next() bool {
if err := r.waitForSuccess(); err != nil {
return false
}
if r.resultSet == nil {
r.fetchAll()
r.offset = 0
// fmt.Println(r.resultSet)
}
if len(r.resultSet) <= 0 {
return false
}
if r.offset >= len(r.resultSet[0]) {
return false
}
r.nextRow = make([]interface{}, 0)
for _, v := range r.resultSet {
r.nextRow = append(r.nextRow, v[r.offset])
}
// fmt.Println(r.nextRow)
r.offset++
return true
}
// Scan the last row prepared via Next() into the destination(s) provided,
// which must be pointers to value types, as in database.sql. Further,
// only pointers of the following types are supported:
// - int, int16, int32, int64
// - string, []byte
// - float64
// - bool
func (r *rowSet) Scan(dest ...interface{}) error {
// TODO: Add type checking and conversion between compatible
// types where possible, as well as some common error checking,
// like passing nil. database/sql's method is very convenient,
// for example: http://golang.org/src/pkg/database/sql/convert.go, like 85
if r.nextRow == nil {
return errors.New("No row to scan! Did you call Next() first?")
}
if len(dest) != len(r.nextRow) {
return fmt.Errorf("Can't scan into %d arguments with input of length %d", len(dest), len(r.nextRow))
}
for i, val := range r.nextRow {
d := dest[i]
switch dt := d.(type) {
case *string:
switch st := val.(type) {
case string:
*dt = st
default:
*dt = fmt.Sprintf("%v", val)
}
case *[]byte:
*dt = []byte(val.(string))
case *int:
*dt = int(val.(int32))
case *int64:
*dt = val.(int64)
case *int32:
*dt = val.(int32)
case *int16:
*dt = val.(int16)
case *float64:
*dt = val.(float64)
case *bool:
*dt = val.(bool)
default:
return fmt.Errorf("Can't scan value of type %T with value %v", dt, val)
}
}
return nil
}
// Returns the names of the columns for the given operation,
// blocking if necessary until the information is available.
func (r *rowSet) Columns() []string {
if r.columnStrs == nil {
if err := r.waitForSuccess(); err != nil {
return nil
}
ret := make([]string, len(r.columns))
for i, col := range r.columns {
ret[i] = col.ColumnName
}
r.columnStrs = ret
}
return r.columnStrs
}
// Return a serialized representation of an identifier that can later
// be used to reattach to a running operation. This identifier and
// serialized representation should be considered opaque by users.
func (r *rowSet) Handle(ctx context.Context) ([]byte, error) {
return serializeOp(ctx, r.operation)
}
func convertColumn(col *inf.TColumn) (colValues interface{}, length int) {
switch {
case col.IsSetStringVal():
return col.GetStringVal().GetValues(), len(col.GetStringVal().GetValues())
case col.IsSetBoolVal():
return col.GetBoolVal().GetValues(), len(col.GetBoolVal().GetValues())
case col.IsSetByteVal():
return col.GetByteVal().GetValues(), len(col.GetByteVal().GetValues())
case col.IsSetI16Val():
return col.GetI16Val().GetValues(), len(col.GetI16Val().GetValues())
case col.IsSetI32Val():
return col.GetI32Val().GetValues(), len(col.GetI32Val().GetValues())
case col.IsSetI64Val():
return col.GetI64Val().GetValues(), len(col.GetI64Val().GetValues())
case col.IsSetDoubleVal():
return col.GetDoubleVal().GetValues(), len(col.GetDoubleVal().GetValues())
default:
return nil, 0
}
}
// Returns a string representation of operation status.
func (s Status) String() string {
if s.state == nil {
return "unknown"
}
return s.state.String()
}
// Returns true if the job has completed or failed.
func (s Status) IsComplete() bool {
if s.state == nil {
return false
}
switch *s.state {
case inf.TOperationState_FINISHED_STATE,
inf.TOperationState_CANCELED_STATE,
inf.TOperationState_CLOSED_STATE,
inf.TOperationState_ERROR_STATE:
return true
}
return false
}
// Returns true if the job compelted successfully.
func (s Status) IsSuccess() bool {
if s.state == nil {
return false
}
return *s.state == inf.TOperationState_FINISHED_STATE
}
func deserializeOp(handle []byte) (*inf.TOperationHandle, error) {
ser := thrift.NewTDeserializer()
var val inf.TOperationHandle
if err := ser.Read(&val, handle); err != nil {
return nil, err
}
return &val, nil
}
func serializeOp(ctx context.Context, operation *inf.TOperationHandle) ([]byte, error) {
ser := thrift.NewTSerializer()
return ser.Write(ctx, operation)
}