forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows_test.go
451 lines (425 loc) · 12.7 KB
/
rows_test.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Copyright (c) 2017-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"io"
"net/http"
"sync"
"testing"
"time"
)
type RowsExtended struct {
rows *sql.Rows
closeChan *chan bool
}
func (rs *RowsExtended) Close() error {
*rs.closeChan <- true
close(*rs.closeChan)
return rs.rows.Close()
}
func (rs *RowsExtended) ColumnTypes() ([]*sql.ColumnType, error) {
return rs.rows.ColumnTypes()
}
func (rs *RowsExtended) Columns() ([]string, error) {
return rs.rows.Columns()
}
func (rs *RowsExtended) Err() error {
return rs.rows.Err()
}
func (rs *RowsExtended) Next() bool {
return rs.rows.Next()
}
func (rs *RowsExtended) NextResultSet() bool {
return rs.rows.NextResultSet()
}
func (rs *RowsExtended) Scan(dest ...interface{}) error {
return rs.rows.Scan(dest...)
}
// test variables
var (
rowsInChunk = 123
)
// Special cases where rows are already closed
func TestRowsClose(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {
rows, err := dbt.db.Query("SELECT 1")
if err != nil {
dbt.Fatal(err)
}
if err = rows.Close(); err != nil {
dbt.Fatal(err)
}
if rows.Next() {
dbt.Fatal("unexpected row after rows.Close()")
}
if err = rows.Err(); err != nil {
dbt.Fatal(err)
}
})
}
func TestResultNoRows(t *testing.T) {
// DDL
runTests(t, dsn, func(dbt *DBTest) {
row, err := dbt.db.Exec("CREATE OR REPLACE TABLE test(c1 int)")
if err != nil {
t.Fatalf("failed to execute DDL. err: %v", err)
}
if _, err = row.RowsAffected(); err == nil {
t.Fatal("should have failed to get RowsAffected")
}
if _, err = row.LastInsertId(); err == nil {
t.Fatal("should have failed to get LastInsertID")
}
})
}
func TestRowsWithoutChunkDownloader(t *testing.T) {
sts1 := "1"
sts2 := "Test1"
var i int
cc := make([][]*string, 0)
for i = 0; i < 10; i++ {
cc = append(cc, []*string{&sts1, &sts2})
}
rt := []execResponseRowType{
{Name: "c1", ByteLength: 10, Length: 10, Type: "FIXED", Scale: 0, Nullable: true},
{Name: "c2", ByteLength: 100000, Length: 100000, Type: "TEXT", Scale: 0, Nullable: false},
}
cm := []execResponseChunk{}
rows := new(snowflakeRows)
rows.sc = nil
rows.ChunkDownloader = &snowflakeChunkDownloader{
sc: nil,
ctx: context.Background(),
Total: int64(len(cc)),
ChunkMetas: cm,
TotalRowIndex: int64(-1),
Qrmk: "",
FuncDownload: nil,
FuncDownloadHelper: nil,
RowSet: rowSetType{RowType: rt, JSON: cc},
QueryResultFormat: "json",
}
rows.ChunkDownloader.start()
dest := make([]driver.Value, 2)
for i = 0; i < len(cc); i++ {
if err := rows.Next(dest); err != nil {
t.Fatalf("failed to get value. err: %v", err)
}
if dest[0] != sts1 {
t.Fatalf("failed to get value. expected: %v, got: %v", sts1, dest[0])
}
if dest[1] != sts2 {
t.Fatalf("failed to get value. expected: %v, got: %v", sts2, dest[1])
}
}
if err := rows.Next(dest); err != io.EOF {
t.Fatalf("failed to finish getting data. err: %v", err)
}
logger.Infof("dest: %v", dest)
}
func downloadChunkTest(ctx context.Context, scd *snowflakeChunkDownloader, idx int) {
d := make([][]*string, 0)
for i := 0; i < rowsInChunk; i++ {
v1 := fmt.Sprintf("%v", idx*1000+i)
v2 := fmt.Sprintf("testchunk%v", idx*1000+i)
d = append(d, []*string{&v1, &v2})
}
scd.ChunksMutex.Lock()
scd.Chunks[idx] = make([]chunkRowType, len(d))
populateJSONRowSet(scd.Chunks[idx], d)
scd.DoneDownloadCond.Broadcast()
scd.ChunksMutex.Unlock()
}
func TestRowsWithChunkDownloader(t *testing.T) {
numChunks := 12
// changed the workers
backupMaxChunkDownloadWorkers := MaxChunkDownloadWorkers
MaxChunkDownloadWorkers = 2
logger.Info("START TESTS")
var i int
cc := make([][]*string, 0)
for i = 0; i < 100; i++ {
v1 := fmt.Sprintf("%v", i)
v2 := fmt.Sprintf("Test%v", i)
cc = append(cc, []*string{&v1, &v2})
}
rt := []execResponseRowType{
{Name: "c1", ByteLength: 10, Length: 10, Type: "FIXED", Scale: 0, Nullable: true},
{Name: "c2", ByteLength: 100000, Length: 100000, Type: "TEXT", Scale: 0, Nullable: false},
}
cm := make([]execResponseChunk, 0)
for i = 0; i < numChunks; i++ {
cm = append(cm, execResponseChunk{URL: fmt.Sprintf("dummyURL%v", i+1), RowCount: rowsInChunk})
}
rows := new(snowflakeRows)
rows.sc = nil
rows.ChunkDownloader = &snowflakeChunkDownloader{
sc: nil,
ctx: context.Background(),
Total: int64(len(cc) + numChunks*rowsInChunk),
ChunkMetas: cm,
TotalRowIndex: int64(-1),
Qrmk: "HAHAHA",
FuncDownload: downloadChunkTest,
RowSet: rowSetType{RowType: rt, JSON: cc},
}
rows.ChunkDownloader.start()
cnt := 0
dest := make([]driver.Value, 2)
var err error
for err != io.EOF {
err := rows.Next(dest)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("failed to get value. err: %v", err)
}
cnt++
}
if cnt != len(cc)+numChunks*rowsInChunk {
t.Fatalf("failed to get all results. expected:%v, got:%v", len(cc)+numChunks*rowsInChunk, cnt)
}
logger.Infof("dest: %v", dest)
MaxChunkDownloadWorkers = backupMaxChunkDownloadWorkers
logger.Info("END TESTS")
}
func downloadChunkTestError(ctx context.Context, scd *snowflakeChunkDownloader, idx int) {
// fail to download 6th and 10th chunk, and retry up to N times and success
// NOTE: zero based index
scd.ChunksMutex.Lock()
defer scd.ChunksMutex.Unlock()
if (idx == 6 || idx == 10) && scd.ChunksErrorCounter < maxChunkDownloaderErrorCounter {
scd.ChunksError <- &chunkError{
Index: idx,
Error: fmt.Errorf(
"dummy error. idx: %v, errCnt: %v", idx+1, scd.ChunksErrorCounter)}
scd.DoneDownloadCond.Broadcast()
return
}
d := make([][]*string, 0)
for i := 0; i < rowsInChunk; i++ {
v1 := fmt.Sprintf("%v", idx*1000+i)
v2 := fmt.Sprintf("testchunk%v", idx*1000+i)
d = append(d, []*string{&v1, &v2})
}
scd.Chunks[idx] = make([]chunkRowType, len(d))
populateJSONRowSet(scd.Chunks[idx], d)
scd.DoneDownloadCond.Broadcast()
}
func TestRowsWithChunkDownloaderError(t *testing.T) {
numChunks := 12
// changed the workers
backupMaxChunkDownloadWorkers := MaxChunkDownloadWorkers
MaxChunkDownloadWorkers = 3
logger.Info("START TESTS")
var i int
cc := make([][]*string, 0)
for i = 0; i < 100; i++ {
v1 := fmt.Sprintf("%v", i)
v2 := fmt.Sprintf("Test%v", i)
cc = append(cc, []*string{&v1, &v2})
}
rt := []execResponseRowType{
{Name: "c1", ByteLength: 10, Length: 10, Type: "FIXED", Scale: 0, Nullable: true},
{Name: "c2", ByteLength: 100000, Length: 100000, Type: "TEXT", Scale: 0, Nullable: false},
}
cm := make([]execResponseChunk, 0)
for i = 0; i < numChunks; i++ {
cm = append(cm, execResponseChunk{URL: fmt.Sprintf("dummyURL%v", i+1), RowCount: rowsInChunk})
}
rows := new(snowflakeRows)
rows.sc = nil
rows.ChunkDownloader = &snowflakeChunkDownloader{
sc: nil,
ctx: context.Background(),
Total: int64(len(cc) + numChunks*rowsInChunk),
ChunkMetas: cm,
TotalRowIndex: int64(-1),
Qrmk: "HOHOHO",
FuncDownload: downloadChunkTestError,
RowSet: rowSetType{RowType: rt, JSON: cc},
}
rows.ChunkDownloader.start()
cnt := 0
dest := make([]driver.Value, 2)
var err error
for err != io.EOF {
err := rows.Next(dest)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("failed to get value. err: %v", err)
}
// fmt.Printf("data: %v\n", dest)
cnt++
}
if cnt != len(cc)+numChunks*rowsInChunk {
t.Fatalf("failed to get all results. expected:%v, got:%v", len(cc)+numChunks*rowsInChunk, cnt)
}
logger.Infof("dest: %v", dest)
MaxChunkDownloadWorkers = backupMaxChunkDownloadWorkers
logger.Info("END TESTS")
}
func downloadChunkTestErrorFail(ctx context.Context, scd *snowflakeChunkDownloader, idx int) {
// fail to download 6th and 10th chunk, and retry up to N times and fail
// NOTE: zero based index
scd.ChunksMutex.Lock()
defer scd.ChunksMutex.Unlock()
if idx == 6 && scd.ChunksErrorCounter <= maxChunkDownloaderErrorCounter {
scd.ChunksError <- &chunkError{
Index: idx,
Error: fmt.Errorf(
"dummy error. idx: %v, errCnt: %v", idx+1, scd.ChunksErrorCounter)}
scd.DoneDownloadCond.Broadcast()
return
}
d := make([][]*string, 0)
for i := 0; i < rowsInChunk; i++ {
v1 := fmt.Sprintf("%v", idx*1000+i)
v2 := fmt.Sprintf("testchunk%v", idx*1000+i)
d = append(d, []*string{&v1, &v2})
}
scd.Chunks[idx] = make([]chunkRowType, len(d))
populateJSONRowSet(scd.Chunks[idx], d)
scd.DoneDownloadCond.Broadcast()
}
func TestRowsWithChunkDownloaderErrorFail(t *testing.T) {
numChunks := 12
// changed the workers
logger.Info("START TESTS")
var i int
cc := make([][]*string, 0)
for i = 0; i < 100; i++ {
v1 := fmt.Sprintf("%v", i)
v2 := fmt.Sprintf("Test%v", i)
cc = append(cc, []*string{&v1, &v2})
}
rt := []execResponseRowType{
{Name: "c1", ByteLength: 10, Length: 10, Type: "FIXED", Scale: 0, Nullable: true},
{Name: "c2", ByteLength: 100000, Length: 100000, Type: "TEXT", Scale: 0, Nullable: false},
}
cm := make([]execResponseChunk, 0)
for i = 0; i < numChunks; i++ {
cm = append(cm, execResponseChunk{URL: fmt.Sprintf("dummyURL%v", i+1), RowCount: rowsInChunk})
}
rows := new(snowflakeRows)
rows.sc = nil
rows.ChunkDownloader = &snowflakeChunkDownloader{
sc: nil,
ctx: context.Background(),
Total: int64(len(cc) + numChunks*rowsInChunk),
ChunkMetas: cm,
TotalRowIndex: int64(-1),
Qrmk: "HOHOHO",
FuncDownload: downloadChunkTestErrorFail,
RowSet: rowSetType{RowType: rt, JSON: cc},
}
rows.ChunkDownloader.start()
cnt := 0
dest := make([]driver.Value, 2)
var err error
for err != io.EOF {
err := rows.Next(dest)
if err == io.EOF {
break
}
if err != nil {
logger.Infof(
"failure was expected by the number of rows is wrong. expected: %v, got: %v", 715, cnt)
break
}
cnt++
}
}
func getChunkTestInvalidResponseBody(_ context.Context, _ *snowflakeChunkDownloader, _ string, _ map[string]string, _ time.Duration) (
*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: &fakeResponseBody{body: []byte{0x12, 0x34}},
}, nil
}
func TestDownloadChunkInvalidResponseBody(t *testing.T) {
numChunks := 2
cm := make([]execResponseChunk, 0)
for i := 0; i < numChunks; i++ {
cm = append(cm, execResponseChunk{URL: fmt.Sprintf(
"dummyURL%v", i+1), RowCount: rowsInChunk})
}
scd := &snowflakeChunkDownloader{
sc: &snowflakeConn{
rest: &snowflakeRestful{RequestTimeout: defaultRequestTimeout},
},
ctx: context.Background(),
ChunkMetas: cm,
TotalRowIndex: int64(-1),
Qrmk: "HOHOHO",
FuncDownload: downloadChunk,
FuncDownloadHelper: downloadChunkHelper,
FuncGet: getChunkTestInvalidResponseBody,
}
scd.ChunksMutex = &sync.Mutex{}
scd.DoneDownloadCond = sync.NewCond(scd.ChunksMutex)
scd.Chunks = make(map[int][]chunkRowType)
scd.ChunksError = make(chan *chunkError, 1)
scd.FuncDownload(scd.ctx, scd, 1)
select {
case errc := <-scd.ChunksError:
if errc.Index != 1 {
t.Fatalf("the error should have caused with chunk idx: %v", errc.Index)
}
default:
t.Fatal("should have caused an error and queued in scd.ChunksError")
}
}
func getChunkTestErrorStatus(_ context.Context, _ *snowflakeChunkDownloader, _ string, _ map[string]string, _ time.Duration) (
*http.Response, error) {
return &http.Response{
StatusCode: http.StatusBadGateway,
Body: &fakeResponseBody{body: []byte{0x12, 0x34}},
}, nil
}
func TestDownloadChunkErrorStatus(t *testing.T) {
numChunks := 2
cm := make([]execResponseChunk, 0)
for i := 0; i < numChunks; i++ {
cm = append(cm, execResponseChunk{URL: fmt.Sprintf(
"dummyURL%v", i+1), RowCount: rowsInChunk})
}
scd := &snowflakeChunkDownloader{
sc: &snowflakeConn{
rest: &snowflakeRestful{RequestTimeout: defaultRequestTimeout},
},
ctx: context.Background(),
ChunkMetas: cm,
TotalRowIndex: int64(-1),
Qrmk: "HOHOHO",
FuncDownload: downloadChunk,
FuncDownloadHelper: downloadChunkHelper,
FuncGet: getChunkTestErrorStatus,
}
scd.ChunksMutex = &sync.Mutex{}
scd.DoneDownloadCond = sync.NewCond(scd.ChunksMutex)
scd.Chunks = make(map[int][]chunkRowType)
scd.ChunksError = make(chan *chunkError, 1)
scd.FuncDownload(scd.ctx, scd, 1)
select {
case errc := <-scd.ChunksError:
if errc.Index != 1 {
t.Fatalf("the error should have caused with chunk idx: %v", errc.Index)
}
serr, ok := errc.Error.(*SnowflakeError)
if !ok {
t.Fatalf("should have been snowflake error. err: %v", errc.Error)
}
if serr.Number != ErrFailedToGetChunk {
t.Fatalf("message error code is not correct. msg: %v", serr.Number)
}
default:
t.Fatal("should have caused an error and queued in scd.ChunksError")
}
}