Repository navigation
Expand file tree
/
Copy pathconn_test.go
More file actions
528 lines (482 loc) · 16 KB
/
Copy pathconn_test.go
File metadata and controls
528 lines (482 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// Copyright 2026 The Sqlite Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlite
import (
"database/sql"
"fmt"
"reflect"
"strings"
"testing"
"time"
)
// TestColumnTextScan exercises the rows.Scan TEXT-column path that
// (*conn).columnText feeds. The test covers the three branches of that
// function: NULL (returned as empty string), empty string, and non-empty
// values of varying lengths. A regression on the unsafe.String pattern would
// either truncate the result, expose Go-heap garbage, or trip the race
// detector under -race.
func TestColumnTextScan(t *testing.T) {
db, err := sql.Open(driverName, "file::memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.Exec(`CREATE TABLE t (id INTEGER PRIMARY KEY, s TEXT)`); err != nil {
t.Fatal(err)
}
long := strings.Repeat("a1B2c3D4", 256) // 2048 bytes, well past inline storage
cases := []struct {
id int64
s string
}{
{1, "hello"},
{2, ""},
{3, "unicode é 中文 \U0001F600"},
{4, long},
}
for _, c := range cases {
if _, err := db.Exec(`INSERT INTO t(id, s) VALUES (?, ?)`, c.id, c.s); err != nil {
t.Fatalf("insert id=%d: %v", c.id, err)
}
}
for _, c := range cases {
var got string
if err := db.QueryRow(`SELECT s FROM t WHERE id = ?`, c.id).Scan(&got); err != nil {
t.Fatalf("scan id=%d: %v", c.id, err)
}
if got != c.s {
t.Errorf("id=%d: got %q (len %d), want %q (len %d)", c.id, got, len(got), c.s, len(c.s))
}
}
// Read all rows in a single query so columnText is invoked many times in
// quick succession; a stale-pointer regression would surface here as the
// last row's text bleeding into earlier rows.
rows, err := db.Query(`SELECT s FROM t ORDER BY id`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
var got []string
for rows.Next() {
var s string
if err := rows.Scan(&s); err != nil {
t.Fatal(err)
}
got = append(got, s)
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
want := []string{"hello", "", "unicode é 中文 \U0001F600", long}
if len(got) != len(want) {
t.Fatalf("row count: got %d, want %d", len(got), len(want))
}
for i := range got {
if got[i] != want[i] {
t.Errorf("row %d: got %q (len %d), want %q (len %d)", i, got[i], len(got[i]), want[i], len(want[i]))
}
}
}
// benchColumnTextScan exercises the rows.Scan TEXT path many times so the
// per-row cost is dominated by (*conn).columnText, not by statement
// preparation or driver bookkeeping. textLen controls the per-row payload
// size; the default mode in conn.go did one make+one string(b)-copy per
// invocation, which means at small sizes the alloc count dominates and at
// large sizes the memcpy dominates.
func benchColumnTextScan(b *testing.B, textLen int) {
db, err := sql.Open(driverName, "file::memory:")
if err != nil {
b.Fatal(err)
}
defer db.Close()
if _, err := db.Exec(`CREATE TABLE t (s TEXT)`); err != nil {
b.Fatal(err)
}
payload := strings.Repeat("X", textLen)
const rows = 1000
tx, err := db.Begin()
if err != nil {
b.Fatal(err)
}
stmt, err := tx.Prepare(`INSERT INTO t (s) VALUES (?)`)
if err != nil {
b.Fatal(err)
}
for i := 0; i < rows; i++ {
if _, err := stmt.Exec(payload); err != nil {
b.Fatal(err)
}
}
stmt.Close()
if err := tx.Commit(); err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r, err := db.Query(`SELECT s FROM t`)
if err != nil {
b.Fatal(err)
}
for r.Next() {
var s string
if err := r.Scan(&s); err != nil {
b.Fatal(err)
}
}
if err := r.Err(); err != nil {
b.Fatal(err)
}
r.Close()
}
}
// BenchmarkColumnTextScanShort measures the rows.Scan TEXT path on a small
// payload where alloc count dominates the cost.
func BenchmarkColumnTextScanShort(b *testing.B) {
benchColumnTextScan(b, 16)
}
// BenchmarkColumnTextScanMedium measures the rows.Scan TEXT path on a
// medium payload where both alloc count and memcpy contribute.
func BenchmarkColumnTextScanMedium(b *testing.B) {
benchColumnTextScan(b, 256)
}
// BenchmarkColumnTextScanLong measures the rows.Scan TEXT path on a large
// payload where the second memcpy that the old string(b) conversion forced
// is the dominant cost.
func BenchmarkColumnTextScanLong(b *testing.B) {
benchColumnTextScan(b, 4096)
}
// TestColumnTypeDatabaseTypeNameCache verifies that the rows decltype cache
// returns the same uppercase declared type for every row of a result set
// (across multiple Next calls) and matches the schema's declared type case-
// insensitively. The result is compared against a Next loop that reads the
// decltype on each row, so a stale or per-row regression would surface as a
// type-name mismatch between the two readers.
func TestColumnTypeDatabaseTypeNameCache(t *testing.T) {
db, err := sql.Open(driverName, "file::memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
// Mix declared types in different cases and across all SQLite storage
// classes. The varied casing exercises the strings.ToUpper path of the
// cache.
if _, err := db.Exec(`CREATE TABLE t (a integer, b TEXT, c BlOb, d DATETIME, e Date, f boolean)`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`INSERT INTO t VALUES (1, 'x', X'00', '2025-01-15 10:30:00', '2025-01-15', 1)`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`INSERT INTO t VALUES (2, 'y', X'01', '2025-01-16 11:00:00', '2025-01-16', 0)`); err != nil {
t.Fatal(err)
}
rows, err := db.Query(`SELECT a, b, c, d, e, f FROM t`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
types, err := rows.ColumnTypes()
if err != nil {
t.Fatal(err)
}
wantTypes := []string{"INTEGER", "TEXT", "BLOB", "DATETIME", "DATE", "BOOLEAN"}
if len(types) != len(wantTypes) {
t.Fatalf("column count: got %d, want %d", len(types), len(wantTypes))
}
for i, ct := range types {
if got := ct.DatabaseTypeName(); got != wantTypes[i] {
t.Errorf("column %d: got %q, want %q", i, got, wantTypes[i])
}
}
// Drain rows; reading the cache for every row of a multi-row scan must
// keep returning the same values for the lifetime of the result set.
rowCount := 0
for rows.Next() {
rowCount++
for i, ct := range types {
if got := ct.DatabaseTypeName(); got != wantTypes[i] {
t.Errorf("row %d column %d: got %q, want %q (cache changed mid-scan)", rowCount, i, got, wantTypes[i])
}
}
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
if rowCount != 2 {
t.Errorf("row count: got %d, want 2", rowCount)
}
}
// benchTextToTimeScan exercises the rows.Next + Scan path under
// _texttotime=1, which fires r.ColumnTypeDatabaseTypeName(i) for every TEXT
// column on every row to decide whether to parse the value as time. Before
// the decltype cache landed, each call did a libc.GoString + strings.ToUpper
// per row per column. With the cache the lookup is a single slice index.
func benchTextToTimeScan(b *testing.B, columnCount int) {
db, err := sql.Open(driverName, "file::memory:?_texttotime=1")
if err != nil {
b.Fatal(err)
}
defer db.Close()
var schemaCols, selectCols, insertCols, insertVals []string
for i := 0; i < columnCount; i++ {
schemaCols = append(schemaCols, fmt.Sprintf("c%d DATETIME", i))
selectCols = append(selectCols, fmt.Sprintf("c%d", i))
insertCols = append(insertCols, fmt.Sprintf("c%d", i))
insertVals = append(insertVals, "?")
}
if _, err := db.Exec(`CREATE TABLE t (` + strings.Join(schemaCols, ", ") + `)`); err != nil {
b.Fatal(err)
}
const rows = 1000
tx, err := db.Begin()
if err != nil {
b.Fatal(err)
}
stmt, err := tx.Prepare(`INSERT INTO t (` + strings.Join(insertCols, ", ") + `) VALUES (` + strings.Join(insertVals, ", ") + `)`)
if err != nil {
b.Fatal(err)
}
args := make([]any, columnCount)
for i := 0; i < columnCount; i++ {
args[i] = "2025-01-15 10:30:00"
}
for i := 0; i < rows; i++ {
if _, err := stmt.Exec(args...); err != nil {
b.Fatal(err)
}
}
stmt.Close()
if err := tx.Commit(); err != nil {
b.Fatal(err)
}
query := `SELECT ` + strings.Join(selectCols, ", ") + ` FROM t`
dest := make([]any, columnCount)
destPtrs := make([]any, columnCount)
for i := range dest {
destPtrs[i] = &dest[i]
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r, err := db.Query(query)
if err != nil {
b.Fatal(err)
}
for r.Next() {
if err := r.Scan(destPtrs...); err != nil {
b.Fatal(err)
}
}
if err := r.Err(); err != nil {
b.Fatal(err)
}
r.Close()
}
}
// BenchmarkTextToTimeScan1Col measures the _texttotime=1 hot path with a
// single DATETIME column, isolating the per-row decltype lookup cost.
func BenchmarkTextToTimeScan1Col(b *testing.B) {
benchTextToTimeScan(b, 1)
}
// BenchmarkTextToTimeScan5Cols measures the same path with a wider result
// set so the cache savings scale linearly with column count.
func BenchmarkTextToTimeScan5Cols(b *testing.B) {
benchTextToTimeScan(b, 5)
}
// TestColumnTypeScanTypeDecltypeCache locks down the ColumnTypeScanType comparison logic
// that was rewritten from a lowercase decltype switch to the cached uppercase
// switch. Each of the four arms that look at the cache (INTEGER -> BOOLEAN,
// INTEGER -> DATE/DATETIME/TIME/TIMESTAMP, TEXT default, TEXT under
// _texttotime -> DATE/DATETIME/TIME/TIMESTAMP) gets a column with a
// mixed-case declared type so a regression on the case-folding path would
// surface as a wrong reflect.Type for that column.
func TestColumnTypeScanTypeDecltypeCache(t *testing.T) {
// Each case inserts one row before querying so ColumnTypeScanType sees
// the actual storage class (sqlite3_column_type), not SQLITE_NULL.
cases := []struct {
dsn string
col string
value any
want reflect.Type
label string
}{
// INTEGER + BOOLEAN decltype (case-insensitive) always returns bool.
{"file::memory:", "b BoOlEaN", int64(1), reflect.TypeOf(false), "BOOLEAN INTEGER default"},
{"file::memory:?_texttotime=1", "b boolean", int64(0), reflect.TypeOf(false), "BOOLEAN INTEGER + _texttotime"},
// INTEGER + DATE/DATETIME/TIME/TIMESTAMP decltype always returns
// time.Time independent of any DSN flag (the cache-driven switch
// has no flag gate on the INTEGER arm).
{"file::memory:", "d Date", int64(1736899200), reflect.TypeOf(time.Time{}), "DATE INTEGER default"},
{"file::memory:", "dt datetime", int64(1736941800), reflect.TypeOf(time.Time{}), "DATETIME INTEGER default"},
{"file::memory:", "tm TIME", int64(37800), reflect.TypeOf(time.Time{}), "TIME INTEGER default"},
{"file::memory:", "ts TimeStamp", int64(1736941800), reflect.TypeOf(time.Time{}), "TIMESTAMP INTEGER default"},
// INTEGER without a recognised decltype falls back to int64.
{"file::memory:", "n integer", int64(42), reflect.TypeOf(int64(0)), "plain INTEGER default"},
{"file::memory:", "x BIGINT", int64(42), reflect.TypeOf(int64(0)), "unrecognised INTEGER decltype"},
// TEXT default returns string, even for DATETIME-shaped decltypes,
// because the textToTime opt-in is off.
{"file::memory:", "s TEXT", "hello", reflect.TypeOf(""), "plain TEXT default"},
{"file::memory:", "dt DateTime", "2025-01-15 10:30:00", reflect.TypeOf(""), "DATETIME TEXT default (no textToTime)"},
// TEXT under _texttotime=1 returns time.Time for the four recognised
// decltypes (mixed case), and string for everything else.
{"file::memory:?_texttotime=1", "d Date", "2025-01-15", reflect.TypeOf(time.Time{}), "DATE TEXT + _texttotime"},
{"file::memory:?_texttotime=1", "dt DateTime", "2025-01-15 10:30:00", reflect.TypeOf(time.Time{}), "DATETIME TEXT + _texttotime"},
{"file::memory:?_texttotime=1", "tm time", "10:30:00", reflect.TypeOf(time.Time{}), "TIME TEXT + _texttotime"},
{"file::memory:?_texttotime=1", "ts TIMESTAMP", "2025-01-15 10:30:00", reflect.TypeOf(time.Time{}), "TIMESTAMP TEXT + _texttotime"},
{"file::memory:?_texttotime=1", "s TEXT", "hello", reflect.TypeOf(""), "plain TEXT + _texttotime"},
}
for _, c := range cases {
t.Run(c.label, func(t *testing.T) {
db, err := sql.Open(driverName, c.dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.Exec(`CREATE TABLE t (` + c.col + `)`); err != nil {
t.Fatalf("create table: %v", err)
}
if _, err := db.Exec(`INSERT INTO t VALUES (?)`, c.value); err != nil {
t.Fatalf("insert: %v", err)
}
rows, err := db.Query(`SELECT * FROM t`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
if !rows.Next() {
t.Fatal("expected one row")
}
types, err := rows.ColumnTypes()
if err != nil {
t.Fatal(err)
}
if len(types) != 1 {
t.Fatalf("column count: got %d, want 1", len(types))
}
if got := types[0].ScanType(); got != c.want {
t.Errorf("ScanType: got %v, want %v", got, c.want)
}
})
}
}
// TestParseTimeFormatCache verifies that the per-rows-per-column format-index
// hint reused by (*conn).parseTime keeps returning correct parsed time values
// across many rows of a steady-format column, and that a column whose format
// switches mid-result-set still parses correctly via the fallthrough path.
func TestParseTimeFormatCache(t *testing.T) {
db, err := sql.Open(driverName, "file::memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.Exec(`CREATE TABLE t (id INTEGER PRIMARY KEY, dt DATETIME)`); err != nil {
t.Fatal(err)
}
// First three rows use the same canonical SQLite TEXT format (matches
// format index 2 of parseTimeFormats: "2006-01-02 15:04:05.999999999").
// Row 4 uses the ISO-T format (matches index 3). Row 5 uses the
// date-only fallback (index 6). After the cache stabilises on row 1, the
// hinted format helps rows 2 and 3 directly and rows 4-5 fall through.
values := []string{
"2025-01-15 10:30:00",
"2025-01-15 11:00:00",
"2025-01-15 11:30:00",
"2025-01-16T08:15:00",
"2025-01-17",
}
for i, v := range values {
if _, err := db.Exec(`INSERT INTO t(id, dt) VALUES (?, ?)`, i+1, v); err != nil {
t.Fatalf("insert id=%d: %v", i+1, err)
}
}
rows, err := db.Query(`SELECT dt FROM t ORDER BY id`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
wantTimes := []time.Time{
time.Date(2025, 1, 15, 10, 30, 0, 0, time.UTC),
time.Date(2025, 1, 15, 11, 0, 0, 0, time.UTC),
time.Date(2025, 1, 15, 11, 30, 0, 0, time.UTC),
time.Date(2025, 1, 16, 8, 15, 0, 0, time.UTC),
time.Date(2025, 1, 17, 0, 0, 0, 0, time.UTC),
}
i := 0
for rows.Next() {
if i >= len(wantTimes) {
t.Fatalf("too many rows; want %d", len(wantTimes))
}
var got time.Time
if err := rows.Scan(&got); err != nil {
t.Fatalf("row %d scan: %v", i, err)
}
if !got.Equal(wantTimes[i]) {
t.Errorf("row %d: got %v, want %v", i, got, wantTimes[i])
}
i++
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
if i != len(wantTimes) {
t.Fatalf("row count: got %d, want %d", i, len(wantTimes))
}
}
// benchParseTimeScan exercises the rows.Next + Scan path on a DATETIME TEXT
// column. With the parseTime format-index cache, every row after the first
// hits the hinted format directly; without the cache, every row re-walks
// the parseTimeFormats list until it finds a match.
func benchParseTimeScan(b *testing.B) {
db, err := sql.Open(driverName, "file::memory:")
if err != nil {
b.Fatal(err)
}
defer db.Close()
if _, err := db.Exec(`CREATE TABLE t (dt DATETIME)`); err != nil {
b.Fatal(err)
}
const rows = 1000
tx, err := db.Begin()
if err != nil {
b.Fatal(err)
}
stmt, err := tx.Prepare(`INSERT INTO t (dt) VALUES (?)`)
if err != nil {
b.Fatal(err)
}
for i := 0; i < rows; i++ {
// Canonical SQLite TEXT datetime format (index 2 of
// parseTimeFormats).
if _, err := stmt.Exec("2025-01-15 10:30:00"); err != nil {
b.Fatal(err)
}
}
stmt.Close()
if err := tx.Commit(); err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r, err := db.Query(`SELECT dt FROM t`)
if err != nil {
b.Fatal(err)
}
var got time.Time
for r.Next() {
if err := r.Scan(&got); err != nil {
b.Fatal(err)
}
}
if err := r.Err(); err != nil {
b.Fatal(err)
}
r.Close()
}
}
// BenchmarkParseTimeScan measures the rows.Next DATETIME TEXT path with the
// format-index cache active.
func BenchmarkParseTimeScan(b *testing.B) {
benchParseTimeScan(b)
}