forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduckdb_test.go
1149 lines (996 loc) · 31.5 KB
/
duckdb_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
package duckdb
import (
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
"reflect"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOpen(t *testing.T) {
t.Parallel()
t.Run("without config", func(t *testing.T) {
db := openDB(t)
defer db.Close()
require.NotNil(t, db)
})
t.Run("with config", func(t *testing.T) {
db, err := sql.Open("duckdb", "?access_mode=read_write&threads=4")
require.NoError(t, err)
defer db.Close()
var (
accessMode string
threads int64
)
res := db.QueryRow("SELECT current_setting('access_mode'), current_setting('threads')")
require.NoError(t, res.Scan(&accessMode, &threads))
require.Equal(t, int64(4), threads)
require.Equal(t, "read_write", accessMode)
})
t.Run("existing sqlite database", func(t *testing.T) {
db, err := sql.Open("duckdb", "sqlite:testdata/pets.sqlite")
require.NoError(t, err)
defer db.Close()
var species string
res := db.QueryRow("SELECT species FROM pets WHERE id=1")
require.NoError(t, res.Scan(&species))
require.Equal(t, "Gopher", species)
})
t.Run("with invalid config", func(t *testing.T) {
_, err := sql.Open("duckdb", "?threads=NaN")
if !errors.Is(err, errSetConfig) {
t.Fatal("invalid config should not be accepted")
}
})
}
func TestConnector_Close(t *testing.T) {
t.Parallel()
connector, err := NewConnector("", nil)
require.NoError(t, err)
// check that multiple close calls don't cause panics or errors
require.NoError(t, connector.Close())
require.NoError(t, connector.Close())
}
func TestConnPool(t *testing.T) {
db := openDB(t)
db.SetMaxOpenConns(2) // set connection pool size greater than 1
defer db.Close()
createTable(db, t)
// Get two separate connections and check they're consistent
conn1, err := db.Conn(context.Background())
require.NoError(t, err)
conn2, err := db.Conn(context.Background())
require.NoError(t, err)
res, err := conn1.ExecContext(context.Background(), "INSERT INTO foo VALUES ('lala', ?), ('lalo', ?)", 12345, 1234)
require.NoError(t, err)
ra, err := res.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(2), ra)
require.NoError(t, err)
rows, err := conn1.QueryContext(context.Background(), "select bar from foo limit 1")
require.NoError(t, err)
require.True(t, rows.Next())
rows.Close()
require.NoError(t, err)
rows, err = conn2.QueryContext(context.Background(), "select bar from foo limit 1")
require.NoError(t, err)
require.True(t, rows.Next())
rows.Close()
err = conn1.Close()
require.NoError(t, err)
err = conn2.Close()
require.NoError(t, err)
}
func TestConnInit(t *testing.T) {
connector, err := NewConnector("", func(execer driver.ExecerContext) error {
bootQueries := []string{
"INSTALL 'json'",
"LOAD 'json'",
}
for _, qry := range bootQueries {
_, err := execer.ExecContext(context.Background(), qry, nil)
if err != nil {
return err
}
}
return nil
})
require.NoError(t, err)
db := sql.OpenDB(connector)
db.SetMaxOpenConns(2) // set connection pool size greater than 1
defer db.Close()
// Get two separate connections and check they're consistent
conn1, err := db.Conn(context.Background())
require.NoError(t, err)
conn2, err := db.Conn(context.Background())
require.NoError(t, err)
res, err := conn1.ExecContext(context.Background(), "CREATE TABLE example (j JSON)")
require.NoError(t, err)
ra, err := res.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(0), ra)
res, err = conn2.ExecContext(context.Background(), "INSERT INTO example VALUES(' { \"family\": \"anatidae\", \"species\": [ \"duck\", \"goose\", \"swan\", null ] }')")
require.NoError(t, err)
ra, err = res.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), ra)
require.NoError(t, err)
rows, err := conn1.QueryContext(context.Background(), "SELECT json_valid(j) FROM example")
require.NoError(t, err)
require.True(t, rows.Next())
rows.Close()
require.NoError(t, err)
rows, err = conn2.QueryContext(context.Background(), "SELECT json_valid(j) FROM example")
require.NoError(t, err)
require.True(t, rows.Next())
rows.Close()
err = conn1.Close()
require.NoError(t, err)
err = conn2.Close()
require.NoError(t, err)
}
func TestExec(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
require.NotNil(t, createTable(db, t))
}
func TestQuery(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
createTable(db, t)
t.Run("simple", func(t *testing.T) {
res, err := db.Exec("INSERT INTO foo VALUES ('lala', ?), ('lalo', ?)", 12345, 1234)
require.NoError(t, err)
ra, err := res.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(2), ra)
rows, err := db.Query("SELECT bar, baz FROM foo WHERE baz > ?", 12344)
require.NoError(t, err)
defer rows.Close()
found := false
for rows.Next() {
var (
bar string
baz int
)
err := rows.Scan(&bar, &baz)
require.NoError(t, err)
if bar != "lala" || baz != 12345 {
t.Errorf("wrong values for bar [%s] and baz [%d]", bar, baz)
}
found = true
}
require.True(t, found, "could not find row")
})
t.Run("large number of rows", func(t *testing.T) {
_, err := db.Exec("CREATE TABLE integers AS SELECT * FROM range(0, 100000, 1) t(i);")
require.NoError(t, err)
rows, err := db.Query("SELECT i FROM integers ORDER BY i ASC")
require.NoError(t, err)
defer rows.Close()
expected := 0
for rows.Next() {
var i int
require.NoError(t, rows.Scan(&i))
assert.Equal(t, expected, i)
expected++
}
})
t.Run("wrong syntax", func(t *testing.T) {
_, err := db.Query("select * from tbl col=?", 1)
require.Error(t, err, "should return error with invalid syntax")
})
t.Run("missing parameter", func(t *testing.T) {
_, err := db.Query("select * from tbl col=?")
require.Error(t, err, "should return error with missing parameters")
})
t.Run("null", func(t *testing.T) {
var s *int
require.NoError(t, db.QueryRow("select null").Scan(&s))
require.Nil(t, s)
})
}
func TestStruct(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
createTable(db, t)
t.Run("scan single struct", func(t *testing.T) {
row := db.QueryRow("SELECT {'x': 1, 'y': 2}")
type point struct {
X int
Y int
}
want := Composite[point]{
point{1, 2},
}
var result Composite[point]
require.NoError(t, row.Scan(&result))
require.Equal(t, want, result)
})
t.Run("scan slice of structs", func(t *testing.T) {
_, err := db.Exec("INSERT INTO foo VALUES('lala', ?), ('lala', ?), ('lalo', 42), ('lalo', 43)", 12345, 12346)
require.NoError(t, err)
type row struct {
Bar string
Baz int
}
want := []Composite[row]{
{row{"lalo", 42}},
{row{"lalo", 43}},
{row{"lala", 12345}},
{row{"lala", 12346}},
}
rows, err := db.Query("SELECT struct_pack(bar, baz) FROM foo ORDER BY baz")
require.NoError(t, err)
defer rows.Close()
var result []Composite[row]
for rows.Next() {
var res Composite[row]
err := rows.Scan(&res)
require.NoError(t, err)
result = append(result, res)
}
require.Equal(t, want, result)
})
}
func TestMap(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
t.Run("select map", func(t *testing.T) {
var m Map
require.NoError(t, db.QueryRow("SELECT map(['foo', 'bar'], ['a', 'e'])").Scan(&m))
require.Equal(t, Map{
"foo": "a",
"bar": "e",
}, m)
})
t.Run("select nested map", func(t *testing.T) {
var m Map
err := db.QueryRow("SELECT map([map([1], [1]), map([2], [2])], ['a', 'e'])").Scan(&m)
require.ErrorIs(t, err, errUnsupportedMapKeyType)
})
t.Run("insert map", func(t *testing.T) {
tests := []struct {
sqlType string
input Map
}{
// {
// "MAP(VARCHAR, VARCHAR)",
// Map{"foo": "bar"},
// },
// {
// "MAP(INTEGER, VARCHAR)",
// Map{int32(1): "bar"},
// },
// {
// "MAP(VARCHAR, BIGINT)",
// Map{"foo": int64(2)},
// },
// {
// "MAP(VARCHAR, BOOLEAN)",
// Map{"foo": true},
// },
// {
// "MAP(DOUBLE, VARCHAR)",
// Map{1.1: "foobar"},
// },
}
for i, test := range tests {
_, err := db.Exec(fmt.Sprintf("CREATE TABLE map_test_%d(properties %s)", i, test.sqlType))
require.NoError(t, err)
_, err = db.Exec(fmt.Sprintf("INSERT INTO map_test_%d VALUES(?)", i), test.input)
require.NoError(t, err, test.input)
var m Map
require.NoError(t, db.QueryRow(fmt.Sprintf("SELECT properties FROM map_test_%d", i)).Scan(&m))
require.Equal(t, test.input, m)
}
})
}
func TestList(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
t.Run("integer list", func(t *testing.T) {
const n = 1000
var row Composite[[]int]
require.NoError(t, db.QueryRow("SELECT range(0, ?, 1)", n).Scan(&row))
require.Equal(t, n, len(row.Get()))
for i := 0; i < n; i++ {
require.Equal(t, i, row.Get()[i])
}
})
}
func compareDecimal(t *testing.T, want Decimal, got Decimal) {
require.Equal(t, want.Scale, got.Scale)
require.Equal(t, want.Width, got.Width)
require.Equal(t, want.Value.String(), got.Value.String())
}
func TestDecimal(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
t.Run("decimal widths", func(t *testing.T) {
for i := 1; i <= 38; i++ {
var f Decimal
require.NoError(t, db.QueryRow(fmt.Sprintf("SELECT 0::DECIMAL(%d, 1)", i)).Scan(&f))
require.Equal(t, Decimal{Width: uint8(i), Value: big.NewInt(0), Scale: 1}, f)
}
})
t.Run("multiple decimal types", func(t *testing.T) {
rows, err := db.Query(`SELECT * FROM (VALUES
(1.23::DECIMAL(3, 2)),
(-1.23::DECIMAL(3, 2)),
(123.45::DECIMAL(5, 2)),
(-123.45::DECIMAL(5, 2)),
(123456789.01::DECIMAL(11, 2)),
(-123456789.01::DECIMAL(11, 2)),
(1234567890123456789.234::DECIMAL(22, 3)),
(-1234567890123456789.234::DECIMAL(22, 3)),
) v
ORDER BY v ASC`)
require.NoError(t, err)
defer rows.Close()
bigNumber, success := new(big.Int).SetString("1234567890123456789234", 10)
require.True(t, success, "failed to parse big number")
bigNumberNegative, success := new(big.Int).SetString("-1234567890123456789234", 10)
require.True(t, success, "failed to parse big number")
tests := []struct {
input string
want Decimal
}{
{input: "1.23::DECIMAL(3, 2)", want: Decimal{Value: big.NewInt(123), Width: 3, Scale: 2}},
{input: "-1.23::DECIMAL(3, 2)", want: Decimal{Value: big.NewInt(-123), Width: 3, Scale: 2}},
{input: "123.45::DECIMAL(5, 2)", want: Decimal{Value: big.NewInt(12345), Width: 5, Scale: 2}},
{input: "-123.45::DECIMAL(5, 2)", want: Decimal{Value: big.NewInt(-12345), Width: 5, Scale: 2}},
{input: "123456789.01::DECIMAL(11, 2)", want: Decimal{Value: big.NewInt(12345678901), Width: 11, Scale: 2}},
{input: "-123456789.01::DECIMAL(11, 2)", want: Decimal{Value: big.NewInt(-12345678901), Width: 11, Scale: 2}},
{input: "1234567890123456789.234::DECIMAL(22, 3)", want: Decimal{Value: bigNumber, Width: 22, Scale: 3}},
{input: "-1234567890123456789.234::DECIMAL(22, 3)", want: Decimal{Value: bigNumberNegative, Width: 22, Scale: 3}},
}
for _, tc := range tests {
row := db.QueryRow(fmt.Sprintf("SELECT %s", tc.input))
var fs Decimal
require.NoError(t, row.Scan(&fs))
compareDecimal(t, tc.want, fs)
}
})
t.Run("huge decimal", func(t *testing.T) {
bigNumber, success := new(big.Int).SetString("12345678901234567890123456789", 10)
require.True(t, success, "failed to parse big number")
var f Decimal
require.NoError(t, db.QueryRow("SELECT 123456789.01234567890123456789::DECIMAL(29, 20)").Scan(&f))
compareDecimal(t, Decimal{Value: bigNumber, Width: 29, Scale: 20}, f)
})
t.Run("decimal to float64", func(t *testing.T) {
tests := []struct {
input string
want float64
}{
{input: "1.23::DECIMAL(3, 2)", want: 1.23},
{input: "-1.23::DECIMAL(3, 2)", want: -1.23},
{input: "123.45::DECIMAL(5, 2)", want: 123.45},
{input: "-123.45::DECIMAL(5, 2)", want: -123.45},
{input: "123456789.01::DECIMAL(11, 2)", want: 123456789.01},
{input: "-123456789.01::DECIMAL(11, 2)", want: -123456789.01},
{input: "1234567890123456789.234::DECIMAL(22, 3)", want: 1234567890123456789.234},
{input: "-1234567890123456789.234::DECIMAL(22, 3)", want: -1234567890123456789.234},
{input: "123456789.01234567890123456789::DECIMAL(29, 20)", want: 123456789.01234567890123456789},
{input: "-123456789.01234567890123456789::DECIMAL(29, 20)", want: -123456789.01234567890123456789},
}
for _, tc := range tests {
row := db.QueryRow(fmt.Sprintf("SELECT %s", tc.input))
var fs Decimal
require.NoError(t, row.Scan(&fs))
require.Equal(t, tc.want, fs.Float64())
}
})
}
func TestUUID(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
_, err := db.Exec("CREATE TABLE uuid_test(uuid UUID)")
require.NoError(t, err)
tests := []uuid.UUID{
uuid.New(),
uuid.Nil,
uuid.MustParse("80000000-0000-0000-0000-200000000000"),
}
for _, v := range tests {
_, err := db.Exec("INSERT INTO uuid_test VALUES(?)", v)
require.NoError(t, err)
var uuid uuid.UUID
require.NoError(t, db.QueryRow("SELECT uuid FROM uuid_test WHERE uuid = ?", v).Scan(&uuid))
require.Equal(t, v, uuid)
require.NoError(t, db.QueryRow("SELECT ?", v).Scan(&uuid))
require.Equal(t, v, uuid)
require.NoError(t, db.QueryRow("SELECT ?::uuid", v).Scan(&uuid))
require.Equal(t, v, uuid)
}
}
func TestENUMs(t *testing.T) {
t.Parallel()
type environment string
const (
Sea environment = "Sea"
Air environment = "Air"
Land environment = "Land"
)
db := openDB(t)
defer db.Close()
_, err := db.Exec("CREATE TYPE element AS ENUM ('Sea', 'Air', 'Land')")
require.NoError(t, err)
_, err = db.Exec("CREATE TABLE vehicles (name text, environment element)")
require.NoError(t, err)
_, err = db.Exec("INSERT INTO vehicles VALUES (?, ?), (?, ?)", "Aircraft", Air, "Boat", Sea)
require.NoError(t, err)
var name string
var env environment
require.NoError(t, db.QueryRow("SELECT name, environment FROM vehicles WHERE environment = ?", Air).Scan(&name, &env))
require.Equal(t, "Aircraft", name)
require.Equal(t, Air, env)
// enum list
_, err = db.Exec("CREATE TABLE all_enums (environments element[])")
require.NoError(t, err)
_, err = db.Exec("INSERT INTO all_enums VALUES ([?, ?, ?])", Air, Land, Sea)
require.NoError(t, err)
var row Composite[[]environment]
require.NoError(t, db.QueryRow("SELECT environments FROM all_enums").Scan(&row))
require.ElementsMatch(t, []environment{Air, Sea, Land}, row.Get())
}
func TestHugeInt(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
t.Run("scan hugeint", func(t *testing.T) {
tests := []string{
"0",
"1",
"-1",
"9223372036854775807",
"-9223372036854775808",
"170141183460469231731687303715884105727",
"-170141183460469231731687303715884105727",
}
for _, expected := range tests {
t.Run(expected, func(t *testing.T) {
var res *big.Int
err := db.QueryRow(fmt.Sprintf("SELECT %s::HUGEINT", expected)).Scan(&res)
require.NoError(t, err)
require.Equal(t, expected, res.String())
})
}
})
t.Run("bind hugeint", func(t *testing.T) {
_, err := db.Exec("CREATE TABLE hugeint_test (number HUGEINT)")
require.NoError(t, err)
val := big.NewInt(1)
val.SetBit(val, 101, 1)
_, err = db.Exec("INSERT INTO hugeint_test VALUES(?)", val)
require.NoError(t, err)
var res *big.Int
err = db.QueryRow("SELECT number FROM hugeint_test WHERE number = ?", val).Scan(&res)
require.NoError(t, err)
require.Equal(t, val.String(), res.String())
tooHuge := big.NewInt(1)
tooHuge.SetBit(tooHuge, 129, 1)
_, err = db.Exec("INSERT INTO hugeint_test VALUES(?)", tooHuge)
require.Error(t, err)
require.Contains(t, err.Error(), "too big")
})
}
func TestVarchar(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
var s string
t.Run("size is smaller than 12", func(t *testing.T) {
require.NoError(t, db.QueryRow("select 'inline'").Scan(&s))
require.Equal(t, "inline", s)
})
t.Run("size is exactly 12", func(t *testing.T) {
require.NoError(t, db.QueryRow("select 'inlineSize12'").Scan(&s))
require.Equal(t, "inlineSize12", s)
})
t.Run("size is 13", func(t *testing.T) {
require.NoError(t, db.QueryRow("select 'inlineSize13_'").Scan(&s))
require.Equal(t, "inlineSize13_", s)
})
t.Run("size is greater than 12", func(t *testing.T) {
const q = "uninlined string that is greater than 12 bytes long"
require.NoError(t, db.QueryRow(fmt.Sprintf("SELECT '%s'", q)).Scan(&s))
require.Equal(t, q, s)
})
}
func TestBlob(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
var bytes []byte
t.Run("select as string", func(t *testing.T) {
require.NoError(t, db.QueryRow("SELECT 'AB'::BLOB;").Scan(&bytes))
require.Equal(t, "AB", string(bytes))
})
t.Run("select as hex", func(t *testing.T) {
require.NoError(t, db.QueryRow("SELECT '\\xAA'::BLOB").Scan(&bytes))
require.Equal(t, []byte{0xAA}, bytes)
})
}
func TestBoolean(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
var res bool
t.Run("scan", func(t *testing.T) {
require.NoError(t, db.QueryRow("SELECT true").Scan(&res))
require.Equal(t, true, res)
require.NoError(t, db.QueryRow("SELECT false").Scan(&res))
require.Equal(t, false, res)
})
t.Run("bind", func(t *testing.T) {
require.NoError(t, db.QueryRow("SELECT ?", true).Scan(&res))
require.Equal(t, true, res)
require.NoError(t, db.QueryRow("SELECT ?", false).Scan(&res))
require.Equal(t, false, res)
require.NoError(t, db.QueryRow("SELECT ?", 0).Scan(&res))
require.Equal(t, false, res)
require.NoError(t, db.QueryRow("SELECT ?", 1).Scan(&res))
require.Equal(t, true, res)
})
}
func TestJSON(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
loadJSONExt(t, db)
var data string
t.Run("select empty JSON", func(t *testing.T) {
require.NoError(t, db.QueryRow("SELECT '{}'::JSON").Scan(&data))
require.Equal(t, "{}", string(data))
})
t.Run("select from marshalled JSON", func(t *testing.T) {
val, _ := json.Marshal(struct {
Foo string `json:"foo"`
}{
Foo: "bar",
})
require.NoError(t, db.QueryRow(`SELECT ?::JSON->>'foo'`, string(val)).Scan(&data))
require.Equal(t, "bar", data)
})
t.Run("select JSON array", func(t *testing.T) {
require.NoError(t, db.QueryRow("SELECT json_array('foo', 'bar')").Scan(&data))
require.Equal(t, `["foo","bar"]`, data)
var items []string
require.NoError(t, json.Unmarshal([]byte(data), &items))
require.Equal(t, len(items), 2)
require.Equal(t, items, []string{"foo", "bar"})
})
}
// CAST(? as DATE) generate result of type Date (time.Time)
func TestDate(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
tests := map[string]struct {
want time.Time
input string
}{
"epoch": {input: "1970-01-01", want: time.UnixMilli(0).UTC()},
"before 1970": {input: "1950-12-12", want: time.Date(1950, 12, 12, 0, 0, 0, 0, time.UTC)},
"after 1970": {input: "2022-12-12", want: time.Date(2022, 12, 12, 0, 0, 0, 0, time.UTC)},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var res time.Time
err := db.QueryRow("SELECT CAST(? as DATE)", tc.input).Scan(&res)
require.NoError(t, err)
require.Equal(t, tc.want, res)
})
}
}
func TestTimestamp(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
tests := map[string]struct {
input string
want time.Time
}{
"epoch": {input: "1970-01-01", want: time.UnixMilli(0).UTC()},
"before 1970": {input: "1950-12-12", want: time.Date(1950, 12, 12, 0, 0, 0, 0, time.UTC)},
"after 1970": {input: "2022-12-12", want: time.Date(2022, 12, 12, 0, 0, 0, 0, time.UTC)},
"HH:MM:SS": {input: "2022-12-12 11:35:43", want: time.Date(2022, 12, 12, 11, 35, 43, 0, time.UTC)},
"HH:MM:SS.DDDD": {input: "2022-12-12 11:35:43.5678", want: time.Date(2022, 12, 12, 11, 35, 43, 567800000, time.UTC)},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var res time.Time
if err := db.QueryRow("SELECT CAST(? as TIMESTAMP)", tc.input).Scan(&res); err != nil {
t.Errorf("can not scan value %v", err)
} else if res != tc.want {
t.Errorf("expected value %v != resulting value %v", tc.want, res)
}
})
}
}
func TestInterval(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
t.Run("bind interval", func(t *testing.T) {
interval := Interval{Days: 10, Months: 4, Micros: 4}
row := db.QueryRow("SELECT ?::INTERVAL", interval)
var res Interval
require.NoError(t, row.Scan(&res))
require.Equal(t, interval, res)
})
t.Run("scan interval", func(t *testing.T) {
tests := map[string]struct {
input string
want Interval
}{
"simple interval": {
input: "INTERVAL 5 HOUR",
want: Interval{Days: 0, Months: 0, Micros: 18000000000},
},
"interval arithmetic": {
input: "INTERVAL 1 DAY + INTERVAL 5 DAY",
want: Interval{Days: 6, Months: 0, Micros: 0},
},
"timestamp arithmetic": {
input: "CAST('2022-05-01' as TIMESTAMP) - CAST('2022-04-01' as TIMESTAMP)",
want: Interval{Days: 30, Months: 0, Micros: 0},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var res Interval
require.NoError(t, db.QueryRow(fmt.Sprintf("SELECT %s", tc.input)).Scan(&res))
require.Equal(t, tc.want, res)
})
}
})
}
func TestEmpty(t *testing.T) {
t.Parallel()
db := openDB(t)
defer db.Close()
rows, err := db.Query(`SELECT 1 WHERE 1 = 0`)
require.NoError(t, err)
defer rows.Close()
require.False(t, rows.Next())
require.NoError(t, rows.Err())
}
func TestTypeNamesAndScanTypes(t *testing.T) {
tests := []struct {
sql string
value any
typeName string
}{
// DUCKDB_TYPE_BOOLEAN
{
sql: "SELECT true AS col",
value: true,
typeName: "BOOLEAN",
},
// DUCKDB_TYPE_TINYINT
{
sql: "SELECT 31::TINYINT AS col",
value: int8(31),
typeName: "TINYINT",
},
// DUCKDB_TYPE_SMALLINT
{
sql: "SELECT 31::SMALLINT AS col",
value: int16(31),
typeName: "SMALLINT",
},
// DUCKDB_TYPE_INTEGER
{
sql: "SELECT 31::INTEGER AS col",
value: int32(31),
typeName: "INTEGER",
},
// DUCKDB_TYPE_BIGINT
{
sql: "SELECT 31::BIGINT AS col",
value: int64(31),
typeName: "BIGINT",
},
// DUCKDB_TYPE_UTINYINT
{
sql: "SELECT 31::UTINYINT AS col",
value: uint8(31),
typeName: "UTINYINT",
},
// DUCKDB_TYPE_USMALLINT
{
sql: "SELECT 31::USMALLINT AS col",
value: uint16(31),
typeName: "USMALLINT",
},
// DUCKDB_TYPE_UINTEGER
{
sql: "SELECT 31::UINTEGER AS col",
value: uint32(31),
typeName: "UINTEGER",
},
// DUCKDB_TYPE_UBIGINT
{
sql: "SELECT 31::UBIGINT AS col",
value: uint64(31),
typeName: "UBIGINT",
},
// DUCKDB_TYPE_FLOAT
{
sql: "SELECT 3.14::FLOAT AS col",
value: float32(3.14),
typeName: "FLOAT",
},
// DUCKDB_TYPE_DOUBLE
{
sql: "SELECT 3.14::DOUBLE AS col",
value: float64(3.14),
typeName: "DOUBLE",
},
// DUCKDB_TYPE_TIMESTAMP
{
sql: "SELECT '1992-09-20 11:30:00'::TIMESTAMP AS col",
value: time.Date(1992, 9, 20, 11, 30, 0, 0, time.UTC),
typeName: "TIMESTAMP",
},
// DUCKDB_TYPE_DATE
{
sql: "SELECT '1992-09-20'::DATE AS col",
value: time.Date(1992, 9, 20, 0, 0, 0, 0, time.UTC),
typeName: "DATE",
},
// DUCKDB_TYPE_TIME
{
sql: "SELECT '11:30:00'::TIME AS col",
value: time.Date(1970, 1, 1, 11, 30, 0, 0, time.UTC),
typeName: "TIME",
},
// DUCKDB_TYPE_INTERVAL
{
sql: "SELECT INTERVAL 15 MINUTES AS col",
value: Interval{Micros: 15 * 60 * 1000000},
typeName: "INTERVAL",
},
// DUCKDB_TYPE_HUGEINT
{
sql: "SELECT 31::HUGEINT AS col",
value: big.NewInt(31),
typeName: "HUGEINT",
},
// DUCKDB_TYPE_VARCHAR
{
sql: "SELECT 'foo'::VARCHAR AS col",
value: "foo",
typeName: "VARCHAR",
},
// DUCKDB_TYPE_BLOB
{
sql: "SELECT 'foo'::BLOB AS col",
value: []byte("foo"),
typeName: "BLOB",
},
// DUCKDB_TYPE_DECIMAL
{
sql: "SELECT 31::DECIMAL(30,17) AS col",
value: Decimal{Value: big.NewInt(3100000000000000000), Width: 30, Scale: 17},
typeName: "DECIMAL(30,17)",
},
// DUCKDB_TYPE_TIMESTAMP_S
{
sql: "SELECT '1992-09-20 11:30:00'::TIMESTAMP_S AS col",
value: time.Date(1992, 9, 20, 11, 30, 0, 0, time.UTC),
typeName: "TIMESTAMP_S",
},
// DUCKDB_TYPE_TIMESTAMP_MS
{
sql: "SELECT '1992-09-20 11:30:00'::TIMESTAMP_MS AS col",
value: time.Date(1992, 9, 20, 11, 30, 0, 0, time.UTC),
typeName: "TIMESTAMP_MS",
},
// DUCKDB_TYPE_TIMESTAMP_NS
{
sql: "SELECT '1992-09-20 11:30:00'::TIMESTAMP_NS AS col",
value: time.Date(1992, 9, 20, 11, 30, 0, 0, time.UTC),
typeName: "TIMESTAMP_NS",
},
// DUCKDB_TYPE_LIST
{
sql: "SELECT [['duck', 'goose', 'heron'], NULL, ['frog', 'toad'], []] AS col",
value: []any{[]any{"duck", "goose", "heron"}, nil, []any{"frog", "toad"}, []any{}},
typeName: "VARCHAR[][]",
},
// DUCKDB_TYPE_STRUCT
{
sql: "SELECT {'key1': 'string', 'key2': 1, 'key3': 12.345::DOUBLE} AS col",
value: map[string]any{"key1": "string", "key2": int32(1), "key3": float64(12.345)},
typeName: `STRUCT("key1" VARCHAR, "key2" INTEGER, "key3" DOUBLE)`,
},
{
sql: `SELECT {'key1 (,) \ \" "" "': 1} AS col`,
value: map[string]any{`key1 (,) \ \" "" "`: int32(1)},
typeName: `STRUCT("key1 (,) \ \"" """" """ INTEGER)`,
},
// DUCKDB_TYPE_MAP
{
sql: "SELECT map([1, 5], ['a', 'e']) AS col",
value: Map{int32(1): "a", int32(5): "e"},
typeName: "MAP(INTEGER, VARCHAR)",
},
// DUCKDB_TYPE_UUID
{
sql: "SELECT '53b4e983-b287-481a-94ad-6e3c90489913'::UUID AS col",
value: []byte{0x53, 0xb4, 0xe9, 0x83, 0xb2, 0x87, 0x48, 0x1a, 0x94, 0xad, 0x6e, 0x3c, 0x90, 0x48, 0x99, 0x13},
typeName: "UUID",
},
}
db := openDB(t)
for _, test := range tests {
t.Run(test.typeName, func(t *testing.T) {
rows, err := db.Query(test.sql)
require.NoError(t, err)
cols, err := rows.ColumnTypes()
require.NoError(t, err)
require.Equal(t, reflect.TypeOf(test.value), cols[0].ScanType())
require.Equal(t, test.typeName, cols[0].DatabaseTypeName())
var val any
require.True(t, rows.Next())
err = rows.Scan(&val)
require.NoError(t, err)
require.Equal(t, test.value, val)
require.Equal(t, rows.Next(), false)
})
}
}
// Running multiple statements in a single query. All statements except the last one are executed and if no error then last statement is executed with args and result returned.
func TestMultipleStatements(t *testing.T) {
db := openDB(t)
defer db.Close()
// test empty query
_, err := db.Exec("")
require.Error(t, err)
require.Contains(t, err.Error(), "no statements found")
// test invalid query
_, err = db.Exec("abc;")
require.Error(t, err)
require.Contains(t, err.Error(), "syntax error at or near \"abc\"")