Skip to content

Commit 14c2a58

Browse files
committed
sqlite: a declared spelling stands on its affinity
A SQLite dialect setting makes each alias in types.jsonl a type of its own standing on the type it aliases, so a column declared VARCHAR(255) reports that spelling, as SQLite does, while comparing as text. A spelling neither seeded nor aliased — FOO BAR(3) — gets the affinity SQLite's rule gives it as its base, through a per-dialect hook the catalog asks when a schema declares a family it does not know. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qryf7a1doPFuCz2eGT3bCr
1 parent 48a9f7a commit 14c2a58

8 files changed

Lines changed: 455 additions & 2 deletions

File tree

‎internal/core/hooks.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,42 @@ import "sync"
1111
// for what only code can say.
1212
type Canonicalizer func(*TypeExpr) *TypeExpr
1313

14+
// A UserTypeBase says what a type family the schema declared and the
15+
// dialect did not seed stands on: SQLite gives every declared spelling one
16+
// of five affinities by a rule over its words, so FOO BAR(3) compares as a
17+
// numeric. It returns the base family's name and the category the new type
18+
// takes, or an empty name for a type that stands on nothing.
19+
type UserTypeBase func(name string) (base, category string)
20+
1421
var (
1522
hooksMu sync.RWMutex
1623
canonicalizers = map[string]Canonicalizer{}
24+
userTypeBases = map[string]UserTypeBase{}
1725
)
1826

27+
// RegisterUserTypeBase installs the rule a dialect resolves an unseeded
28+
// type family by, under the dialect's name.
29+
func RegisterUserTypeBase(dialect string, fn UserTypeBase) {
30+
hooksMu.Lock()
31+
defer hooksMu.Unlock()
32+
userTypeBases[dialect] = fn
33+
}
34+
35+
// userTypeBase applies the catalog's dialect's rule for an unseeded family.
36+
func (c *Catalog) userTypeBase(name string) (base, category string) {
37+
dialect := c.dialectName()
38+
if dialect == "" {
39+
return "", ""
40+
}
41+
hooksMu.RLock()
42+
fn := userTypeBases[dialect]
43+
hooksMu.RUnlock()
44+
if fn == nil {
45+
return "", ""
46+
}
47+
return fn(name)
48+
}
49+
1950
// RegisterCanonicalizer installs the canonicalizer for a dialect, by the
2051
// name its dialect.json records. An engine registers its own at init, so
2152
// that a catalog restored from the cache — which runs no seed — finds it by

‎internal/core/types.go‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,26 @@ func (c *Catalog) CreateUserType(name, category string) (int64, error) {
146146
if err != nil {
147147
return 0, fmt.Errorf("create type %q: %w", name, err)
148148
}
149-
oid, err := c.CreateTypeSpec(TypeSpec{
149+
spec := TypeSpec{
150150
Name: bare,
151151
NamespaceOID: nsOID,
152152
Typtype: typtype,
153153
Category: category,
154154
DialectOID: c.dialectOID,
155-
})
155+
}
156+
// A dialect may say what an unknown spelling stands on, as SQLite's
157+
// affinity rule does; the type then resolves through that base and
158+
// needs no operators of its own.
159+
if category == "U" {
160+
if base, cat := c.userTypeBase(bare); base != "" {
161+
if baseOID, err := c.TypeOID(base); err == nil {
162+
spec.BaseOID = baseOID
163+
spec.Category = cat
164+
return c.CreateTypeSpec(spec)
165+
}
166+
}
167+
}
168+
oid, err := c.CreateTypeSpec(spec)
156169
if err != nil {
157170
return 0, err
158171
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "analyze",
3+
"args": ["--dialect", "sqlite", "--schema", "schema.sql", "query.sql"],
4+
"contexts": ["base"]
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
INSERT INTO things (id, title, code, price, flag, big, data, untyped, ratio, weird, created, n, label)
2+
VALUES (1, 'abc', 'xy', 1.5, 1, 9000000000, x'00ff', 7, 2.5, '12', '2024-01-01 00:00:00', 3, 'ab');
3+
INSERT INTO strict_things (id, anything, body, amount, raw, n) VALUES (1, 'x', 'b', 1.5, x'00', 2);
4+
INSERT INTO things (id, n) VALUES (2, 0);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- name: AllTypes :many
2+
SELECT * FROM things;
3+
4+
-- name: StrictTypes :many
5+
SELECT * FROM strict_things;
6+
7+
-- name: Casts :one
8+
SELECT
9+
CAST(title AS INTEGER) AS a,
10+
CAST(n AS TEXT) AS b,
11+
CAST(title AS REAL) AS d,
12+
weird + 1 AS e,
13+
title || 'x' AS f
14+
FROM things;
15+
16+
-- name: Params :one
17+
SELECT id FROM things
18+
WHERE title = ? AND price = ? AND untyped = ? AND weird = ? AND big = ? AND n = ?;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CREATE TABLE things (
2+
id INTEGER PRIMARY KEY,
3+
title VARCHAR(255),
4+
code VARYING CHARACTER(10),
5+
price DECIMAL(10,5),
6+
flag BOOLEAN,
7+
big UNSIGNED BIG INT,
8+
data BLOB,
9+
untyped,
10+
ratio DOUBLE PRECISION,
11+
weird FOO BAR(3),
12+
created DATETIME,
13+
n INT NOT NULL,
14+
label NCHAR(5)
15+
);
16+
17+
CREATE TABLE strict_things (
18+
id INTEGER,
19+
anything ANY,
20+
body TEXT,
21+
amount REAL,
22+
raw BLOB,
23+
n INT
24+
) STRICT;

0 commit comments

Comments
 (0)