forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk_test.go
More file actions
559 lines (488 loc) · 16.3 KB
/
chunk_test.go
File metadata and controls
559 lines (488 loc) · 16.3 KB
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package gosnowflake
import (
"bytes"
"context"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
errors2 "github.com/snowflakedb/gosnowflake/v2/internal/errors"
"io"
"strings"
"sync"
"testing"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/memory"
ia "github.com/snowflakedb/gosnowflake/v2/internal/arrow"
)
func TestBadChunkData(t *testing.T) {
testDecodeErr(t, "")
testDecodeErr(t, "null")
testDecodeErr(t, "42")
testDecodeErr(t, "\"null\"")
testDecodeErr(t, "{}")
testDecodeErr(t, "[[]")
testDecodeErr(t, "[null]")
testDecodeErr(t, `[[hello world]]`)
testDecodeErr(t, `[[""hello world""]]`)
testDecodeErr(t, `[["\"hello world""]]`)
testDecodeErr(t, `[[""hello world\""]]`)
testDecodeErr(t, `[["hello world`)
testDecodeErr(t, `[["hello world"`)
testDecodeErr(t, `[["hello world"]`)
testDecodeErr(t, `[["\uQQQQ"]]`)
for b := range byte(' ') {
testDecodeErr(t, string([]byte{
'[', '[', '"', b, '"', ']', ']',
}))
}
}
func TestValidChunkData(t *testing.T) {
testDecodeOk(t, "[]")
testDecodeOk(t, "[ ]")
testDecodeOk(t, "[[]]")
testDecodeOk(t, "[ [ ] ]")
testDecodeOk(t, "[[],[],[],[]]")
testDecodeOk(t, "[[] , [] , [], [] ]")
testDecodeOk(t, "[[null]]")
testDecodeOk(t, "[[\n\t\r null]]")
testDecodeOk(t, "[[null,null]]")
testDecodeOk(t, "[[ null , null ]]")
testDecodeOk(t, "[[null],[null],[null]]")
testDecodeOk(t, "[[null],[ null ] , [null]]")
testDecodeOk(t, `[[""]]`)
testDecodeOk(t, `[["false"]]`)
testDecodeOk(t, `[["true"]]`)
testDecodeOk(t, `[["42"]]`)
testDecodeOk(t, `[[""]]`)
testDecodeOk(t, `[["hello"]]`)
testDecodeOk(t, `[["hello world"]]`)
testDecodeOk(t, `[["/ ' \\ \b \t \n \f \r \""]]`)
testDecodeOk(t, `[["❄"]]`)
testDecodeOk(t, `[["\u2744"]]`)
testDecodeOk(t, `[["\uFfFc"]]`) // consume replacement chars
testDecodeOk(t, `[["\ufffd"]]`) // consume replacement chars
testDecodeOk(t, `[["\u0000"]]`) // yes, this is valid
testDecodeOk(t, `[["\uD834\uDD1E"]]`) // surrogate pair
testDecodeOk(t, `[["\uD834\u0000"]]`) // corrupt surrogate pair
testDecodeOk(t, `[["$"]]`) // "$"
testDecodeOk(t, `[["\u0024"]]`) // "$"
testDecodeOk(t, `[["\uC2A2"]]`) // "¢"
testDecodeOk(t, `[["¢"]]`) // "¢"
testDecodeOk(t, `[["\u00E2\u82AC"]]`) // "€"
testDecodeOk(t, `[["€"]]`) // "€"
testDecodeOk(t, `[["\uF090\u8D88"]]`) // "𐍈"
testDecodeOk(t, `[["𐍈"]]`) // "𐍈"
}
func TestSmallBufferChunkData(t *testing.T) {
r := strings.NewReader(`[
[null,"hello world"],
["foo bar", null],
[null, null] ,
["foo bar", "hello world" ]
]`)
lcd := largeChunkDecoder{
r, 0, 0,
0, 0,
make([]byte, 1),
bytes.NewBuffer(make([]byte, defaultStringBufferSize)),
nil,
}
if _, err := lcd.decode(); err != nil {
t.Fatalf("failed with small buffer: %s", err)
}
}
func TestEnsureBytes(t *testing.T) {
// the content here doesn't matter
r := strings.NewReader("0123456789")
lcd := largeChunkDecoder{
r, 0, 0,
3, 8189,
make([]byte, 8192),
bytes.NewBuffer(make([]byte, defaultStringBufferSize)),
nil,
}
lcd.ensureBytes(4)
// we expect the new remainder to be 3 + 10 (length of r)
if lcd.rem != 13 {
t.Fatalf("buffer was not refilled correctly")
}
}
func testDecodeOk(t *testing.T, s string) {
var rows [][]*string
if err := json.Unmarshal([]byte(s), &rows); err != nil {
t.Fatalf("test case is not valid json / [][]*string: %s", s)
}
// NOTE we parse and stringify the expected result to
// remove superficial differences, like whitespace
expect, err := json.Marshal(rows)
if err != nil {
t.Fatalf("unreachable: %s", err)
}
rows, err = decodeLargeChunk(strings.NewReader(s), 0, 0)
if err != nil {
t.Fatalf("expected decode to succeed: %s", err)
}
actual, err := json.Marshal(rows)
if err != nil {
t.Fatalf("json marshal failed: %s", err)
}
if string(actual) != string(expect) {
t.Fatalf(`
result did not match expected result
expect=%s
bytes=(%v)
acutal=%s
bytes=(%v)`,
string(expect), expect,
string(actual), actual,
)
}
}
func testDecodeErr(t *testing.T, s string) {
if _, err := decodeLargeChunk(strings.NewReader(s), 0, 0); err == nil {
t.Fatalf("expected decode to fail for input: %s", s)
}
}
func TestEnableArrowBatches(t *testing.T) {
runSnowflakeConnTest(t, func(sct *SCTest) {
ctx := ia.EnableArrowBatches(sct.sc.ctx)
numrows := 3000 // approximately 6 ArrowBatch objects
pool := memory.NewCheckedAllocator(memory.DefaultAllocator)
defer pool.AssertSize(t, 0)
ctx = WithArrowAllocator(ctx, pool)
query := fmt.Sprintf(selectRandomGenerator, numrows)
rows := sct.mustQueryContext(ctx, query, []driver.NamedValue{})
defer rows.Close()
// getting result batches via raw bridge
info, err := rows.(*snowflakeRows).GetArrowBatches()
if err != nil {
t.Error(err)
}
batches := info.Batches
numBatches := len(batches)
maxWorkers := 10 // enough for 3000 rows
type count struct {
m sync.Mutex
recVal int
metaVal int
}
cnt := count{recVal: 0}
var wg sync.WaitGroup
chunks := make(chan int, numBatches)
for w := 1; w <= maxWorkers; w++ {
wg.Add(1)
go func(wg *sync.WaitGroup, chunks <-chan int) {
defer wg.Done()
for i := range chunks {
batch := batches[i]
var recs *[]arrow.Record
if batch.Records != nil {
recs = batch.Records
} else if batch.Download != nil {
var downloadErr error
recs, _, downloadErr = batch.Download(context.Background())
if downloadErr != nil {
t.Error(downloadErr)
}
}
if recs != nil {
for _, r := range *recs {
cnt.m.Lock()
cnt.recVal += int(r.NumRows())
cnt.m.Unlock()
r.Release()
}
}
cnt.m.Lock()
cnt.metaVal += batch.RowCount
cnt.m.Unlock()
}
}(&wg, chunks)
}
for j := range numBatches {
chunks <- j
}
close(chunks)
wg.Wait()
if cnt.recVal != numrows {
t.Errorf("number of rows from records didn't match. expected: %v, got: %v", numrows, cnt.recVal)
}
if cnt.metaVal != numrows {
t.Errorf("number of rows from arrow batch metadata didn't match. expected: %v, got: %v", numrows, cnt.metaVal)
}
})
}
func TestWithArrowBatchesAsync(t *testing.T) {
runSnowflakeConnTest(t, func(sct *SCTest) {
ctx := WithAsyncMode(sct.sc.ctx)
ctx = ia.EnableArrowBatches(ctx)
numrows := 50000
pool := memory.NewCheckedAllocator(memory.DefaultAllocator)
defer pool.AssertSize(t, 0)
ctx = WithArrowAllocator(ctx, pool)
query := fmt.Sprintf(selectRandomGenerator, numrows)
rows := sct.mustQueryContext(ctx, query, []driver.NamedValue{})
defer rows.Close()
info, err := rows.(*snowflakeRows).GetArrowBatches()
if err != nil {
t.Error(err)
}
batches := info.Batches
numBatches := len(batches)
maxWorkers := 10
type count struct {
m sync.Mutex
recVal int
metaVal int
}
cnt := count{recVal: 0}
var wg sync.WaitGroup
chunks := make(chan int, numBatches)
for w := 1; w <= maxWorkers; w++ {
wg.Add(1)
go func(wg *sync.WaitGroup, chunks <-chan int) {
defer wg.Done()
for i := range chunks {
batch := batches[i]
var recs *[]arrow.Record
if batch.Records != nil {
recs = batch.Records
} else if batch.Download != nil {
var downloadErr error
recs, _, downloadErr = batch.Download(context.Background())
if downloadErr != nil {
t.Error(downloadErr)
}
}
if recs != nil {
for _, r := range *recs {
cnt.m.Lock()
cnt.recVal += int(r.NumRows())
cnt.m.Unlock()
r.Release()
}
}
cnt.m.Lock()
cnt.metaVal += batch.RowCount
cnt.m.Unlock()
}
}(&wg, chunks)
}
for j := range numBatches {
chunks <- j
}
close(chunks)
wg.Wait()
if cnt.recVal != numrows {
t.Errorf("number of rows from records didn't match. expected: %v, got: %v", numrows, cnt.recVal)
}
if cnt.metaVal != numrows {
t.Errorf("number of rows from arrow batch metadata didn't match. expected: %v, got: %v", numrows, cnt.metaVal)
}
})
}
func TestWithArrowBatchesButReturningJSON(t *testing.T) {
testWithArrowBatchesButReturningJSON(t, false)
}
func TestWithArrowBatchesButReturningJSONAsync(t *testing.T) {
testWithArrowBatchesButReturningJSON(t, true)
}
func testWithArrowBatchesButReturningJSON(t *testing.T, async bool) {
runSnowflakeConnTest(t, func(sct *SCTest) {
requestID := NewUUID()
pool := memory.NewCheckedAllocator(memory.DefaultAllocator)
defer pool.AssertSize(t, 0)
ctx := WithArrowAllocator(context.Background(), pool)
ctx = ia.EnableArrowBatches(ctx)
ctx = WithRequestID(ctx, requestID)
if async {
ctx = WithAsyncMode(ctx)
}
sct.mustExec(forceJSON, nil)
rows := sct.mustQueryContext(ctx, "SELECT 'hello'", nil)
defer rows.Close()
_, err := rows.(ia.BatchDataProvider).GetArrowBatches()
assertNotNilF(t, err)
var se *SnowflakeError
assertTrueE(t, errors.As(err, &se))
assertEqualE(t, se.Message, errors2.ErrMsgNonArrowResponseInArrowBatches)
assertEqualE(t, se.Number, ErrNonArrowResponseInArrowBatches)
v := make([]driver.Value, 1)
assertNilE(t, rows.Next(v))
assertEqualE(t, v[0], "hello")
})
}
func TestWithArrowBatchesMultistatement(t *testing.T) {
testWithArrowBatchesMultistatement(t, false)
}
func TestWithArrowBatchesMultistatementAsync(t *testing.T) {
testWithArrowBatchesMultistatement(t, true)
}
func testWithArrowBatchesMultistatement(t *testing.T, async bool) {
runSnowflakeConnTest(t, func(sct *SCTest) {
sct.mustExec("ALTER SESSION SET ENABLE_FIX_1758055_ADD_ARROW_SUPPORT_FOR_MULTI_STMTS = true", nil)
pool := memory.NewCheckedAllocator(memory.DefaultAllocator)
defer pool.AssertSize(t, 0)
ctx := WithMultiStatement(ia.EnableArrowBatches(WithArrowAllocator(context.Background(), pool)), 2)
if async {
ctx = WithAsyncMode(ctx)
}
driverRows := sct.mustQueryContext(ctx, "SELECT 'abc' UNION SELECT 'def' ORDER BY 1; SELECT 'ghi' UNION SELECT 'jkl' ORDER BY 1", nil)
defer driverRows.Close()
sfRows := driverRows.(SnowflakeRows)
expectedResults := [][]string{{"abc", "def"}, {"ghi", "jkl"}}
resultSetIdx := 0
for hasNextResultSet := true; hasNextResultSet; hasNextResultSet = sfRows.NextResultSet() != io.EOF {
info, err := driverRows.(ia.BatchDataProvider).GetArrowBatches()
assertNilF(t, err)
assertEqualF(t, len(info.Batches), 1)
batch := info.Batches[0]
assertNotNilF(t, batch.Records)
records := *batch.Records
assertEqualF(t, len(records), 1)
record := records[0]
defer record.Release()
assertEqualF(t, record.Column(0).(*array.String).Value(0), expectedResults[resultSetIdx][0])
assertEqualF(t, record.Column(0).(*array.String).Value(1), expectedResults[resultSetIdx][1])
resultSetIdx++
}
assertEqualF(t, resultSetIdx, len(expectedResults))
err := sfRows.NextResultSet()
assertErrIsE(t, err, io.EOF)
})
}
func TestWithArrowBatchesMultistatementWithJSONResponse(t *testing.T) {
runSnowflakeConnTest(t, func(sct *SCTest) {
sct.mustExec(forceJSON, nil)
pool := memory.NewCheckedAllocator(memory.DefaultAllocator)
defer pool.AssertSize(t, 0)
ctx := WithMultiStatement(ia.EnableArrowBatches(WithArrowAllocator(context.Background(), pool)), 2)
driverRows := sct.mustQueryContext(ctx, "SELECT 'abc' UNION SELECT 'def' ORDER BY 1; SELECT 'ghi' UNION SELECT 'jkl' ORDER BY 1", nil)
defer driverRows.Close()
sfRows := driverRows.(SnowflakeRows)
resultSetIdx := 0
for hasNextResultSet := true; hasNextResultSet; hasNextResultSet = sfRows.NextResultSet() != io.EOF {
_, err := driverRows.(ia.BatchDataProvider).GetArrowBatches()
assertNotNilF(t, err)
var se *SnowflakeError
assertTrueF(t, errors.As(err, &se))
assertEqualE(t, se.Number, ErrNonArrowResponseInArrowBatches)
assertEqualE(t, se.Message, errors2.ErrMsgNonArrowResponseInArrowBatches)
resultSetIdx++
}
assertEqualF(t, resultSetIdx, 2)
err := sfRows.NextResultSet()
assertErrIsE(t, err, io.EOF)
})
}
func TestWithArrowBatchesMultistatementWithLargeResultSet(t *testing.T) {
runSnowflakeConnTest(t, func(sct *SCTest) {
sct.mustExec("ALTER SESSION SET ENABLE_FIX_1758055_ADD_ARROW_SUPPORT_FOR_MULTI_STMTS = true", nil)
pool := memory.NewCheckedAllocator(memory.DefaultAllocator)
defer pool.AssertSize(t, 0)
ctx := WithMultiStatement(ia.EnableArrowBatches(WithArrowAllocator(context.Background(), pool)), 2)
driverRows := sct.mustQueryContext(ctx, "SELECT 'abc' FROM TABLE(GENERATOR(ROWCOUNT => 1000000)); SELECT 'abc' FROM TABLE(GENERATOR(ROWCOUNT => 1000000))", nil)
defer driverRows.Close()
sfRows := driverRows.(SnowflakeRows)
rowCount := 0
for hasNextResultSet := true; hasNextResultSet; hasNextResultSet = sfRows.NextResultSet() != io.EOF {
info, err := driverRows.(ia.BatchDataProvider).GetArrowBatches()
assertNilF(t, err)
assertTrueF(t, len(info.Batches) > 1)
for _, batch := range info.Batches {
var recs *[]arrow.Record
if batch.Records != nil {
recs = batch.Records
} else if batch.Download != nil {
recs, _, err = batch.Download(context.Background())
assertNilF(t, err)
}
if recs != nil {
for _, record := range *recs {
defer record.Release()
for i := 0; i < int(record.NumRows()); i++ {
assertEqualF(t, record.Column(0).(*array.String).Value(i), "abc")
rowCount++
}
}
}
}
}
err := sfRows.NextResultSet()
assertErrIsE(t, err, io.EOF)
})
}
func TestQueryArrowStream(t *testing.T) {
runSnowflakeConnTest(t, func(sct *SCTest) {
numrows := 50000
query := fmt.Sprintf(selectRandomGenerator, numrows)
loader, err := sct.sc.QueryArrowStream(sct.sc.ctx, query)
assertNilF(t, err)
if loader.TotalRows() != int64(numrows) {
t.Errorf("total numrows did not match expected, wanted %v, got %v", numrows, loader.TotalRows())
}
batches, err := loader.GetBatches()
assertNilF(t, err)
assertTrueF(t, len(batches) > 0, "should have at least one batch")
assertTrueF(t, len(loader.RowTypes()) > 0, "should have row types")
})
}
func TestQueryArrowStreamDescribeOnly(t *testing.T) {
runSnowflakeConnTest(t, func(sct *SCTest) {
numrows := 50000
query := fmt.Sprintf(selectRandomGenerator, numrows)
loader, err := sct.sc.QueryArrowStream(WithDescribeOnly(sct.sc.ctx), query)
assertNilF(t, err, "failed to run query")
if loader.TotalRows() != 0 {
t.Errorf("total numrows did not match expected, wanted 0, got %v", loader.TotalRows())
}
if len(loader.RowTypes()) != 2 {
t.Errorf("rowTypes length did not match expected, wanted 2, got %v", len(loader.RowTypes()))
}
})
}
func TestRetainChunkWOHighPrecision(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
var rows driver.Rows
var err error
err = dbt.conn.Raw(func(connection interface{}) error {
rows, err = connection.(driver.QueryerContext).QueryContext(ia.EnableArrowBatches(context.Background()), "select 0", nil)
return err
})
assertNilF(t, err, "error running select 0 query")
info, err := rows.(ia.BatchDataProvider).GetArrowBatches()
assertNilF(t, err, "error getting arrow batch data")
assertEqualF(t, len(info.Batches), 1, "should have one batch")
records := info.Batches[0].Records
assertNotNilF(t, records, "records should not be nil")
numRecords := len(*records)
assertEqualF(t, numRecords, 1, "should have exactly one record")
record := (*records)[0]
assertEqualF(t, len(record.Columns()), 1, "should have exactly one column")
column := record.Column(0).(*array.Int8)
row := column.Len()
assertEqualF(t, row, 1, "should have exactly one row")
int8Val := column.Value(0)
assertEqualF(t, int8Val, int8(0), "value of cell should be 0")
})
}
func TestQueryArrowStreamMultiStatement(t *testing.T) {
runSnowflakeConnTest(t, func(sct *SCTest) {
sct.mustExec("ALTER SESSION SET ENABLE_FIX_1758055_ADD_ARROW_SUPPORT_FOR_MULTI_STMTS = true", nil)
ctx := WithMultiStatement(ia.EnableArrowBatches(sct.sc.ctx), 2)
loader, err := sct.sc.QueryArrowStream(ctx, "SELECT 'abc'; SELECT 'abc' UNION SELECT 'def' ORDER BY 1")
assertNilF(t, err)
assertTrueF(t, len(loader.RowTypes()) > 0, "should have row types")
assertTrueF(t, loader.TotalRows() > 0, "should have total rows")
})
}
func TestQueryArrowStreamMultiStatementForJSONData(t *testing.T) {
runSnowflakeConnTest(t, func(sct *SCTest) {
ctx := WithMultiStatement(ia.EnableArrowBatches(sct.sc.ctx), 2)
loader, err := sct.sc.QueryArrowStream(ctx, "SELECT 'abc'; SELECT 'abc'")
assertNilF(t, err)
assertTrueF(t, loader.TotalRows() > 0, "should return data")
})
}