Skip to content

Commit 65af806

Browse files
committed
Fixes.
1 parent 5c1c0f0 commit 65af806

File tree

4 files changed

+50
-46
lines changed

4 files changed

+50
-46
lines changed

ext/bloom/bloom.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ func create(db *sqlite3.Conn, _, schema, table string, arg ...string) (_ *bloom,
8787
return nil, err
8888
}
8989

90+
id := db.LastInsertRowID()
91+
defer db.SetLastInsertRowID(id)
92+
9093
err = db.Exec(fmt.Sprintf(
9194
`INSERT INTO %s.%s (rowid, data, p, n, m, k)
9295
VALUES (1, zeroblob(%d), %f, %d, %d, %d)`,
@@ -163,13 +166,14 @@ func (b *bloom) BestIndex(idx *sqlite3.IndexInfo) error {
163166
if cst.Usable && cst.Column == 1 &&
164167
cst.Op == sqlite3.INDEX_CONSTRAINT_EQ {
165168
idx.ConstraintUsage[n].ArgvIndex = 1
169+
idx.OrderByConsumed = true
170+
idx.EstimatedRows = 1
171+
idx.EstimatedCost = float64(b.hashes)
172+
idx.IdxFlags = sqlite3.INDEX_SCAN_UNIQUE
173+
return nil
166174
}
167175
}
168-
idx.OrderByConsumed = true
169-
idx.EstimatedRows = 1
170-
idx.EstimatedCost = float64(b.hashes)
171-
idx.IdxFlags = sqlite3.INDEX_SCAN_UNIQUE
172-
return nil
176+
return sqlite3.CONSTRAINT
173177
}
174178

175179
func (b *bloom) Update(arg ...sqlite3.Value) (rowid int64, err error) {

ext/csv/csv.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ func RegisterFS(db *sqlite3.Conn, fsys fs.FS) {
9696
}
9797
schema = getSchema(header, columns, row)
9898
} else {
99-
table.typs = getColumnAffinities(schema)
99+
defer func() {
100+
if err == nil {
101+
table.typs, err = getColumnAffinities(schema)
102+
}
103+
}()
100104
}
101105

102106
err = db.DeclareVTab(schema)

ext/csv/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ const (
1717
real affinity = 4
1818
)
1919

20-
func getColumnAffinities(schema string) []affinity {
20+
func getColumnAffinities(schema string) ([]affinity, error) {
2121
tab, err := vtabutil.Parse(schema)
2222
if err != nil {
23-
return nil
23+
return nil, err
2424
}
2525
defer tab.Close()
2626

@@ -29,7 +29,7 @@ func getColumnAffinities(schema string) []affinity {
2929
col := tab.Column(i)
3030
types[i] = getAffinity(col.Type())
3131
}
32-
return types
32+
return types, nil
3333
}
3434

3535
func getAffinity(declType string) affinity {

util/vtabutil/parse.go

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ import (
1212
)
1313

1414
const (
15-
code = 4
16-
base = 8
15+
_NONE = iota
16+
_MEMORY
17+
_SYNTAX
18+
_UNSUPPORTEDSQL
19+
20+
codeptr = 4
21+
baseptr = 8
1722
)
1823

1924
var (
@@ -22,6 +27,7 @@ var (
2227
ctx context.Context
2328
once sync.Once
2429
runtime wazero.Runtime
30+
module wazero.CompiledModule
2531
)
2632

2733
// Table holds metadata about a table.
@@ -35,32 +41,38 @@ type Table struct {
3541
//
3642
// [CREATE]: https://sqlite.org/lang_createtable.html
3743
// [ALTER TABLE]: https://sqlite.org/lang_altertable.html
38-
func Parse(sql string) (*Table, error) {
44+
func Parse(sql string) (_ *Table, err error) {
3945
once.Do(func() {
4046
ctx = context.Background()
4147
cfg := wazero.NewRuntimeConfigInterpreter().WithDebugInfoEnabled(false)
4248
runtime = wazero.NewRuntimeWithConfig(ctx, cfg)
49+
module, err = runtime.CompileModule(ctx, binary)
4350
})
51+
if err != nil {
52+
return nil, err
53+
}
4454

45-
mod, err := runtime.InstantiateWithConfig(ctx, binary, wazero.NewModuleConfig().WithName(""))
55+
mod, err := runtime.InstantiateModule(ctx, module, wazero.NewModuleConfig().WithName(""))
4656
if err != nil {
4757
return nil, err
4858
}
4959

50-
if buf, ok := mod.Memory().Read(base, uint32(len(sql))); ok {
60+
if buf, ok := mod.Memory().Read(baseptr, uint32(len(sql))); ok {
5161
copy(buf, sql)
5262
}
53-
r, err := mod.ExportedFunction("sql3parse_table").Call(ctx, base, uint64(len(sql)), code)
63+
r, err := mod.ExportedFunction("sql3parse_table").Call(ctx, baseptr, uint64(len(sql)), codeptr)
5464
if err != nil {
5565
return nil, err
5666
}
5767

58-
c, _ := mod.Memory().ReadUint32Le(code)
59-
if c == uint32(_MEMORY) {
68+
c, _ := mod.Memory().ReadUint32Le(codeptr)
69+
switch c {
70+
case _MEMORY:
6071
panic(util.OOMErr)
61-
}
62-
if c != uint32(_NONE) {
63-
return nil, ecode(c)
72+
case _SYNTAX:
73+
return nil, util.ErrorString("sql3parse: invalid syntax")
74+
case _UNSUPPORTEDSQL:
75+
return nil, util.ErrorString("sql3parse: unsupported SQL")
6476
}
6577
if r[0] == 0 {
6678
return nil, nil
@@ -102,6 +114,15 @@ func (t *Table) Column(i int) Column {
102114
}
103115
}
104116

117+
func (t *Table) string(ptr uint32) string {
118+
if ptr == 0 {
119+
return ""
120+
}
121+
off, _ := t.mod.Memory().ReadUint32Le(ptr + 0)
122+
len, _ := t.mod.Memory().ReadUint32Le(ptr + 4)
123+
return t.sql[off-baseptr : off+len-baseptr]
124+
}
125+
105126
// Column holds metadata about a column.
106127
type Column struct {
107128
tab *Table
@@ -116,30 +137,5 @@ func (c Column) Type() string {
116137
if err != nil {
117138
panic(err)
118139
}
119-
if r[0] == 0 {
120-
return ""
121-
}
122-
off, _ := c.tab.mod.Memory().ReadUint32Le(uint32(r[0]) + 0)
123-
len, _ := c.tab.mod.Memory().ReadUint32Le(uint32(r[0]) + 4)
124-
return c.tab.sql[off-base : off+len-base]
125-
}
126-
127-
type ecode uint32
128-
129-
const (
130-
_NONE ecode = iota
131-
_MEMORY
132-
_SYNTAX
133-
_UNSUPPORTEDSQL
134-
)
135-
136-
func (e ecode) Error() string {
137-
switch e {
138-
case _SYNTAX:
139-
return "sql3parse: invalid syntax"
140-
case _UNSUPPORTEDSQL:
141-
return "sql3parse: unsupported SQL"
142-
default:
143-
panic(util.AssertErr())
144-
}
140+
return c.tab.string(uint32(r[0]))
145141
}

0 commit comments

Comments
 (0)