forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows.go
245 lines (216 loc) · 6.41 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
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
// Copyright (c) 2017-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"database/sql/driver"
"io"
"reflect"
"strings"
"time"
)
const (
headerSseCAlgorithm = "x-amz-server-side-encryption-customer-algorithm"
headerSseCKey = "x-amz-server-side-encryption-customer-key"
headerSseCAes = "AES256"
)
var (
// MaxChunkDownloadWorkers specifies the maximum number of goroutines used to download chunks
MaxChunkDownloadWorkers = 10
// CustomJSONDecoderEnabled has the chunk downloader use the custom JSON decoder to reduce memory footprint.
CustomJSONDecoderEnabled = false
)
var (
maxChunkDownloaderErrorCounter = 5
)
// SnowflakeRows provides an API for methods exposed to the clients
type SnowflakeRows interface {
GetQueryID() string
GetStatus() queryStatus
GetArrowBatches() ([]*ArrowBatch, error)
}
type snowflakeRows struct {
sc *snowflakeConn
ChunkDownloader chunkDownloader
tailChunkDownloader chunkDownloader
queryID string
status queryStatus
err error
errChannel chan error
}
type snowflakeValue interface{}
type chunkRowType struct {
RowSet []*string
ArrowRow []snowflakeValue
}
type rowSetType struct {
RowType []execResponseRowType
JSON [][]*string
RowSetBase64 string
}
type chunkError struct {
Index int
Error error
}
func (rows *snowflakeRows) Close() (err error) {
if err := rows.waitForAsyncQueryStatus(); err != nil {
return err
}
logger.WithContext(rows.sc.ctx).Debugln("Rows.Close")
return nil
}
// ColumnTypeDatabaseTypeName returns the database column name.
func (rows *snowflakeRows) ColumnTypeDatabaseTypeName(index int) string {
if err := rows.waitForAsyncQueryStatus(); err != nil {
return err.Error()
}
return strings.ToUpper(rows.ChunkDownloader.getRowType()[index].Type)
}
// ColumnTypeLength returns the length of the column
func (rows *snowflakeRows) ColumnTypeLength(index int) (length int64, ok bool) {
if err := rows.waitForAsyncQueryStatus(); err != nil {
return 0, false
}
if index < 0 || index > len(rows.ChunkDownloader.getRowType()) {
return 0, false
}
switch rows.ChunkDownloader.getRowType()[index].Type {
case "text", "variant", "object", "array", "binary":
return rows.ChunkDownloader.getRowType()[index].Length, true
}
return 0, false
}
func (rows *snowflakeRows) ColumnTypeNullable(index int) (nullable, ok bool) {
if err := rows.waitForAsyncQueryStatus(); err != nil {
return false, false
}
if index < 0 || index > len(rows.ChunkDownloader.getRowType()) {
return false, false
}
return rows.ChunkDownloader.getRowType()[index].Nullable, true
}
func (rows *snowflakeRows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
if err := rows.waitForAsyncQueryStatus(); err != nil {
return 0, 0, false
}
rowType := rows.ChunkDownloader.getRowType()
if index < 0 || index > len(rowType) {
return 0, 0, false
}
switch rowType[index].Type {
case "fixed":
return rowType[index].Precision, rowType[index].Scale, true
case "time":
return rowType[index].Scale, 0, true
case "timestamp":
return rowType[index].Scale, 0, true
}
return 0, 0, false
}
func (rows *snowflakeRows) Columns() []string {
if err := rows.waitForAsyncQueryStatus(); err != nil {
return make([]string, 0)
}
logger.Debug("Rows.Columns")
ret := make([]string, len(rows.ChunkDownloader.getRowType()))
for i, n := 0, len(rows.ChunkDownloader.getRowType()); i < n; i++ {
ret[i] = rows.ChunkDownloader.getRowType()[i].Name
}
return ret
}
func (rows *snowflakeRows) ColumnTypeScanType(index int) reflect.Type {
if err := rows.waitForAsyncQueryStatus(); err != nil {
return nil
}
return snowflakeTypeToGo(
getSnowflakeType(strings.ToUpper(rows.ChunkDownloader.getRowType()[index].Type)),
rows.ChunkDownloader.getRowType()[index].Scale)
}
func (rows *snowflakeRows) GetQueryID() string {
return rows.queryID
}
func (rows *snowflakeRows) GetStatus() queryStatus {
return rows.status
}
// GetArrowBatches returns an array of ArrowBatch objects to retrieve data in array.Record format
func (rows *snowflakeRows) GetArrowBatches() ([]*ArrowBatch, error) {
// Wait for all arrow batches before fetching.
// Otherwise, a panic error "invalid memory address or nil pointer dereference" will be thrown.
if err := rows.waitForAsyncQueryStatus(); err != nil {
return nil, err
}
return rows.ChunkDownloader.getArrowBatches(), nil
}
func (rows *snowflakeRows) Next(dest []driver.Value) (err error) {
if err = rows.waitForAsyncQueryStatus(); err != nil {
return err
}
row, err := rows.ChunkDownloader.next()
if err != nil {
// includes io.EOF
if err == io.EOF {
rows.ChunkDownloader.reset()
}
return err
}
if rows.ChunkDownloader.getQueryResultFormat() == arrowFormat {
for i, n := 0, len(row.ArrowRow); i < n; i++ {
dest[i] = row.ArrowRow[i]
}
} else {
for i, n := 0, len(row.RowSet); i < n; i++ {
// could move to chunk downloader so that each go routine
// can convert data
var loc *time.Location
if rows.sc != nil {
loc = getCurrentLocation(rows.sc.cfg.Params)
}
err = stringToValue(&dest[i], rows.ChunkDownloader.getRowType()[i], row.RowSet[i], loc)
if err != nil {
return err
}
}
}
return err
}
func (rows *snowflakeRows) HasNextResultSet() bool {
if err := rows.waitForAsyncQueryStatus(); err != nil {
return false
}
return rows.ChunkDownloader.hasNextResultSet()
}
func (rows *snowflakeRows) NextResultSet() error {
if err := rows.waitForAsyncQueryStatus(); err != nil {
return err
}
if len(rows.ChunkDownloader.getChunkMetas()) == 0 {
if rows.ChunkDownloader.getNextChunkDownloader() == nil {
return io.EOF
}
rows.ChunkDownloader = rows.ChunkDownloader.getNextChunkDownloader()
rows.ChunkDownloader.start()
}
return rows.ChunkDownloader.nextResultSet()
}
func (rows *snowflakeRows) waitForAsyncQueryStatus() error {
// if async query, block until query is finished
if rows.status == QueryStatusInProgress {
err := <-rows.errChannel
rows.status = QueryStatusComplete
if err != nil {
rows.status = QueryFailed
rows.err = err
return rows.err
}
} else if rows.status == QueryFailed {
return rows.err
}
return nil
}
func (rows *snowflakeRows) addDownloader(newDL chunkDownloader) {
if rows.ChunkDownloader == nil {
rows.ChunkDownloader = newDL
rows.tailChunkDownloader = newDL
return
}
rows.tailChunkDownloader.setNextChunkDownloader(newDL)
rows.tailChunkDownloader = newDL
}