forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multistatement_test.go
575 lines (518 loc) · 15.1 KB
/
multistatement_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
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Copyright (c) 2020-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"os"
"reflect"
"testing"
"time"
)
func TestMultiStatementExecuteNoResultSet(t *testing.T) {
ctx, _ := WithMultiStatement(context.Background(), 4)
multiStmtQuery := "begin;\n" +
"delete from test_multi_statement_txn;\n" +
"insert into test_multi_statement_txn values (1, 'a'), (2, 'b');\n" +
"commit;"
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec(`create or replace table test_multi_statement_txn(c1 number, c2 string) as select 10, 'z'`)
res := dbt.mustExecContext(ctx, multiStmtQuery)
count, err := res.RowsAffected()
if err != nil {
t.Fatalf("res.RowsAffected() returned error: %v", err)
}
if count != 3 {
t.Fatalf("expected 3 affected rows, got %d", count)
}
})
}
func TestMultiStatementQueryResultSet(t *testing.T) {
ctx, _ := WithMultiStatement(context.Background(), 4)
multiStmtQuery := "select 123;\n" +
"select 456;\n" +
"select 789;\n" +
"select '000';"
var v1, v2, v3 int64
var v4 string
runDBTest(t, func(dbt *DBTest) {
rows := dbt.mustQueryContext(ctx, multiStmtQuery)
defer rows.Close()
// first statement
if rows.Next() {
if err := rows.Scan(&v1); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v1 != 123 {
t.Fatalf("failed to fetch. value: %v", v1)
}
} else {
t.Error("failed to query")
}
// second statement
if !rows.NextResultSet() {
t.Error("failed to retrieve next result set")
}
if rows.Next() {
if err := rows.Scan(&v2); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v2 != 456 {
t.Fatalf("failed to fetch. value: %v", v2)
}
} else {
t.Error("failed to query")
}
// third statement
if !rows.NextResultSet() {
t.Error("failed to retrieve next result set")
}
if rows.Next() {
if err := rows.Scan(&v3); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v3 != 789 {
t.Fatalf("failed to fetch. value: %v", v3)
}
} else {
t.Error("failed to query")
}
// fourth statement
if !rows.NextResultSet() {
t.Error("failed to retrieve next result set")
}
if rows.Next() {
if err := rows.Scan(&v4); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v4 != "000" {
t.Fatalf("failed to fetch. value: %v", v4)
}
} else {
t.Error("failed to query")
}
})
}
func TestMultiStatementExecuteResultSet(t *testing.T) {
ctx, _ := WithMultiStatement(context.Background(), 6)
multiStmtQuery := "begin;\n" +
"delete from test_multi_statement_txn_rb;\n" +
"insert into test_multi_statement_txn_rb values (1, 'a'), (2, 'b');\n" +
"select 1;\n" +
"select 2;\n" +
"rollback;"
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec("drop table if exists test_multi_statement_txn_rb")
dbt.mustExec(`create or replace table test_multi_statement_txn_rb(
c1 number, c2 string) as select 10, 'z'`)
defer dbt.mustExec("drop table if exists test_multi_statement_txn_rb")
res := dbt.mustExecContext(ctx, multiStmtQuery)
count, err := res.RowsAffected()
if err != nil {
t.Fatalf("res.RowsAffected() returned error: %v", err)
}
if count != 3 {
t.Fatalf("expected 3 affected rows, got %d", count)
}
})
}
func TestMultiStatementQueryNoResultSet(t *testing.T) {
ctx, _ := WithMultiStatement(context.Background(), 4)
multiStmtQuery := "begin;\n" +
"delete from test_multi_statement_txn;\n" +
"insert into test_multi_statement_txn values (1, 'a'), (2, 'b');\n" +
"commit;"
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec("drop table if exists test_multi_statement_txn")
dbt.mustExec(`create or replace table test_multi_statement_txn(
c1 number, c2 string) as select 10, 'z'`)
defer dbt.mustExec("drop table if exists tfmuest_multi_statement_txn")
rows := dbt.mustQueryContext(ctx, multiStmtQuery)
defer rows.Close()
})
}
func TestMultiStatementExecuteMix(t *testing.T) {
ctx, _ := WithMultiStatement(context.Background(), 3)
multiStmtQuery := "create or replace temporary table test_multi (cola int);\n" +
"insert into test_multi values (1), (2);\n" +
"select cola from test_multi order by cola asc;"
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec("drop table if exists test_multi_statement_txn")
dbt.mustExec(`create or replace table test_multi_statement_txn(
c1 number, c2 string) as select 10, 'z'`)
defer dbt.mustExec("drop table if exists test_multi_statement_txn")
res := dbt.mustExecContext(ctx, multiStmtQuery)
count, err := res.RowsAffected()
if err != nil {
t.Fatalf("res.RowsAffected() returned error: %v", err)
}
if count != 2 {
t.Fatalf("expected 2 affected rows, got %d", count)
}
})
}
func TestMultiStatementQueryMix(t *testing.T) {
ctx, _ := WithMultiStatement(context.Background(), 3)
multiStmtQuery := "create or replace temporary table test_multi (cola int);\n" +
"insert into test_multi values (1), (2);\n" +
"select cola from test_multi order by cola asc;"
var count, v int
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec("drop table if exists test_multi_statement_txn")
dbt.mustExec(`create or replace table test_multi_statement_txn(
c1 number, c2 string) as select 10, 'z'`)
defer dbt.mustExec("drop table if exists test_multi_statement_txn")
rows := dbt.mustQueryContext(ctx, multiStmtQuery)
defer rows.Close()
// first statement
if !rows.Next() {
t.Error("failed to query")
}
// second statement
rows.NextResultSet()
if rows.Next() {
if err := rows.Scan(&count); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if count != 2 {
t.Fatalf("expected 2 affected rows, got %d", count)
}
}
expected := 1
// third statement
rows.NextResultSet()
for rows.Next() {
if err := rows.Scan(&v); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v != expected {
t.Fatalf("failed to fetch. value: %v", v)
}
expected++
}
})
}
func TestMultiStatementCountZero(t *testing.T) {
ctx, _ := WithMultiStatement(context.Background(), 0)
var v1 int
var v2 string
var v3 float64
var v4 bool
runDBTest(t, func(dbt *DBTest) {
// first query
multiStmtQuery1 := "select 123;\n" +
"select '456';"
rows1 := dbt.mustQueryContext(ctx, multiStmtQuery1)
defer rows1.Close()
// first statement
if rows1.Next() {
if err := rows1.Scan(&v1); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v1 != 123 {
t.Fatalf("failed to fetch. value: %v", v1)
}
} else {
t.Error("failed to query")
}
// second statement
if !rows1.NextResultSet() {
t.Error("failed to retrieve next result set")
}
if rows1.Next() {
if err := rows1.Scan(&v2); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v2 != "456" {
t.Fatalf("failed to fetch. value: %v", v2)
}
} else {
t.Error("failed to query")
}
// second query
multiStmtQuery2 := "select 789;\n" +
"select 'foo';\n" +
"select 0.123;\n" +
"select true;"
rows2 := dbt.mustQueryContext(ctx, multiStmtQuery2)
defer rows2.Close()
// first statement
if rows2.Next() {
if err := rows2.Scan(&v1); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v1 != 789 {
t.Fatalf("failed to fetch. value: %v", v1)
}
} else {
t.Error("failed to query")
}
// second statement
if !rows2.NextResultSet() {
t.Error("failed to retrieve next result set")
}
if rows2.Next() {
if err := rows2.Scan(&v2); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v2 != "foo" {
t.Fatalf("failed to fetch. value: %v", v2)
}
} else {
t.Error("failed to query")
}
// third statement
if !rows2.NextResultSet() {
t.Error("failed to retrieve next result set")
}
if rows2.Next() {
if err := rows2.Scan(&v3); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v3 != 0.123 {
t.Fatalf("failed to fetch. value: %v", v3)
}
} else {
t.Error("failed to query")
}
// fourth statement
if !rows2.NextResultSet() {
t.Error("failed to retrieve next result set")
}
if rows2.Next() {
if err := rows2.Scan(&v4); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v4 != true {
t.Fatalf("failed to fetch. value: %v", v4)
}
} else {
t.Error("failed to query")
}
})
}
func TestMultiStatementCountMismatch(t *testing.T) {
conn := openConn(t)
defer conn.Close()
multiStmtQuery := "select 123;\n" +
"select 456;\n" +
"select 789;\n" +
"select '000';"
ctx, _ := WithMultiStatement(context.Background(), 3)
if _, err := conn.QueryContext(ctx, multiStmtQuery); err == nil {
t.Fatal("should have failed to query multiple statements")
}
}
func TestMultiStatementVaryingColumnCount(t *testing.T) {
multiStmtQuery := "select c1 from test_tbl;\n" +
"select c1,c2 from test_tbl;"
ctx, _ := WithMultiStatement(context.Background(), 0)
var v1, v2 int
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec("create or replace table test_tbl(c1 int, c2 int)")
dbt.mustExec("insert into test_tbl values(1, 0)")
defer dbt.mustExec("drop table if exists test_tbl")
rows := dbt.mustQueryContext(ctx, multiStmtQuery)
defer rows.Close()
if rows.Next() {
if err := rows.Scan(&v1); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v1 != 1 {
t.Fatalf("failed to fetch. value: %v", v1)
}
} else {
t.Error("failed to query")
}
if !rows.NextResultSet() {
t.Error("failed to retrieve next result set")
}
if rows.Next() {
if err := rows.Scan(&v1, &v2); err != nil {
t.Errorf("failed to scan: %#v", err)
}
if v1 != 1 || v2 != 0 {
t.Fatalf("failed to fetch. value: %v, %v", v1, v2)
}
} else {
t.Error("failed to query")
}
})
}
// The total completion time should be similar to the duration of the query on Snowflake UI.
func TestMultiStatementExecutePerformance(t *testing.T) {
ctx, _ := WithMultiStatement(context.Background(), 100)
runDBTest(t, func(dbt *DBTest) {
file, err := os.Open("test_data/multistatements.sql")
if err != nil {
t.Fatalf("failed opening file: %s", err)
}
defer file.Close()
statements, err := io.ReadAll(file)
if err != nil {
t.Fatalf("failed reading file: %s", err)
}
sql := string(statements)
start := time.Now()
res := dbt.mustExecContext(ctx, sql)
duration := time.Since(start)
count, err := res.RowsAffected()
if err != nil {
t.Fatalf("res.RowsAffected() returned error: %v", err)
}
if count != 0 {
t.Fatalf("expected 0 affected rows, got %d", count)
}
t.Logf("The total completion time was %v", duration)
file, err = os.Open("test_data/multistatements_drop.sql")
if err != nil {
t.Fatalf("failed opening file: %s", err)
}
defer file.Close()
statements, err = io.ReadAll(file)
if err != nil {
t.Fatalf("failed reading file: %s", err)
}
sql = string(statements)
dbt.mustExecContext(ctx, sql)
})
}
func TestUnitGetChildResults(t *testing.T) {
testcases := []struct {
ids string
types string
out []childResult
}{
{"", "", nil},
{"", "4096", nil},
{"01aa3265-0405-ab7c-0000-53b106343aba,02aa3265-0405-ab7c-0000-53b106343aba", "12544,12544", []childResult{
{"01aa3265-0405-ab7c-0000-53b106343aba", "12544"},
{"02aa3265-0405-ab7c-0000-53b106343aba", "12544"}}},
{"01aa3265-0405-ab7c-0000-53b106343aba,02aa3265-0405-ab7c-0000-53b106343aba,03aa3265-0405-ab7c-0000-53b106343aba", "25344,4096,12544", []childResult{
{"01aa3265-0405-ab7c-0000-53b106343aba", "25344"},
{"02aa3265-0405-ab7c-0000-53b106343aba", "4096"},
{"03aa3265-0405-ab7c-0000-53b106343aba", "12544"}}},
}
for _, test := range testcases {
t.Run(test.ids, func(t *testing.T) {
res := getChildResults(test.ids, test.types)
if !reflect.DeepEqual(res, test.out) {
t.Fatalf("Child result should be equal, expected %v, actual %v", res, test.out)
}
})
}
}
func funcGetQueryRespFail(_ context.Context, _ *snowflakeRestful, _ *url.URL, _ map[string]string, _ time.Duration) (*http.Response, error) {
return nil, errors.New("failed to get query response")
}
func funcGetQueryRespError(_ context.Context, _ *snowflakeRestful, _ *url.URL, _ map[string]string, _ time.Duration) (*http.Response, error) {
dd := &execResponseData{}
er := &execResponse{
Data: *dd,
Message: "query failed",
Code: "261000",
Success: false,
}
ba, err := json.Marshal(er)
if err != nil {
panic(err)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: &fakeResponseBody{body: ba},
}, nil
}
func TestUnitHandleMultiExec(t *testing.T) {
runSnowflakeConnTest(t, func(sct *SCTest) {
data := execResponseData{
ResultIDs: "",
ResultTypes: "",
}
_, err := sct.sc.handleMultiExec(context.Background(), data)
if err == nil {
t.Fatalf("should have failed")
}
driverErr, ok := err.(*SnowflakeError)
if !ok {
t.Fatalf("should be snowflake error. err: %v", err)
}
if driverErr.Number != ErrNoResultIDs {
t.Fatalf("unexpected error code. expected: %v, got: %v", ErrNoResultIDs, driverErr.Number)
}
data = execResponseData{
ResultIDs: "1eFhmhe23242kmfd540GgGre,1eFhmhe23242kmfd540GgGre",
ResultTypes: "12544,12544",
}
sct.sc.rest = &snowflakeRestful{
FuncGet: funcGetQueryRespFail,
FuncCloseSession: closeSessionMock,
TokenAccessor: getSimpleTokenAccessor(),
}
_, err = sct.sc.handleMultiExec(context.Background(), data)
if err == nil {
t.Fatalf("should have failed")
}
sct.sc.rest.FuncGet = funcGetQueryRespError
data.SQLState = "01112"
_, err = sct.sc.handleMultiExec(context.Background(), data)
if err == nil {
t.Fatalf("should have failed")
}
driverErr, ok = err.(*SnowflakeError)
if !ok {
t.Fatalf("should be snowflake error. err: %v", err)
}
if driverErr.Number != ErrFailedToPostQuery {
t.Fatalf("unexpected error code. expected: %v, got: %v", ErrFailedToPostQuery, driverErr.Number)
}
})
}
func TestUnitHandleMultiQuery(t *testing.T) {
runSnowflakeConnTest(t, func(sct *SCTest) {
data := execResponseData{
ResultIDs: "",
ResultTypes: "",
}
rows := new(snowflakeRows)
err := sct.sc.handleMultiQuery(context.Background(), data, rows)
if err == nil {
t.Fatalf("should have failed")
}
driverErr, ok := err.(*SnowflakeError)
if !ok {
t.Fatalf("should be snowflake error. err: %v", err)
}
if driverErr.Number != ErrNoResultIDs {
t.Fatalf("unexpected error code. expected: %v, got: %v", ErrNoResultIDs, driverErr.Number)
}
data = execResponseData{
ResultIDs: "1eFhmhe23242kmfd540GgGre,1eFhmhe23242kmfd540GgGre",
ResultTypes: "12544,12544",
}
sct.sc.rest = &snowflakeRestful{
FuncGet: funcGetQueryRespFail,
FuncCloseSession: closeSessionMock,
TokenAccessor: getSimpleTokenAccessor(),
}
err = sct.sc.handleMultiQuery(context.Background(), data, rows)
if err == nil {
t.Fatalf("should have failed")
}
sct.sc.rest.FuncGet = funcGetQueryRespError
data.SQLState = "01112"
err = sct.sc.handleMultiQuery(context.Background(), data, rows)
if err == nil {
t.Fatalf("should have failed")
}
driverErr, ok = err.(*SnowflakeError)
if !ok {
t.Fatalf("should be snowflake error. err: %v", err)
}
if driverErr.Number != ErrFailedToPostQuery {
t.Fatalf("unexpected error code. expected: %v, got: %v", ErrFailedToPostQuery, driverErr.Number)
}
})
}