-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvtabs_test.go
More file actions
511 lines (433 loc) · 12.1 KB
/
vtabs_test.go
File metadata and controls
511 lines (433 loc) · 12.1 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
package sqlite
import (
"context"
"encoding/json"
"hash/maphash"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestMetadata(t *testing.T) {
t.Run("simple values", func(t *testing.T) {
type S1 struct {
Name string `vtab:"name"`
Age int `vtab:"age"`
}
s1 := S1{Name: "John Doe", Age: 42}
db, err := Open(t.TempDir()+"/db", RegisterConstant("users", s1))
if err != nil {
t.Fatal(err)
}
var got S1
err = db.Exec(context.Background(), "select name, age from users").ScanOne(&got.Name, &got.Age)
if err != nil {
t.Fatal("error", err)
}
if got != s1 {
t.Errorf("invalid read back: want %v, got %v", s1, got)
}
})
t.Run("required value", func(t *testing.T) {
type S1 struct {
Name string `vtab:"name,required"`
Age int `vtab:"age"`
}
s1 := S1{Name: "John Doe", Age: 42}
db, err := Open(t.TempDir()+"/db", RegisterConstant("users", s1))
if err != nil {
t.Fatal(err)
}
var got S1
err = db.Exec(context.Background(), "select name, age from users").ScanOne(&got.Name, &got.Age)
if err == nil {
t.Error("expected constraint violation, got nothing")
}
t.Log(err)
})
t.Run("skipped value", func(t *testing.T) {
type S1 struct {
Name string `vtab:"name"`
Age int `vtab:"-"`
Gender string `vtab:"gender"`
}
s1 := S1{Name: "John Doe", Age: 42, Gender: "male"}
db, err := Open(t.TempDir()+"/db", RegisterConstant("users", s1))
if err != nil {
t.Fatal(err)
}
var got S1
err = db.Exec(context.Background(), "select name, gender from users").ScanOne(&got.Name, &got.Gender)
if err != nil {
t.Fatal("error", err)
}
if !cmp.Equal(s1, got, cmpopts.IgnoreFields(S1{}, "Age")) {
t.Errorf("invalid read back: want %v, got %v", s1, got)
}
})
t.Run("JSON accepted", func(t *testing.T) {
type S1 struct {
Name json.RawMessage `vtab:"name"`
}
s1 := S1{Name: []byte(`{"hello": "world"}`)}
db, err := Open(t.TempDir()+"/db", RegisterConstant("users", s1))
if err != nil {
t.Fatal(err)
}
var got string
err = db.Exec(context.Background(), "select name->'hello' from users").ScanOne(&got)
if err != nil {
t.Fatal("error", err)
}
if got != `"world"` {
t.Error("got invalid JSON back", got)
}
})
t.Run("Bool accepted", func(t *testing.T) {
type S1 struct {
OK bool `vtab:"ok"`
}
s1 := S1{OK: true} // false by init otherwise
db, err := Open(t.TempDir()+"/db", RegisterConstant("bools", s1))
if err != nil {
t.Fatal(err)
}
var got bool
err = db.Exec(context.Background(), "select ok from bools").ScanOne(&got)
if err != nil {
t.Fatal("error", err)
}
if !got {
t.Error("got invalid boolean back", got)
}
})
}
func TestFilter(t *testing.T) {
extract := make(chan []Constraint, 1) // extra space to prevent blocking
db, err := Open(t.TempDir()+"/db", RegisterTable("filter", filterValue{cs: extract}))
if err != nil {
t.Fatal(err)
}
lexsort := cmpopts.SortSlices(func(i, j Constraint) bool { return i.Column < j.Column })
onlypub := cmpopts.IgnoreUnexported(Constraint{})
t.Run("text", func(t *testing.T) {
err := db.Exec(context.Background(), "select count(*) from filter where text='hello'").Err()
if err != nil {
t.Fatal(err)
}
cs := <-extract
want := []Constraint{{Value: "hello", Operation: ConstraintEQ, Column: "text"}}
if !cmp.Equal(cs, want, onlypub) {
t.Error(cmp.Diff(cs, want, onlypub))
}
})
t.Run("number", func(t *testing.T) {
err := db.Exec(context.Background(), "select count(*) from filter where num=42").Err()
if err != nil {
t.Fatal(err)
}
cs := <-extract
want := []Constraint{{Value: 42, Operation: ConstraintEQ, Column: "num"}}
if !cmp.Equal(cs, want, onlypub) {
t.Error(cmp.Diff(cs, want, onlypub))
}
})
t.Run("blob", func(t *testing.T) {
err := db.Exec(context.Background(), "select count(*) from filter where blob=?", []byte("hellobytes")).Err()
if err != nil {
t.Fatal(err)
}
cs := <-extract
want := []Constraint{{Value: []byte("hellobytes"), Operation: ConstraintEQ, Column: "blob"}}
if !cmp.Equal(cs, want, onlypub) {
t.Error(cmp.Diff(cs, want, onlypub))
}
})
t.Run("multiple values", func(t *testing.T) {
err := db.Exec(context.Background(), "select count(*) from filter where text=? and num < 3", "hello world").Err()
if err != nil {
t.Fatal(err)
}
cs := <-extract
want := []Constraint{
{Value: "hello world", Operation: ConstraintEQ, Column: "text"},
{Value: 3, Operation: ConstraintLT, Column: "num"},
}
if !cmp.Equal(cs, want, lexsort, onlypub) {
t.Error(cmp.Diff(cs, want, lexsort, onlypub))
}
})
t.Run("multiple values in same column", func(t *testing.T) {
err := db.Exec(context.Background(), "select count(*) from filter where text=? and text = 'other'", "hello world").Err()
if err != nil {
t.Fatal(err)
}
})
t.Run("exclude limit", func(t *testing.T) {
err := db.Exec(context.Background(), "select count(*) from filter where text='needme' limit 10").Err()
if err != nil {
t.Fatal(err)
}
cs := <-extract
want := []Constraint{
{Value: "needme", Operation: ConstraintEQ, Column: "text"},
}
if !cmp.Equal(cs, want, onlypub) {
t.Error(cmp.Diff(cs, want, onlypub))
}
})
}
type filterValue struct {
Primary string `vtab:"text,filtered"`
Num int `vtab:"num,filtered"`
Blob []byte `vtab:"blob,filtered"`
cs chan []Constraint `vtab:"-"`
}
func (f filterValue) Filter(_ int, cs Constraints) Iter[filterValue] {
f.cs <- cs // capture for testing
return &iterone[filterValue]{v: f}
}
func (f filterValue) BestIndex(st IndexState, cs Constraints) (ecost, erow int64) {
for i := range cs {
st.Use(i)
}
return 1, 1
}
func TestLeftJoin(t *testing.T) {
db, err := Open(":memory:", RegisterTable("p", LeftJoinS1{}), RegisterTable("q", LeftJoinS2{}))
if err != nil {
t.Fatal(err)
}
type person struct{ Name, BirthPlace string }
st := db.Exec(context.Background(), `explain select q.name, birth_place from p left join q using (name) order by name`)
for st.Next() {
var res MultiString
st.Scan(&res)
t.Log("query plan", res)
}
if st.Err() != nil {
t.Fatal("query plan error", st.Err())
}
rows := db.Exec(context.Background(), `select q.name, birth_place from p left join q using (name) order by name`)
var people []person
for rows.Next() {
var p person
rows.Scan(&p.Name, &p.BirthPlace)
people = append(people, p)
}
if rows.Err() != nil {
t.Fatal(rows.Err())
}
want := []person{{"Carson", "Z"}, {"Martin", "Y"}, {"Octavio", "X"}}
if !cmp.Equal(people, want) {
t.Errorf("got different results back %s", cmp.Diff(people, want))
}
}
type LeftJoinS1 struct {
Name string `vtab:"name"`
}
func (s LeftJoinS1) Filter(_ int, cs Constraints) Iter[LeftJoinS1] {
return FromArray([]LeftJoinS1{{Name: "Octavio"}, {Name: "Martin"}, {Name: "Carson"}})
}
type LeftJoinS2 struct {
Name string `vtab:"name,required"`
BirthPlace string `vtab:"birth_place"`
}
// manually expanded template from constrainer (avoid circular import)
func (r LeftJoinS2) GetName(cs Constraints) (v string) {
match := 0
for _, c := range cs {
if c.Column == "name" {
if c.Operation == ConstraintEQ {
v = c.ValueString()
match++
} else {
panic("Value getter with non-constrained values")
}
}
}
if match != 1 {
panic("field should have been filtered")
}
return v
}
func (s LeftJoinS2) Filter(_ int, cs Constraints) Iter[LeftJoinS2] {
var birthPlace string
name := s.GetName(cs)
switch name {
case "Octavio":
birthPlace = "X"
case "Martin":
birthPlace = "Y"
case "Carson":
birthPlace = "Z"
}
return FromOne(LeftJoinS2{
Name: name,
BirthPlace: birthPlace,
})
}
func TestPointerPassing(t *testing.T) {
db, err := Open(":memory:",
RegisterTable("pointed", Rich{}),
RegisterFunc("printme", func(p *PtrTo) string {
t.Log("got pointer", p)
return p.v
}),
)
if err != nil {
t.Fatal(err)
}
var plan strings.Builder
stn := db.Exec(context.Background(), "explain select printme(ptr) from pointed(?)", AsPointer(&PtrTo{v: "hello, world!"}))
for stn.Next() {
var line MultiString
stn.Scan(&line)
t.Log("write lin", line)
plan.WriteString(strings.Join(line, "|") + "\n")
}
if stn.Err() != nil {
t.Fatal(stn.Err())
}
t.Log("plan is ", plan.String())
var got string
if err := db.Exec(context.Background(), "select printme(ptr) from pointed(?)", AsPointer(&PtrTo{v: "hello, world!"})).ScanOne(&got); err != nil {
t.Fatal(err)
}
if got != "hello, world!" {
t.Error("invalid pointer passed ", got)
}
}
type PtrTo struct{ v string }
type Rich struct {
Ptr *PtrTo `vtab:"ptr,required,hidden"`
}
func (r Rich) Filter(_ int, cs Constraints) Iter[Rich] {
var ptr *PtrTo
for _, cs := range cs {
if cs.Column == "ptr" {
ptr = cs.Value.(*PtrTo)
}
}
if ptr == nil {
panic("invalid")
}
return &iterone[Rich]{v: Rich{Ptr: ptr}}
}
// https://sqlite.org/vtab.html#xrowid must return the same number if the values are the same
// by default, we have a counter per connection, so every row ID is considered different
// implementations might have a specific Hash method to override this behavior
func TestRowID(t *testing.T) {
db, err := Open(":memory:",
RegisterTable("stringsize", StringSize{}))
if err != nil {
t.Fatal(err)
}
type ss struct {
S string
D int
}
var got []ss
if err := db.Exec(context.Background(), "PRAGMA vdbe_trace = true").Err(); err != nil {
t.Fatal(err)
}
rows := db.Exec(context.Background(), `select value, size from stringsize where value in (?, 'wworld');`, "hello")
t.Log(rows.ExplainQueryPlan())
t.Log(rows.Bytecode())
for rows.Next() {
var v string
var sz int
rows.Scan(&v, &sz)
if len(v) != sz {
t.Errorf("%s has invalid size %d", v, sz)
}
got = append(got, ss{v, sz})
}
if rows.Err() != nil {
t.Fatal(rows.Err())
}
want := []ss{
{"hello", 5},
{"wworld", 6},
}
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}
type StringSize struct {
Value string `vtab:"value,filtered"`
Size int `vtab:"size,filtered"`
}
func (a StringSize) Filter(_ int, cs Constraints) Iter[StringSize] {
for _, c := range cs {
if c.Column == "value" {
val := c.Value.(string)
return FromOne(StringSize{Value: val, Size: len(val)})
}
if c.Column == "size" {
sz := c.Value.(int)
val := strings.Repeat("a", sz)
return FromOne(StringSize{Value: val, Size: sz})
}
}
return FromOne(StringSize{})
}
var sseed = maphash.MakeSeed()
func (a StringSize) Hash64() int64 { return int64(maphash.String(sseed, a.Value)) }
func TestUpdate(t *testing.T) {
db, err := Open(":memory:",
RegisterTable("test", Standard{}))
if err != nil {
t.Fatal(err)
}
scenario := [...]struct {
query string
want []Standard
}{
{"insert into test (key, value) values ('hello', 'world')", []Standard{{"hello", "world"}}},
{"update test set value = 'other world' where key = 'hello'", []Standard{{"hello", "other world"}}},
{"insert into test (key, value) values ('well', 'done')", []Standard{{"hello", "other world"}, {"well", "done"}}},
{"delete from test", nil},
}
for i, s := range scenario {
if err := db.Exec(context.Background(), s.query).Err(); err != nil {
t.Fatalf("at step %d: %s", i, err)
}
var got []Standard
for st := db.Exec(context.Background(), "select key, value from test"); st.Next(); {
var s Standard
st.Scan(&s.Key, &s.Value)
got = append(got, s)
}
if err != nil {
t.Fatalf("cannot read data back: %s", err)
}
if !cmp.Equal(got, s.want) {
t.Errorf("at step %d: %s", i, cmp.Diff(got, s.want))
}
}
}
var GlobalStandards []Standard
type Standard struct {
Key string `vtab:"key"`
Value string `vtab:"value"`
}
func (s Standard) Filter(_ int, cs Constraints) Iter[Standard] {
return FromArray(GlobalStandards)
}
func (s Standard) Hash64() int64 { return int64(maphash.String(sseed, s.Key)) }
func (s Standard) Update(_ context.Context, idx int64, n Standard) error {
for i, v := range GlobalStandards {
ix := int64(maphash.String(sseed, v.Key))
if idx != -1 && idx == ix {
if n.Key != "" || n.Value != "" {
GlobalStandards[i] = n
} else {
GlobalStandards = append(GlobalStandards[:i], GlobalStandards[i+1:]...)
}
return nil
}
}
GlobalStandards = append(GlobalStandards, n)
return nil
}