-
Notifications
You must be signed in to change notification settings - Fork 125
/
bindings_test.go
1355 lines (1256 loc) · 44.3 KB
/
bindings_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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2021-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"bytes"
"context"
"database/sql"
"fmt"
"log"
"math/big"
"math/rand"
"reflect"
"strconv"
"strings"
"testing"
"time"
)
const (
createTableSQL = `create or replace table test_prep_statement(c1 INTEGER,
c2 FLOAT, c3 BOOLEAN, c4 STRING, C5 BINARY, C6 TIMESTAMP_NTZ,
C7 TIMESTAMP_LTZ, C8 TIMESTAMP_TZ, C9 DATE, C10 TIME)`
deleteTableSQL = "drop table if exists TEST_PREP_STATEMENT"
insertSQL = "insert into TEST_PREP_STATEMENT values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
selectAllSQL = "select * from TEST_PREP_STATEMENT ORDER BY 1"
createTableSQLBulkArray = `create or replace table test_bulk_array(c1 INTEGER,
c2 FLOAT, c3 BOOLEAN, c4 STRING, C5 BINARY, C6 INTEGER)`
deleteTableSQLBulkArray = "drop table if exists test_bulk_array"
insertSQLBulkArray = "insert into test_bulk_array values(?, ?, ?, ?, ?, ?)"
selectAllSQLBulkArray = "select * from test_bulk_array ORDER BY 1"
createTableSQLBulkArrayDateTimeTimestamp = `create or replace table test_bulk_array_DateTimeTimestamp(
C1 TIMESTAMP_NTZ, C2 TIMESTAMP_LTZ, C3 TIMESTAMP_TZ, C4 DATE, C5 TIME)`
deleteTableSQLBulkArrayDateTimeTimestamp = "drop table if exists test_bulk_array_DateTimeTimestamp"
insertSQLBulkArrayDateTimeTimestamp = "insert into test_bulk_array_DateTimeTimestamp values(?, ?, ?, ?, ?)"
selectAllSQLBulkArrayDateTimeTimestamp = "select * from test_bulk_array_DateTimeTimestamp ORDER BY 1"
enableFeatureMaxLOBSize = "ALTER SESSION SET FEATURE_INCREASED_MAX_LOB_SIZE_IN_MEMORY='ENABLED'"
unsetFeatureMaxLOBSize = "ALTER SESSION UNSET FEATURE_INCREASED_MAX_LOB_SIZE_IN_MEMORY"
enableLargeVarcharAndBinary = "ALTER SESSION SET ENABLE_LARGE_VARCHAR_AND_BINARY_IN_RESULT=TRUE"
disableLargeVarcharAndBinary = "ALTER SESSION SET ENABLE_LARGE_VARCHAR_AND_BINARY_IN_RESULT=FALSE"
unsetLargeVarcharAndBinary = "ALTER SESSION UNSET ENABLE_LARGE_VARCHAR_AND_BINARY_IN_RESULT"
maxVarcharAndBinarySizeParam = "varchar_and_binary_max_size_in_result"
originSize = 16 * 1024 * 1024
smallSize = 16
// range to use for generating random numbers
lobRandomRange = 100000
)
var (
// maxLOBSize = 128 * 1024 * 1024 // new max LOB size
maxLOBSize = 16 * 1024 * 1024 // current max LOB size
largeSize = maxLOBSize / 2
mediumSize = largeSize / 2
)
func TestBindingFloat64(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
types := [2]string{"FLOAT", "DOUBLE"}
expected := 42.23
var out float64
var rows *RowsExtended
for _, v := range types {
t.Run(v, func(t *testing.T) {
dbt.mustExec(fmt.Sprintf("CREATE OR REPLACE TABLE test (id int, value %v)", v))
dbt.mustExec("INSERT INTO test VALUES (1, ?)", expected)
rows = dbt.mustQuery("SELECT value FROM test WHERE id = ?", 1)
defer func() {
assertNilF(t, rows.Close())
}()
if rows.Next() {
assertNilF(t, rows.Scan(&out))
if expected != out {
dbt.Errorf("%s: %g != %g", v, expected, out)
}
} else {
dbt.Errorf("%s: no data", v)
}
})
}
dbt.mustExec("DROP TABLE IF EXISTS test")
})
}
// TestBindingUint64 tests uint64 binding. Should fail as unit64 is not a
// supported binding value by Go's sql package.
func TestBindingUint64(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
expected := uint64(18446744073709551615)
dbt.mustExec("CREATE OR REPLACE TABLE test (id int, value INTEGER)")
if _, err := dbt.exec("INSERT INTO test VALUES (1, ?)", expected); err == nil {
dbt.Fatal("should fail as uint64 values with high bit set are not supported.")
} else {
logger.Infof("expected err: %v", err)
}
dbt.mustExec("DROP TABLE IF EXISTS test")
})
}
func TestBindingDateTimeTimestamp(t *testing.T) {
createDSN(PSTLocation)
runDBTest(t, func(dbt *DBTest) {
expected := time.Now()
dbt.mustExec(
"CREATE OR REPLACE TABLE tztest (id int, ntz timestamp_ntz, ltz timestamp_ltz, dt date, tm time)")
stmt, err := dbt.prepare("INSERT INTO tztest(id,ntz,ltz,dt,tm) VALUES(1,?,?,?,?)")
if err != nil {
dbt.Fatal(err.Error())
}
defer stmt.Close()
if _, err = stmt.Exec(
DataTypeTimestampNtz, expected,
DataTypeTimestampLtz, expected,
DataTypeDate, expected,
DataTypeTime, expected); err != nil {
dbt.Fatal(err)
}
rows := dbt.mustQuery("SELECT ntz,ltz,dt,tm FROM tztest WHERE id=?", 1)
defer rows.Close()
var ntz, vltz, dt, tm time.Time
columnTypes, err := rows.ColumnTypes()
if err != nil {
dbt.Errorf("column type error. err: %v", err)
}
if columnTypes[0].Name() != "NTZ" {
dbt.Errorf("expected column name: %v, got: %v", "TEST", columnTypes[0])
}
canNull := dbt.mustNullable(columnTypes[0])
if !canNull {
dbt.Errorf("expected nullable: %v, got: %v", true, canNull)
}
if columnTypes[0].DatabaseTypeName() != "TIMESTAMP_NTZ" {
dbt.Errorf("expected database type: %v, got: %v", "TIMESTAMP_NTZ", columnTypes[0].DatabaseTypeName())
}
dbt.mustFailDecimalSize(columnTypes[0])
dbt.mustFailLength(columnTypes[0])
cols, err := rows.Columns()
if err != nil {
dbt.Errorf("failed to get columns. err: %v", err)
}
if len(cols) != 4 || cols[0] != "NTZ" || cols[1] != "LTZ" || cols[2] != "DT" || cols[3] != "TM" {
dbt.Errorf("failed to get columns. got: %v", cols)
}
if rows.Next() {
assertNilF(t, rows.Scan(&ntz, &vltz, &dt, &tm))
if expected.UnixNano() != ntz.UnixNano() {
dbt.Errorf("returned TIMESTAMP_NTZ value didn't match. expected: %v:%v, got: %v:%v",
expected.UnixNano(), expected, ntz.UnixNano(), ntz)
}
if expected.UnixNano() != vltz.UnixNano() {
dbt.Errorf("returned TIMESTAMP_LTZ value didn't match. expected: %v:%v, got: %v:%v",
expected.UnixNano(), expected, vltz.UnixNano(), vltz)
}
if expected.Year() != dt.Year() || expected.Month() != dt.Month() || expected.Day() != dt.Day() {
dbt.Errorf("returned DATE value didn't match. expected: %v:%v, got: %v:%v",
expected.Unix()*1000, expected, dt.Unix()*1000, dt)
}
if expected.Hour() != tm.Hour() || expected.Minute() != tm.Minute() || expected.Second() != tm.Second() || expected.Nanosecond() != tm.Nanosecond() {
dbt.Errorf("returned TIME value didn't match. expected: %v:%v, got: %v:%v",
expected.UnixNano(), expected, tm.UnixNano(), tm)
}
} else {
dbt.Error("no data")
}
dbt.mustExec("DROP TABLE tztest")
})
createDSN("UTC")
}
func TestBindingBinary(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec("CREATE OR REPLACE TABLE bintest (id int, b binary)")
var b = []byte{0x01, 0x02, 0x03}
dbt.mustExec("INSERT INTO bintest(id,b) VALUES(1, ?)", DataTypeBinary, b)
rows := dbt.mustQuery("SELECT b FROM bintest WHERE id=?", 1)
defer rows.Close()
if rows.Next() {
var rb []byte
if err := rows.Scan(&rb); err != nil {
dbt.Errorf("failed to scan data. err: %v", err)
}
if !bytes.Equal(b, rb) {
dbt.Errorf("failed to match data. expected: %v, got: %v", b, rb)
}
} else {
dbt.Errorf("no data")
}
dbt.mustExec("DROP TABLE bintest")
})
}
func TestBindingTimestampTZ(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
expected := time.Now()
dbt.mustExec("CREATE OR REPLACE TABLE tztest (id int, tz timestamp_tz)")
stmt, err := dbt.prepare("INSERT INTO tztest(id,tz) VALUES(1, ?)")
if err != nil {
dbt.Fatal(err.Error())
}
defer func() {
assertNilF(t, stmt.Close())
}()
if _, err = stmt.Exec(DataTypeTimestampTz, expected); err != nil {
dbt.Fatal(err)
}
rows := dbt.mustQuery("SELECT tz FROM tztest WHERE id=?", 1)
defer func() {
assertNilF(t, rows.Close())
}()
var v time.Time
if rows.Next() {
assertNilF(t, rows.Scan(&v))
if expected.UnixNano() != v.UnixNano() {
dbt.Errorf("returned value didn't match. expected: %v:%v, got: %v:%v",
expected.UnixNano(), expected, v.UnixNano(), v)
}
} else {
dbt.Error("no data")
}
dbt.mustExec("DROP TABLE tztest")
})
}
// SNOW-755844: Test the use of a pointer *time.Time type in user-defined structures to perform updates/inserts
func TestBindingTimePtrInStruct(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
type timePtrStruct struct {
id *int
timeVal *time.Time
}
expectedID := 1
expectedTime := time.Now()
testStruct := timePtrStruct{id: &expectedID, timeVal: &expectedTime}
dbt.mustExec("CREATE OR REPLACE TABLE timeStructTest (id int, tz timestamp_tz)")
runInsertQuery := false
for i := 0; i < 2; i++ {
if !runInsertQuery {
_, err := dbt.exec("INSERT INTO timeStructTest(id,tz) VALUES(?, ?)", testStruct.id, testStruct.timeVal)
if err != nil {
dbt.Fatal(err.Error())
}
runInsertQuery = true
} else {
// Update row with a new time value
expectedTime = time.Now().Add(1)
testStruct.timeVal = &expectedTime
_, err := dbt.exec("UPDATE timeStructTest SET tz = ? where id = ?", testStruct.timeVal, testStruct.id)
if err != nil {
dbt.Fatal(err.Error())
}
}
rows := dbt.mustQuery("SELECT tz FROM timeStructTest WHERE id=?", &expectedID)
defer func() {
assertNilF(t, rows.Close())
}()
var v time.Time
if rows.Next() {
assertNilF(t, rows.Scan(&v))
if expectedTime.UnixNano() != v.UnixNano() {
dbt.Errorf("returned value didn't match. expected: %v:%v, got: %v:%v",
expectedTime.UnixNano(), expectedTime, v.UnixNano(), v)
}
} else {
dbt.Error("no data")
}
}
dbt.mustExec("DROP TABLE timeStructTest")
})
}
// SNOW-755844: Test the use of a time.Time type in user-defined structures to perform updates/inserts
func TestBindingTimeInStruct(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
type timeStruct struct {
id int
timeVal time.Time
}
expectedID := 1
expectedTime := time.Now()
testStruct := timeStruct{id: expectedID, timeVal: expectedTime}
dbt.mustExec("CREATE OR REPLACE TABLE timeStructTest (id int, tz timestamp_tz)")
runInsertQuery := false
for i := 0; i < 2; i++ {
if !runInsertQuery {
_, err := dbt.exec("INSERT INTO timeStructTest(id,tz) VALUES(?, ?)", testStruct.id, testStruct.timeVal)
if err != nil {
dbt.Fatal(err.Error())
}
runInsertQuery = true
} else {
// Update row with a new time value
expectedTime = time.Now().Add(1)
testStruct.timeVal = expectedTime
_, err := dbt.exec("UPDATE timeStructTest SET tz = ? where id = ?", testStruct.timeVal, testStruct.id)
if err != nil {
dbt.Fatal(err.Error())
}
}
rows := dbt.mustQuery("SELECT tz FROM timeStructTest WHERE id=?", &expectedID)
defer func() {
assertNilF(t, rows.Close())
}()
var v time.Time
if rows.Next() {
assertNilF(t, rows.Scan(&v))
if expectedTime.UnixNano() != v.UnixNano() {
dbt.Errorf("returned value didn't match. expected: %v:%v, got: %v:%v",
expectedTime.UnixNano(), expectedTime, v.UnixNano(), v)
}
} else {
dbt.Error("no data")
}
}
dbt.mustExec("DROP TABLE timeStructTest")
})
}
func TestBindingInterface(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
rows := dbt.mustQueryContext(
WithHigherPrecision(context.Background()), selectVariousTypes)
defer func() {
assertNilF(t, rows.Close())
}()
if !rows.Next() {
dbt.Error("failed to query")
}
var v1, v2, v3, v4, v5, v6 any
if err := rows.Scan(&v1, &v2, &v3, &v4, &v5, &v6); err != nil {
dbt.Errorf("failed to scan: %#v", err)
}
if s1, ok := v1.(*big.Float); !ok || s1.Cmp(big.NewFloat(1.0)) != 0 {
dbt.Fatalf("failed to fetch. ok: %v, value: %v", ok, v1)
}
if s2, ok := v2.(int64); !ok || s2 != 2 {
dbt.Fatalf("failed to fetch. ok: %v, value: %v", ok, v2)
}
if s3, ok := v3.(string); !ok || s3 != "t3" {
dbt.Fatalf("failed to fetch. ok: %v, value: %v", ok, v3)
}
if s4, ok := v4.(float64); !ok || s4 != 4.2 {
dbt.Fatalf("failed to fetch. ok: %v, value: %v", ok, v4)
}
})
}
func TestBindingInterfaceString(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
rows := dbt.mustQuery(selectVariousTypes)
defer func() {
assertNilF(t, rows.Close())
}()
if !rows.Next() {
dbt.Error("failed to query")
}
var v1, v2, v3, v4, v5, v6 any
if err := rows.Scan(&v1, &v2, &v3, &v4, &v5, &v6); err != nil {
dbt.Errorf("failed to scan: %#v", err)
}
if s, ok := v1.(string); !ok {
dbt.Error("failed to convert to string")
} else if d, err := strconv.ParseFloat(s, 64); err != nil {
dbt.Errorf("failed to convert to float. value: %v, err: %v", v1, err)
} else if d != 1.00 {
dbt.Errorf("failed to fetch. expected: 1.00, value: %v", v1)
}
if s, ok := v2.(string); !ok || s != "2" {
dbt.Fatalf("failed to fetch. ok: %v, value: %v", ok, v2)
}
if s, ok := v3.(string); !ok || s != "t3" {
dbt.Fatalf("failed to fetch. ok: %v, value: %v", ok, v3)
}
})
}
func TestBulkArrayBindingInterfaceNil(t *testing.T) {
nilArray := make([]any, 1)
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec(createTableSQL)
defer dbt.mustExec(deleteTableSQL)
dbt.mustExec(insertSQL, Array(&nilArray), Array(&nilArray),
Array(&nilArray), Array(&nilArray), Array(&nilArray),
Array(&nilArray, TimestampNTZType), Array(&nilArray, TimestampTZType),
Array(&nilArray, TimestampTZType), Array(&nilArray, DateType),
Array(&nilArray, TimeType))
rows := dbt.mustQuery(selectAllSQL)
defer func() {
assertNilF(t, rows.Close())
}()
var v0 sql.NullInt32
var v1 sql.NullFloat64
var v2 sql.NullBool
var v3 sql.NullString
var v4 []byte
var v5, v6, v7, v8, v9 sql.NullTime
cnt := 0
for i := 0; rows.Next(); i++ {
if err := rows.Scan(&v0, &v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, &v9); err != nil {
t.Fatal(err)
}
if v0.Valid {
t.Fatalf("failed to fetch the sql.NullInt32 column v0. expected %v, got: %v", nilArray[i], v0)
}
if v1.Valid {
t.Fatalf("failed to fetch the sql.NullFloat64 column v1. expected %v, got: %v", nilArray[i], v1)
}
if v2.Valid {
t.Fatalf("failed to fetch the sql.NullBool column v2. expected %v, got: %v", nilArray[i], v2)
}
if v3.Valid {
t.Fatalf("failed to fetch the sql.NullString column v3. expected %v, got: %v", nilArray[i], v3)
}
if v4 != nil {
t.Fatalf("failed to fetch the []byte column v4. expected %v, got: %v", nilArray[i], v4)
}
if v5.Valid {
t.Fatalf("failed to fetch the sql.NullTime column v5. expected %v, got: %v", nilArray[i], v5)
}
if v6.Valid {
t.Fatalf("failed to fetch the sql.NullTime column v6. expected %v, got: %v", nilArray[i], v6)
}
if v7.Valid {
t.Fatalf("failed to fetch the sql.NullTime column v7. expected %v, got: %v", nilArray[i], v7)
}
if v8.Valid {
t.Fatalf("failed to fetch the sql.NullTime column v8. expected %v, got: %v", nilArray[i], v8)
}
if v9.Valid {
t.Fatalf("failed to fetch the sql.NullTime column v9. expected %v, got: %v", nilArray[i], v9)
}
cnt++
}
if cnt != len(nilArray) {
t.Fatal("failed to query")
}
})
}
func TestBulkArrayBindingInterface(t *testing.T) {
intArray := make([]any, 3)
intArray[0] = int32(100)
intArray[1] = int32(200)
fltArray := make([]any, 3)
fltArray[0] = float64(0.1)
fltArray[2] = float64(5.678)
boolArray := make([]any, 3)
boolArray[1] = false
boolArray[2] = true
strArray := make([]any, 3)
strArray[2] = "test3"
byteArray := make([]any, 3)
byteArray[0] = []byte{0x01, 0x02, 0x03}
byteArray[2] = []byte{0x07, 0x08, 0x09}
int64Array := make([]any, 3)
int64Array[0] = int64(100)
int64Array[1] = int64(200)
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec(createTableSQLBulkArray)
defer dbt.mustExec(deleteTableSQLBulkArray)
dbt.mustExec(insertSQLBulkArray, Array(&intArray), Array(&fltArray),
Array(&boolArray), Array(&strArray), Array(&byteArray), Array(&int64Array))
rows := dbt.mustQuery(selectAllSQLBulkArray)
defer func() {
assertNilF(t, rows.Close())
}()
var v0 sql.NullInt32
var v1 sql.NullFloat64
var v2 sql.NullBool
var v3 sql.NullString
var v4 []byte
var v5 sql.NullInt64
cnt := 0
for i := 0; rows.Next(); i++ {
if err := rows.Scan(&v0, &v1, &v2, &v3, &v4, &v5); err != nil {
t.Fatal(err)
}
if v0.Valid {
if v0.Int32 != intArray[i] {
t.Fatalf("failed to fetch the sql.NullInt32 column v0. expected %v, got: %v", intArray[i], v0.Int32)
}
} else if intArray[i] != nil {
t.Fatalf("failed to fetch the sql.NullInt32 column v0. expected %v, got: %v", intArray[i], v0)
}
if v1.Valid {
if v1.Float64 != fltArray[i] {
t.Fatalf("failed to fetch the sql.NullFloat64 column v1. expected %v, got: %v", fltArray[i], v1.Float64)
}
} else if fltArray[i] != nil {
t.Fatalf("failed to fetch the sql.NullFloat64 column v1. expected %v, got: %v", fltArray[i], v1)
}
if v2.Valid {
if v2.Bool != boolArray[i] {
t.Fatalf("failed to fetch the sql.NullBool column v2. expected %v, got: %v", boolArray[i], v2.Bool)
}
} else if boolArray[i] != nil {
t.Fatalf("failed to fetch the sql.NullBool column v2. expected %v, got: %v", boolArray[i], v2)
}
if v3.Valid {
if v3.String != strArray[i] {
t.Fatalf("failed to fetch the sql.NullString column v3. expected %v, got: %v", strArray[i], v3.String)
}
} else if strArray[i] != nil {
t.Fatalf("failed to fetch the sql.NullString column v3. expected %v, got: %v", strArray[i], v3)
}
if byteArray[i] != nil {
if !bytes.Equal(v4, byteArray[i].([]byte)) {
t.Fatalf("failed to fetch the []byte column v4. expected %v, got: %v", byteArray[i], v4)
}
} else if v4 != nil {
t.Fatalf("failed to fetch the []byte column v4. expected %v, got: %v", byteArray[i], v4)
}
if v5.Valid {
if v5.Int64 != int64Array[i] {
t.Fatalf("failed to fetch the sql.NullInt64 column v5. expected %v, got: %v", int64Array[i], v5.Int64)
}
} else if int64Array[i] != nil {
t.Fatalf("failed to fetch the sql.NullInt64 column v5. expected %v, got: %v", int64Array[i], v5)
}
cnt++
}
if cnt != len(intArray) {
t.Fatal("failed to query")
}
})
}
func TestBulkArrayBindingInterfaceDateTimeTimestamp(t *testing.T) {
tz := time.Now()
createDSN(PSTLocation)
now := time.Now()
loc, err := time.LoadLocation(PSTLocation)
if err != nil {
t.Error(err)
}
ntzArray := make([]any, 3)
ntzArray[0] = now
ntzArray[1] = now.Add(1)
ltzArray := make([]any, 3)
ltzArray[1] = now.Add(2).In(loc)
ltzArray[2] = now.Add(3).In(loc)
tzArray := make([]any, 3)
tzArray[0] = tz.Add(4).In(loc)
tzArray[2] = tz.Add(5).In(loc)
dtArray := make([]any, 3)
dtArray[0] = tz.Add(6).In(loc)
dtArray[1] = now.Add(7).In(loc)
tmArray := make([]any, 3)
tmArray[1] = now.Add(8).In(loc)
tmArray[2] = now.Add(9).In(loc)
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec(createTableSQLBulkArrayDateTimeTimestamp)
defer dbt.mustExec(deleteTableSQLBulkArrayDateTimeTimestamp)
dbt.mustExec(insertSQLBulkArrayDateTimeTimestamp,
Array(&ntzArray, TimestampNTZType), Array(<zArray, TimestampLTZType),
Array(&tzArray, TimestampTZType), Array(&dtArray, DateType),
Array(&tmArray, TimeType))
rows := dbt.mustQuery(selectAllSQLBulkArrayDateTimeTimestamp)
defer func() {
assertNilF(t, rows.Close())
}()
var v0, v1, v2, v3, v4 sql.NullTime
cnt := 0
for i := 0; rows.Next(); i++ {
if err := rows.Scan(&v0, &v1, &v2, &v3, &v4); err != nil {
t.Fatal(err)
}
if v0.Valid {
if v0.Time.UnixNano() != ntzArray[i].(time.Time).UnixNano() {
t.Fatalf("failed to fetch the column v0. expected %v, got: %v", ntzArray[i], v0)
}
} else if ntzArray[i] != nil {
t.Fatalf("failed to fetch the column v0. expected %v, got: %v", ntzArray[i], v0)
}
if v1.Valid {
if v1.Time.UnixNano() != ltzArray[i].(time.Time).UnixNano() {
t.Fatalf("failed to fetch the column v1. expected %v, got: %v", ltzArray[i], v1)
}
} else if ltzArray[i] != nil {
t.Fatalf("failed to fetch the column v1. expected %v, got: %v", ltzArray[i], v1)
}
if v2.Valid {
if v2.Time.UnixNano() != tzArray[i].(time.Time).UnixNano() {
t.Fatalf("failed to fetch the column v2. expected %v, got: %v", tzArray[i], v2)
}
} else if tzArray[i] != nil {
t.Fatalf("failed to fetch the column v2. expected %v, got: %v", tzArray[i], v2)
}
if v3.Valid {
if v3.Time.Year() != dtArray[i].(time.Time).Year() ||
v3.Time.Month() != dtArray[i].(time.Time).Month() ||
v3.Time.Day() != dtArray[i].(time.Time).Day() {
t.Fatalf("failed to fetch the column v3. expected %v, got: %v", dtArray[i], v3)
}
} else if dtArray[i] != nil {
t.Fatalf("failed to fetch the column v3. expected %v, got: %v", dtArray[i], v3)
}
if v4.Valid {
if v4.Time.Hour() != tmArray[i].(time.Time).Hour() ||
v4.Time.Minute() != tmArray[i].(time.Time).Minute() ||
v4.Time.Second() != tmArray[i].(time.Time).Second() {
t.Fatalf("failed to fetch the column v4. expected %v, got: %v", tmArray[i], v4)
}
} else if tmArray[i] != nil {
t.Fatalf("failed to fetch the column v4. expected %v, got: %v", tmArray[i], v4)
}
cnt++
}
if cnt != len(ntzArray) {
t.Fatal("failed to query")
}
})
createDSN("UTC")
}
// TestBindingArray tests basic array binding via the usage of the Array
// function that converts the passed Golang slice to a Snowflake array type
func TestBindingArray(t *testing.T) {
testBindingArray(t, false)
}
// TestBindingBulkArray tests bulk array binding via the usage of the Array
// function that converts the passed Golang slice to a Snowflake array type
func TestBindingBulkArray(t *testing.T) {
if runningOnGithubAction() {
t.Skip("client_stage_array_binding_threshold value is internal")
}
testBindingArray(t, true)
}
func testBindingArray(t *testing.T, bulk bool) {
tz := time.Now()
createDSN(PSTLocation)
intArray := []int{1, 2, 3}
fltArray := []float64{0.1, 2.34, 5.678}
boolArray := []bool{true, false, true}
strArray := []string{"test1", "test2", "test3"}
byteArray := [][]byte{{0x01, 0x02, 0x03}, {0x04, 0x05, 0x06}, {0x07, 0x08, 0x09}}
now := time.Now()
loc, err := time.LoadLocation(PSTLocation)
if err != nil {
t.Error(err)
}
ntzArray := []time.Time{now, now.Add(1), now.Add(2)}
ltzArray := []time.Time{now.Add(3).In(loc), now.Add(4).In(loc), now.Add(5).In(loc)}
tzArray := []time.Time{tz.Add(6).In(loc), tz.Add(7).In(loc), tz.Add(8).In(loc)}
dtArray := []time.Time{now.Add(9), now.Add(10), now.Add(11)}
tmArray := []time.Time{now.Add(12), now.Add(13), now.Add(14)}
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec(createTableSQL)
defer dbt.mustExec(deleteTableSQL)
if bulk {
if _, err := dbt.exec("ALTER SESSION SET CLIENT_STAGE_ARRAY_BINDING_THRESHOLD = 1"); err != nil {
t.Error(err)
}
}
dbt.mustExec(insertSQL, Array(&intArray), Array(&fltArray),
Array(&boolArray), Array(&strArray), Array(&byteArray),
Array(&ntzArray, TimestampNTZType), Array(<zArray, TimestampLTZType),
Array(&tzArray, TimestampTZType), Array(&dtArray, DateType),
Array(&tmArray, TimeType))
rows := dbt.mustQuery(selectAllSQL)
defer func() {
assertNilF(t, rows.Close())
}()
var v0 int
var v1 float64
var v2 bool
var v3 string
var v4 []byte
var v5, v6, v7, v8, v9 time.Time
cnt := 0
for i := 0; rows.Next(); i++ {
if err := rows.Scan(&v0, &v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, &v9); err != nil {
t.Fatal(err)
}
if v0 != intArray[i] {
t.Fatalf("failed to fetch. expected %v, got: %v", intArray[i], v0)
}
if v1 != fltArray[i] {
t.Fatalf("failed to fetch. expected %v, got: %v", fltArray[i], v1)
}
if v2 != boolArray[i] {
t.Fatalf("failed to fetch. expected %v, got: %v", boolArray[i], v2)
}
if v3 != strArray[i] {
t.Fatalf("failed to fetch. expected %v, got: %v", strArray[i], v3)
}
if !bytes.Equal(v4, byteArray[i]) {
t.Fatalf("failed to fetch. expected %v, got: %v", byteArray[i], v4)
}
if v5.UnixNano() != ntzArray[i].UnixNano() {
t.Fatalf("failed to fetch. expected %v, got: %v", ntzArray[i], v5)
}
if v6.UnixNano() != ltzArray[i].UnixNano() {
t.Fatalf("failed to fetch. expected %v, got: %v", ltzArray[i], v6)
}
if v7.UnixNano() != tzArray[i].UnixNano() {
t.Fatalf("failed to fetch. expected %v, got: %v", tzArray[i], v7)
}
if v8.Year() != dtArray[i].Year() || v8.Month() != dtArray[i].Month() || v8.Day() != dtArray[i].Day() {
t.Fatalf("failed to fetch. expected %v, got: %v", dtArray[i], v8)
}
if v9.Hour() != tmArray[i].Hour() || v9.Minute() != tmArray[i].Minute() || v9.Second() != tmArray[i].Second() {
t.Fatalf("failed to fetch. expected %v, got: %v", tmArray[i], v9)
}
cnt++
}
if cnt != len(intArray) {
t.Fatal("failed to query")
}
})
createDSN("UTC")
}
func TestBulkArrayBinding(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec(fmt.Sprintf("create or replace table %v (c1 integer, c2 string, c3 timestamp_ltz, c4 timestamp_tz, c5 timestamp_ntz, c6 date, c7 time, c8 binary)", dbname))
now := time.Now()
someTime := time.Date(1, time.January, 1, 12, 34, 56, 123456789, time.UTC)
someDate := time.Date(2024, time.March, 18, 0, 0, 0, 0, time.UTC)
someBinary := []byte{0x01, 0x02, 0x03}
numRows := 100000
intArr := make([]int, numRows)
strArr := make([]string, numRows)
ltzArr := make([]time.Time, numRows)
tzArr := make([]time.Time, numRows)
ntzArr := make([]time.Time, numRows)
dateArr := make([]time.Time, numRows)
timeArr := make([]time.Time, numRows)
binArr := make([][]byte, numRows)
for i := 0; i < numRows; i++ {
intArr[i] = i
strArr[i] = "test" + strconv.Itoa(i)
ltzArr[i] = now
tzArr[i] = now.Add(time.Hour).UTC()
ntzArr[i] = now.Add(2 * time.Hour)
dateArr[i] = someDate
timeArr[i] = someTime
binArr[i] = someBinary
}
dbt.mustExec(fmt.Sprintf("insert into %v values (?, ?, ?, ?, ?, ?, ?, ?)", dbname), Array(&intArr), Array(&strArr), Array(<zArr, TimestampLTZType), Array(&tzArr, TimestampTZType), Array(&ntzArr, TimestampNTZType), Array(&dateArr, DateType), Array(&timeArr, TimeType), Array(&binArr))
rows := dbt.mustQuery("select * from " + dbname + " order by c1")
defer func() {
assertNilF(t, rows.Close())
}()
cnt := 0
var i int
var s string
var ltz, tz, ntz, date, tt time.Time
var b []byte
for rows.Next() {
if err := rows.Scan(&i, &s, <z, &tz, &ntz, &date, &tt, &b); err != nil {
t.Fatal(err)
}
assertEqualE(t, i, cnt)
assertEqualE(t, "test"+strconv.Itoa(cnt), s)
assertEqualE(t, ltz.UTC(), now.UTC())
assertEqualE(t, tz.UTC(), now.Add(time.Hour).UTC())
assertEqualE(t, ntz.UTC(), now.Add(2*time.Hour).UTC())
assertEqualE(t, date, someDate)
assertEqualE(t, tt, someTime)
assertBytesEqualE(t, b, someBinary)
cnt++
}
if cnt != numRows {
t.Fatalf("expected %v rows, got %v", numRows, cnt)
}
})
}
func TestBulkArrayBindingTimeWithPrecision(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec(fmt.Sprintf("create or replace table %v (s time(0), ms time(3), us time(6), ns time(9))", dbname))
someTimeWithSeconds := time.Date(1, time.January, 1, 1, 1, 1, 0, time.UTC)
someTimeWithMilliseconds := time.Date(1, time.January, 1, 2, 2, 2, 123000000, time.UTC)
someTimeWithMicroseconds := time.Date(1, time.January, 1, 3, 3, 3, 123456000, time.UTC)
someTimeWithNanoseconds := time.Date(1, time.January, 1, 4, 4, 4, 123456789, time.UTC)
numRows := 100000
secondsArr := make([]time.Time, numRows)
millisecondsArr := make([]time.Time, numRows)
microsecondsArr := make([]time.Time, numRows)
nanosecondsArr := make([]time.Time, numRows)
for i := 0; i < numRows; i++ {
secondsArr[i] = someTimeWithSeconds
millisecondsArr[i] = someTimeWithMilliseconds
microsecondsArr[i] = someTimeWithMicroseconds
nanosecondsArr[i] = someTimeWithNanoseconds
}
dbt.mustExec(fmt.Sprintf("insert into %v values (?, ?, ?, ?)", dbname), Array(&secondsArr, TimeType), Array(&millisecondsArr, TimeType), Array(µsecondsArr, TimeType), Array(&nanosecondsArr, TimeType))
rows := dbt.mustQuery("select * from " + dbname)
defer func() {
assertNilF(t, rows.Close())
}()
cnt := 0
var s, ms, us, ns time.Time
for rows.Next() {
if err := rows.Scan(&s, &ms, &us, &ns); err != nil {
t.Fatal(err)
}
assertEqualE(t, s, someTimeWithSeconds)
assertEqualE(t, ms, someTimeWithMilliseconds)
assertEqualE(t, us, someTimeWithMicroseconds)
assertEqualE(t, ns, someTimeWithNanoseconds)
cnt++
}
if cnt != numRows {
t.Fatalf("expected %v rows, got %v", numRows, cnt)
}
})
}
func TestBulkArrayMultiPartBinding(t *testing.T) {
rowCount := 1000000 // large enough to be partitioned into multiple files
randomIter := rand.Intn(3) + 2
randomStrings := make([]string, rowCount)
str := randomString(30)
for i := 0; i < rowCount; i++ {
randomStrings[i] = str
}
tempTableName := fmt.Sprintf("test_table_%v", randomString(5))
ctx := context.Background()
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec(fmt.Sprintf("CREATE TABLE %s (C VARCHAR(64) NOT NULL)", tempTableName))
defer dbt.mustExec("drop table " + tempTableName)
for i := 0; i < randomIter; i++ {
dbt.mustExecContext(ctx,
fmt.Sprintf("INSERT INTO %s VALUES (?)", tempTableName),
Array(&randomStrings))
rows := dbt.mustQuery("select count(*) from " + tempTableName)
defer func() {
assertNilF(t, rows.Close())
}()
if rows.Next() {
var count int
if err := rows.Scan(&count); err != nil {
t.Error(err)
}
}
}
rows := dbt.mustQuery("select count(*) from " + tempTableName)
defer func() {
assertNilF(t, rows.Close())
}()
if rows.Next() {
var count int
if err := rows.Scan(&count); err != nil {
t.Error(err)
}
if count != randomIter*rowCount {
t.Errorf("expected %v rows, got %v rows intead", randomIter*rowCount, count)
}
}
})
}
func TestBulkArrayMultiPartBindingInt(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec("create or replace table binding_test (c1 integer)")
startNum := 1000000
endNum := 3000000
numRows := endNum - startNum
intArr := make([]int, numRows)
for i := startNum; i < endNum; i++ {
intArr[i-startNum] = i
}
_, err := dbt.exec("insert into binding_test values (?)", Array(&intArr))
if err != nil {
t.Errorf("Should have succeeded to insert. err: %v", err)
}
rows := dbt.mustQuery("select * from binding_test order by c1")
defer func() {
assertNilF(t, rows.Close())
}()
cnt := startNum
var i int
for rows.Next() {
if err := rows.Scan(&i); err != nil {
t.Fatal(err)
}
if i != cnt {
t.Errorf("expected: %v, got: %v", cnt, i)
}
cnt++
}
if cnt != endNum {
t.Fatalf("expected %v rows, got %v", numRows, cnt-startNum)
}
dbt.mustExec("DROP TABLE binding_test")
})
}
func TestBulkArrayMultiPartBindingWithNull(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
dbt.mustExec("create or replace table binding_test (c1 integer, c2 string)")
startNum := 1000000
endNum := 2000000
numRows := endNum - startNum
// Define the integer and string arrays
intArr := make([]any, numRows)
stringArr := make([]any, numRows)
for i := startNum; i < endNum; i++ {
intArr[i-startNum] = i
stringArr[i-startNum] = fmt.Sprint(i)
}
// Set some of the rows to NULL
intArr[numRows-1] = nil
intArr[numRows-2] = nil
intArr[numRows-3] = nil
stringArr[1] = nil
stringArr[2] = nil
stringArr[3] = nil
_, err := dbt.exec("insert into binding_test values (?, ?)", Array(&intArr), Array(&stringArr))
if err != nil {
t.Errorf("Should have succeeded to insert. err: %v", err)
}
rows := dbt.mustQuery("select * from binding_test order by c1,c2")
defer func() {
assertNilF(t, rows.Close())
}()
cnt := startNum
var i sql.NullInt32
var s sql.NullString
for rows.Next() {
if err := rows.Scan(&i, &s); err != nil {
t.Fatal(err)
}
// Verify integer column c1
if i.Valid {
if int(i.Int32) != intArr[cnt-startNum] {
t.Fatalf("expected: %v, got: %v", cnt, int(i.Int32))
}
} else if !(cnt == startNum+numRows-1 || cnt == startNum+numRows-2 || cnt == startNum+numRows-3) {
t.Fatalf("expected NULL in column c1 at index: %v", cnt-startNum)
}
// Verify string column c2
if s.Valid {
if s.String != stringArr[cnt-startNum] {
t.Fatalf("expected: %v, got: %v", cnt, s.String)
}
} else if !(cnt == startNum+1 || cnt == startNum+2 || cnt == startNum+3) {
t.Fatalf("expected NULL in column c2 at index: %v", cnt-startNum)
}
cnt++
}
if cnt != endNum {
t.Fatalf("expected %v rows, got %v", numRows, cnt-startNum)
}
dbt.mustExec("DROP TABLE binding_test")
})
}
func TestFunctionParameters(t *testing.T) {
testcases := []struct {
testDesc string
paramType string
input any