forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
440 lines (369 loc) · 11.8 KB
/
errors_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
package duckdb
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func testErrorInternal(t *testing.T, actual error, contains []string) {
for _, msg := range contains {
require.Contains(t, actual.Error(), msg)
}
levels := strings.Count(actual.Error(), driverErrMsg)
require.Equal(t, 1, levels)
}
func testError(t *testing.T, actual error, contains ...string) {
testErrorInternal(t, actual, contains)
}
func TestErrOpen(t *testing.T) {
t.Run(errParseDSN.Error(), func(t *testing.T) {
_, err := sql.Open("duckdb", ":mem ory:")
testError(t, err, errParseDSN.Error())
})
t.Run(errOpen.Error(), func(t *testing.T) {
_, err := sql.Open("duckdb", "?readonly")
testError(t, err, errOpen.Error(), duckdbErrMsg)
})
t.Run(errSetConfig.Error(), func(t *testing.T) {
_, err := sql.Open("duckdb", "?threads=NaN")
testError(t, err, errSetConfig.Error())
})
t.Run("local config option", func(t *testing.T) {
_, err := sql.Open("duckdb", "?schema=main")
testError(t, err, errSetConfig.Error())
})
}
func TestErrNestedMap(t *testing.T) {
t.Parallel()
db := openDB(t)
var m Map
err := db.QueryRow("SELECT MAP([MAP([1], [1]), MAP([2], [2])], ['a', 'e'])").Scan(&m)
testError(t, err, errUnsupportedMapKeyType.Error())
require.NoError(t, db.Close())
}
func TestErrAppender(t *testing.T) {
t.Parallel()
t.Run(errAppenderInvalidCon.Error(), func(t *testing.T) {
var con driver.Conn
_, err := NewAppenderFromConn(con, "", "test")
testError(t, err, errAppenderInvalidCon.Error())
})
t.Run(errAppenderClosedCon.Error(), func(t *testing.T) {
c, err := NewConnector("", nil)
require.NoError(t, err)
con, err := c.Connect(context.Background())
require.NoError(t, err)
require.NoError(t, con.Close())
_, err = NewAppenderFromConn(con, "", "test")
testError(t, err, errAppenderClosedCon.Error())
require.NoError(t, c.Close())
})
t.Run(errAppenderCreation.Error(), func(t *testing.T) {
c, err := NewConnector("", nil)
require.NoError(t, err)
con, err := c.Connect(context.Background())
require.NoError(t, err)
_, err = NewAppenderFromConn(con, "", "does_not_exist")
testError(t, err, errAppenderCreation.Error(), duckdbErrMsg)
require.NoError(t, con.Close())
require.NoError(t, c.Close())
})
t.Run(errAppenderDoubleClose.Error(), func(t *testing.T) {
c, err := NewConnector("", nil)
require.NoError(t, err)
_, err = sql.OpenDB(c).Exec(`CREATE TABLE tbl (i INTEGER)`)
require.NoError(t, err)
con, err := c.Connect(context.Background())
require.NoError(t, err)
a, err := NewAppenderFromConn(con, "", "tbl")
require.NoError(t, err)
cleanupAppender(t, c, con, a)
err = a.Close()
testError(t, err, errAppenderDoubleClose.Error())
})
t.Run(unsupportedTypeErrMsg, func(t *testing.T) {
c, err := NewConnector("", nil)
require.NoError(t, err)
_, err = sql.OpenDB(c).Exec(`CREATE TABLE test (int_array INTEGER[2])`)
require.NoError(t, err)
con, err := c.Connect(context.Background())
require.NoError(t, err)
_, err = NewAppenderFromConn(con, "", "test")
testError(t, err, errAppenderCreation.Error(), unsupportedTypeErrMsg)
require.NoError(t, con.Close())
require.NoError(t, c.Close())
})
t.Run(columnCountErrMsg, func(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test (a VARCHAR, b VARCHAR)`)
err := a.AppendRow("hello")
testError(t, err, errAppenderAppendRow.Error(), columnCountErrMsg)
cleanupAppender(t, c, con, a)
})
t.Run(errAppenderAppendAfterClose.Error(), func(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test (str VARCHAR)`)
require.NoError(t, a.Close())
err := a.AppendRow("hello")
testError(t, err, errAppenderAppendAfterClose.Error())
require.NoError(t, con.Close())
require.NoError(t, c.Close())
})
t.Run(errAppenderFlush.Error(), func(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test (c1 INTEGER PRIMARY KEY)`)
require.NoError(t, a.AppendRow(int32(1)))
require.NoError(t, a.AppendRow(int32(1)))
err := a.Flush()
testError(t, err, errAppenderFlush.Error())
err = a.Close()
testError(t, err, errAppenderClose.Error())
require.NoError(t, con.Close())
require.NoError(t, c.Close())
})
t.Run(errAppenderClose.Error(), func(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test (c1 INTEGER PRIMARY KEY)`)
require.NoError(t, a.AppendRow(int32(1)))
require.NoError(t, a.AppendRow(int32(1)))
err := a.Close()
testError(t, err, errAppenderClose.Error())
require.NoError(t, con.Close())
require.NoError(t, c.Close())
})
t.Run(errUnsupportedMapKeyType.Error(), func(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test (m MAP(INT[], STRUCT(v INT)))`)
err := a.AppendRow(nil)
testError(t, err, errAppenderAppendRow.Error(), errUnsupportedMapKeyType.Error())
cleanupAppender(t, c, con, a)
})
}
func TestErrAppend(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test (id BIGINT, str VARCHAR)`)
err := a.AppendRow("hello", "world")
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow(false, 42)
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendDecimal(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test (d DECIMAL(8, 2))`)
err := a.AppendRow(Decimal{Width: 9, Scale: 2})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow(Decimal{Width: 8, Scale: 3})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendEnum(t *testing.T) {
c, con, a := prepareAppender(t, testTypesEnumSQL+";"+`CREATE TABLE test (e my_enum)`)
err := a.AppendRow("3")
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendSimpleStruct(t *testing.T) {
c, con, a := prepareAppender(t, `
CREATE TABLE test (
simple_struct STRUCT(A INT, B VARCHAR)
)`)
err := a.AppendRow(1)
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow("hello")
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
type other struct {
S string
I int
}
err = a.AppendRow(other{"hello", 1})
testError(t, err, errAppenderAppendRow.Error(), structFieldErrMsg)
err = a.AppendRow(
wrappedSimpleStruct{
"hello there",
simpleStruct{A: 0, B: "one billion ducks"},
},
)
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow(
wrappedStruct{
"hello there",
simpleStruct{A: 0, B: "one billion ducks"},
},
)
testError(t, err, errAppenderAppendRow.Error(), structFieldErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendStruct(t *testing.T) {
c, con, a := prepareAppender(t, `
CREATE TABLE test (
mix STRUCT(A STRUCT(L VARCHAR[]), B STRUCT(L INT[])[])
)`)
err := a.AppendRow(simpleStruct{1, "hello"})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendList(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test(intSlice INT[])`)
err := a.AppendRow([]string{"foo", "bar", "baz"})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow([][]int32{{1, 2, 3}, {4, 5, 6}})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendStructWithList(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test (struct_with_list STRUCT(L INT[]))`)
err := a.AppendRow([]int32{1, 2, 3})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
l := struct{ L []string }{L: []string{"a", "b", "c"}}
testError(t, a.AppendRow(l), errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendNestedStruct(t *testing.T) {
c, con, a := prepareAppender(t, `
CREATE TABLE test (
wrapped_simple_struct STRUCT(A VARCHAR, B STRUCT(A INT, B VARCHAR)),
)`)
err := a.AppendRow(simpleStruct{1, "hello"})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAppendNestedList(t *testing.T) {
c, con, a := prepareAppender(t, `CREATE TABLE test(int_slice INT[][][])`)
err := a.AppendRow([]int32{1, 2, 3})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow(1)
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
err = a.AppendRow([][]int32{{1, 2, 3}, {4, 5, 6}})
testError(t, err, errAppenderAppendRow.Error(), castErrMsg)
cleanupAppender(t, c, con, a)
}
func TestErrAPISetValue(t *testing.T) {
var chunk DataChunk
err := chunk.SetValue(1, 42, "hello")
testError(t, err, errAPI.Error(), columnCountErrMsg)
}
func TestDuckDBErrors(t *testing.T) {
db := openDB(t)
defer db.Close()
createTable(db, t, `CREATE TABLE duckdberror_test(bar VARCHAR UNIQUE, baz INT32, u_1 UNION("string" VARCHAR))`)
_, err := db.Exec("INSERT INTO duckdberror_test(bar, baz) VALUES('bar', 0)")
require.NoError(t, err)
testCases := []struct {
tpl string
errTyp ErrorType
}{
{
tpl: "SELECT * FROM not_exist WHERE baz=0",
errTyp: ErrorTypeCatalog,
},
{
tpl: "SELECT * FROM duckdberror_test WHERE col=?",
errTyp: ErrorTypeBinder,
},
{
tpl: "SELEC * FROM duckdberror_test baz=0",
errTyp: ErrorTypeParser,
},
{
tpl: "INSERT INTO duckdberror_test(bar, baz) VALUES('bar', 1)",
errTyp: ErrorTypeConstraint,
},
{
tpl: "INSERT INTO duckdberror_test(bar, baz) VALUES('foo', 18446744073709551615)",
errTyp: ErrorTypeConversion,
},
{
tpl: "INSTALL not_exist",
errTyp: ErrorTypeHTTP,
},
{
tpl: "LOAD not_exist",
errTyp: ErrorTypeIO,
},
{
tpl: "SELECT array_length(array_value(array_value(1, 2, 2), array_value(3, 4, 3)), 3)",
errTyp: ErrorTypeOutOfRange,
},
{
tpl: "SELECT '010110'::BIT & '11000'::BIT",
errTyp: ErrorTypeInvalidInput,
},
{
tpl: "SET external_threads=-1",
errTyp: ErrorTypeSyntax,
},
{
tpl: "CREATE UNIQUE INDEX idx ON duckdberror_test(u_1)",
errTyp: ErrorTypeInvalidType,
},
}
for _, tc := range testCases {
_, err := db.Exec(tc.tpl)
de, ok := err.(*Error)
if !ok {
require.Fail(t, "error type is not (*duckdb.Error)", "tql: %s\ngot: %#v", tc.tpl, err)
}
require.Equal(t, de.Type, tc.errTyp, "tpl: %s\nactual error msg: %s", tc.tpl, de.Msg)
}
}
func TestGetDuckDBError(t *testing.T) {
// only for the corner cases
testCases := []*Error{
{
Msg: "",
Type: ErrorTypeInvalid,
},
{
Msg: "Unknown",
Type: ErrorTypeInvalid,
},
{
Msg: "Error: xxx",
Type: ErrorTypeUnknownType,
},
// next two for the prefix testing
{
Msg: "Invalid Error: xxx",
Type: ErrorTypeInvalid,
},
{
Msg: "Invalid Input Error: xxx",
Type: ErrorTypeInvalidInput,
},
}
for _, tc := range testCases {
err := getDuckDBError(tc.Msg).(*Error)
require.Equal(t, tc, err)
}
}
type wrappedDuckDBError struct {
e *Error
}
func (w *wrappedDuckDBError) Error() string {
return w.e.Error()
}
func (w *wrappedDuckDBError) Unwrap() error {
return w.e
}
func TestGetDuckDBErrorIs(t *testing.T) {
const errMsg = "Out of Range Error: Overflow"
outOfRangeErr1 := &Error{
Type: ErrorTypeOutOfRange,
Msg: errMsg,
}
outOfRangeErr1Copy := &Error{
Type: ErrorTypeOutOfRange,
Msg: errMsg,
}
outOfRangeErr2 := &Error{
Type: ErrorTypeOutOfRange,
Msg: "Out of Range Error: array_length dimension '3' out of range (min: '1', max: '2')",
}
invalidInputErr := &Error{
Type: ErrorTypeInvalidInput,
Msg: "Invalid Input Error: Map keys can not be NULL",
}
require.ErrorIs(t, outOfRangeErr1, outOfRangeErr1)
require.ErrorIs(t, outOfRangeErr1Copy, outOfRangeErr1)
require.ErrorIs(t, &wrappedDuckDBError{outOfRangeErr1Copy}, outOfRangeErr1)
require.Equal(t, false, errors.Is(outOfRangeErr2, outOfRangeErr1))
require.Equal(t, false, errors.Is(invalidInputErr, outOfRangeErr1))
require.Equal(t, false, errors.Is(errors.New(errMsg), outOfRangeErr1))
}