Skip to content

Commit 271b10f

Browse files
committed
core: express what a dialect does to a type as data rather than registered code
The per-dialect Go hooks are gone. dialect.json now carries rewrites — an ordered list of pattern, template and bound, such as float($1) to real where $1 <= 24 or Decimal32($1) to Decimal(9, $1) — which the seed loads into sql_type_rewrite and the catalog applies before interning; the words and argument positions that are identifiers rather than types, kept as dialect flags; and SQLite's affinity rule, loaded into sql_type_affinity. A result that depends on an argument's value is a return template in functions.jsonl, kept on sql_proc and filled in from the call's literals. ClickHouse numbers an Enum's members and sorts a Variant's in its converter, where spelling a type belongs, and goldeneye writes an array column of the PostgreSQL system catalogs as its element with the array flag rather than pg_type's _text, which the legacy path now sees too. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qryf7a1doPFuCz2eGT3bCr
1 parent 85b92aa commit 271b10f

30 files changed

Lines changed: 1145 additions & 667 deletions

File tree

‎internal/core/analyzer/expr.go‎

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,9 @@ func (a *analyzer) typeFuncCall(f *ast.FuncCall) (exprType, error) {
829829
}
830830
}
831831
ret := a.returnType(p, argTypes)
832-
// A dialect may know the result better than the catalog does, from the
833-
// arguments' types and literal values.
834-
if computed := a.resultTypeHook(name, args, argTypes); computed != nil {
832+
// A result that depends on an argument's value is spelled by the seed
833+
// as a template over the arguments, filled in from the call.
834+
if computed := a.returnTemplate(p, args, argTypes); computed != nil {
835835
ret = a.lookupType(computed)
836836
}
837837
ret.nullable = p.ReturnNullable
@@ -841,20 +841,46 @@ func (a *analyzer) typeFuncCall(f *ast.FuncCall) (exprType, error) {
841841
return ret, nil
842842
}
843843

844-
// resultTypeHook asks the dialect's result-type rule about a call, handing
845-
// it each argument's type and, for an integer literal, its value.
846-
func (a *analyzer) resultTypeHook(name string, args []ast.Node, argTypes []exprType) *core.TypeExpr {
847-
ras := make([]core.ResultArg, len(args))
848-
for i, arg := range args {
849-
ras[i].Type = a.exprOf(argTypes[i])
850-
if c, ok := arg.(*ast.A_Const); ok {
851-
if n, ok := c.Val.(*ast.Integer); ok {
852-
v := n.Ival
853-
ras[i].Int = &v
844+
// returnTemplate fills a seed's return template — Decimal(18, $2) — from
845+
// the call: a $n that stands for an integer literal takes its value, and
846+
// one that stands for a typed argument takes its type. A template that
847+
// cannot be filled leaves the answer to the catalog.
848+
func (a *analyzer) returnTemplate(p core.ProcOverload, args []ast.Node, argTypes []exprType) *core.TypeExpr {
849+
if p.ReturnTemplate == "" {
850+
return nil
851+
}
852+
template := core.ParseTypeExpr(p.ReturnTemplate)
853+
fill := func(arg core.TypeArg) (core.TypeArg, bool) {
854+
n, ok := argIndex(arg.Type.Name)
855+
if !ok || n >= len(args) {
856+
return core.TypeArg{}, false
857+
}
858+
if c, ok := args[n].(*ast.A_Const); ok {
859+
if lit, ok := c.Val.(*ast.Integer); ok {
860+
v := lit.Ival
861+
return core.TypeArg{Label: arg.Label, Int: &v}, true
862+
}
863+
if lit, ok := c.Val.(*ast.String); ok {
864+
v := lit.Str
865+
return core.TypeArg{Label: arg.Label, String: &v}, true
854866
}
855867
}
868+
if t := a.exprOf(argTypes[n]); t != nil {
869+
return core.TypeArg{Label: arg.Label, Type: t.WithNullable(false)}, true
870+
}
871+
return core.TypeArg{}, false
872+
}
873+
for i, arg := range template.Args {
874+
if arg.Type == nil || !strings.HasPrefix(arg.Type.Name, "$") {
875+
continue
876+
}
877+
filled, ok := fill(arg)
878+
if !ok {
879+
return nil
880+
}
881+
template.Args[i] = filled
856882
}
857-
return a.cat.ResultTypeOf(name, ras)
883+
return template
858884
}
859885

860886
// returnType resolves a polymorphic return type — max(anyelement), or a

‎internal/core/catalog.go‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"database/sql"
66
"fmt"
77
"runtime"
8-
"sync"
98

109
"github.com/sqlc-dev/sqlc/internal/core/catalogdb"
1110
"github.com/sqlc-dev/sqlc/internal/core/catalogdef"
@@ -30,16 +29,12 @@ type Catalog struct {
3029
loadExtension func(name string) error
3130
extensions map[string]bool
3231

33-
// types remembers the rows and expressions looked up so far.
32+
// types remembers the rows and expressions looked up so far, and rules
33+
// what the dialect does to a type before storing it.
3434
types typeCache
35-
36-
// dialect is the seeded dialect's name, read back once it is asked for.
37-
dialect string
38-
dialectNameOnce sync.Once
35+
rules rules
3936
}
4037

41-
func contextBackground() context.Context { return context.Background() }
42-
4338
type Option func(*Catalog) error
4439

4540
func WithSeed(fn func(*Catalog) error) Option {

‎internal/core/catalogdb/models.go‎

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/core/catalogdb/query.sql.go‎

Lines changed: 123 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/core/catalogdef/query.sql‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ SELECT oid, namespace_oid, name, expr, category, typtype, preferred,
106106
FROM sql_type
107107
WHERE oid = ?;
108108

109+
-- name: CreateTypeRewrite :exec
110+
INSERT INTO sql_type_rewrite (dialect_oid, ord, pattern, template, cond)
111+
VALUES (?, ?, ?, ?, ?);
112+
113+
-- name: ListTypeRewrites :many
114+
SELECT pattern, template, cond FROM sql_type_rewrite
115+
WHERE dialect_oid = ? ORDER BY ord;
116+
117+
-- name: CreateTypeAffinity :exec
118+
INSERT INTO sql_type_affinity (dialect_oid, ord, words, type_oid)
119+
VALUES (?, ?, ?, ?);
120+
121+
-- name: ListTypeAffinities :many
122+
SELECT words, type_oid FROM sql_type_affinity
123+
WHERE dialect_oid = ? ORDER BY ord;
124+
109125
-- =============================== sql_class =============================
110126

111127
-- name: CreateClass :execlastid
@@ -213,8 +229,8 @@ INSERT INTO sql_constraint (class_oid, name, kind, columns) VALUES (?, ?, ?, ?);
213229
-- name: CreateProc :execlastid
214230
INSERT INTO sql_proc
215231
(namespace_oid, dialect_oid, name, kind,
216-
return_type_oid, return_set, return_nullable, strict, variadic_kind)
217-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
232+
return_type_oid, return_set, return_nullable, return_template, strict, variadic_kind)
233+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
218234

219235
-- name: CreateProcArg :exec
220236
INSERT INTO sql_proc_arg (proc_oid, ord, name, type_oid, mode, has_default)
@@ -226,12 +242,12 @@ WHERE proc_oid = ? AND mode IN ('i', 'b', 'v')
226242
ORDER BY ord;
227243

228244
-- name: FindProcsAnyNamespace :many
229-
SELECT oid, name, kind, return_type_oid, return_nullable
245+
SELECT oid, name, kind, return_type_oid, return_nullable, return_template
230246
FROM sql_proc
231247
WHERE name = ?;
232248

233249
-- name: FindProcsInNamespaces :many
234-
SELECT oid, name, kind, return_type_oid, return_nullable
250+
SELECT oid, name, kind, return_type_oid, return_nullable, return_template
235251
FROM sql_proc
236252
WHERE name = sqlc.arg(name)
237253
AND namespace_oid IN (sqlc.slice(namespace_oids));

0 commit comments

Comments
 (0)