-
Notifications
You must be signed in to change notification settings - Fork 5
/
rows.go
200 lines (177 loc) · 4.82 KB
/
rows.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
package godatabend
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"io"
"reflect"
"strings"
"sync/atomic"
)
type resultSchema struct {
columns []string
types []string
parsers []DataParser
}
type nextRows struct {
resultSchema
isClosed int32
isCanceled bool
dc *DatabendConn
ctx context.Context
respData *QueryResponse
}
func waitForData(ctx context.Context, dc *DatabendConn, response *QueryResponse) (*QueryResponse, error) {
if response.Error != nil {
return nil, response.Error
}
var err error
for !response.ReadFinished() && len(response.Data) == 0 && response.Error == nil {
response, err = dc.rest.PollQuery(ctx, response.NextURI)
if err != nil {
if errors.Is(err, context.Canceled) {
// context might be canceled due to timeout or canceled. if it's canceled, we need call
// the kill url to tell the backend it's killed.
dc.log("query canceled", response.ID)
_ = dc.rest.KillQuery(context.Background(), response)
} else {
_ = dc.rest.CloseQuery(ctx, response)
}
return nil, err
}
if response.Error != nil {
_ = dc.rest.CloseQuery(ctx, response)
return nil, fmt.Errorf("query error: %+v", response.Error)
}
}
return response, nil
}
func parse_schema(fields *[]DataField) (*resultSchema, error) {
var columns []string
var types []string
if fields != nil {
for _, field := range *fields {
columns = append(columns, field.Name)
types = append(types, field.Type)
}
}
parsers := make([]DataParser, len(types))
for i, typ := range types {
desc, err := ParseTypeDesc(typ)
if err != nil {
return nil, fmt.Errorf("newTextRows: failed to parse a description of the type '%s': %w", typ, err)
}
parsers[i], err = NewDataParser(desc, &DataParserOptions{})
if err != nil {
return nil, fmt.Errorf("newTextRows: failed to create a data parser for the type '%s': %w", typ, err)
}
}
schema := &resultSchema{
columns: columns,
types: types,
parsers: parsers,
}
return schema, nil
}
func newNextRows(ctx context.Context, dc *DatabendConn, resp *QueryResponse) (*nextRows, error) {
schema, err := parse_schema(resp.Schema)
if err != nil {
return nil, err
}
rows := &nextRows{
dc: dc,
ctx: ctx,
respData: resp,
resultSchema: *schema,
}
return rows, nil
}
func (r *nextRows) Columns() []string {
return r.columns
}
// Close will only be called by sql.Rows once.
// But we can doClose internally as soon as EOF.
//
// Not return error for now.
//
// Note it will also be Called by framework when:
// 1. Canceling query/txn Context.
// 2. Next return error other than io.EOF.
func (r *nextRows) Close() error {
return r.doClose()
}
func (r *nextRows) doClose() error {
if atomic.CompareAndSwapInt32(&r.isClosed, 0, 1) {
if r.respData != nil && len(r.respData.FinalURI) != 0 {
err := r.dc.rest.CloseQuery(r.dc.ctx, r.respData)
if err != nil {
return err
}
r.respData = nil
}
r.dc.cancel = nil
return nil
} else {
// Rows should be safe to close multi times
return nil
}
}
func (r *nextRows) Next(dest []driver.Value) error {
if atomic.LoadInt32(&r.isClosed) == 1 || r.respData == nil {
// If user already called Rows.Close(), Rows.Next() will not get here.
// Get here only because we doClose() internally,
// only when call Rows.Next() again after it return false.
return io.EOF
}
if len(r.respData.Data) == 0 {
var err error
r.respData, err = waitForData(r.ctx, r.dc, r.respData)
if err != nil {
return err
}
}
if len(r.respData.Data) == 0 {
_ = r.doClose()
return io.EOF
}
lineData := r.respData.Data[0]
r.respData.Data = r.respData.Data[1:]
for j := range lineData {
val := lineData[j]
if val == nil {
dest[j] = nil
continue
}
reader := strings.NewReader(*val)
v, err := r.parsers[j].Parse(reader)
if err != nil {
r.dc.log("fail to parse field", j, ", error: ", err)
return err
}
dest[j] = v
}
return nil
}
// ColumnTypeScanType implements the driver.RowsColumnTypeScanType
func (r *nextRows) ColumnTypeScanType(index int) reflect.Type {
return r.parsers[index].Type()
}
// ColumnTypeDatabaseTypeName implements the driver.RowsColumnTypeDatabaseTypeName
func (r *nextRows) ColumnTypeDatabaseTypeName(index int) string {
return r.types[index]
}
// ColumnTypeDatabaseTypeName implements the driver.RowsColumnTypeNullable
func (r *nextRows) ColumnTypeNullable(index int) (bool, bool) {
return r.parsers[index].Nullable(), true
}
// // ColumnTypeDatabaseTypeName implements the driver.RowsColumnTypeLength
// func (r *nextRows) ColumnTypeLength(index int) (int64, bool) {
// // TODO: implement this
// return 10, true
// }
// // ColumnTypeDatabaseTypeName implements the driver.RowsColumnTypePrecisionScale
// func (r *nextRows) ColumnTypePrecisionScale(index int) (int64, int64, bool) {
// // TODO: implement this
// return 10, 10, true
// }