Skip to content

Commit fedc4a1

Browse files
committed
postgresql: typmods, dimensions, interval fields, declared types and namespaces in the core
The parser now hands the core a column's type modifiers, so numeric(10,2), character varying(255), timestamp(3) and bit(8) reach the catalog, with an interval's field mask decoded into the words format_type prints. Domains stand on their base with their NOT NULL, composites carry their fields, ranges their subtype and enums their labels, and a schema-qualified type lives in its namespace. A canonicalizer registered per dialect rewrites what only code can, starting with pg_type's _int4 spelling of an array, and is found by name so a catalog restored from the cache has it too. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qryf7a1doPFuCz2eGT3bCr
1 parent 895ed01 commit fedc4a1

17 files changed

Lines changed: 1346 additions & 44 deletions

File tree

‎internal/core/catalog.go‎

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

910
"github.com/sqlc-dev/sqlc/internal/core/catalogdb"
1011
"github.com/sqlc-dev/sqlc/internal/core/catalogdef"
@@ -31,8 +32,14 @@ type Catalog struct {
3132

3233
// types remembers the rows and expressions looked up so far.
3334
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
3439
}
3540

41+
func contextBackground() context.Context { return context.Background() }
42+
3643
type Option func(*Catalog) error
3744

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

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

Lines changed: 17 additions & 0 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ ORDER BY
6666
ns.name
6767
LIMIT 1;
6868

69+
-- name: TypeOIDByNameInNamespace :one
70+
SELECT oid FROM sql_type
71+
WHERE namespace_oid = ? AND name = ? AND family_oid IS NULL;
72+
6973
-- name: TypeOIDByExprInNamespace :one
7074
SELECT oid FROM sql_type WHERE namespace_oid = ? AND expr = ?;
7175

‎internal/core/hooks.go‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package core
2+
3+
import "sync"
4+
5+
// A Canonicalizer rewrites a type expression into the form its engine
6+
// stores and reports: ClickHouse turns Decimal32(4) into Decimal(9, 4) and
7+
// Enum('a', 'b') into Enum8('a' = 1, 'b' = 2), SQL Server turns float(24)
8+
// into real. It sees each expression as a whole before its arguments are
9+
// interned, and again on each argument, so it has to be idempotent. Aliases
10+
// and argument defaults are data in the dialect's seed; a canonicalizer is
11+
// for what only code can say.
12+
type Canonicalizer func(*TypeExpr) *TypeExpr
13+
14+
var (
15+
hooksMu sync.RWMutex
16+
canonicalizers = map[string]Canonicalizer{}
17+
)
18+
19+
// RegisterCanonicalizer installs the canonicalizer for a dialect, by the
20+
// name its dialect.json records. An engine registers its own at init, so
21+
// that a catalog restored from the cache — which runs no seed — finds it by
22+
// the dialect it was seeded with.
23+
func RegisterCanonicalizer(dialect string, fn Canonicalizer) {
24+
hooksMu.Lock()
25+
defer hooksMu.Unlock()
26+
canonicalizers[dialect] = fn
27+
}
28+
29+
// canonicalize applies the catalog's dialect's canonicalizer, if any.
30+
func (c *Catalog) canonicalize(t *TypeExpr) *TypeExpr {
31+
name := c.dialectName()
32+
if name == "" {
33+
return t
34+
}
35+
hooksMu.RLock()
36+
fn := canonicalizers[name]
37+
hooksMu.RUnlock()
38+
if fn == nil {
39+
return t
40+
}
41+
return fn(t)
42+
}
43+
44+
// dialectName is the name of the dialect the catalog was seeded with.
45+
func (c *Catalog) dialectName() string {
46+
if c.dialectOID == 0 {
47+
return ""
48+
}
49+
c.dialectNameOnce.Do(func() {
50+
if row, err := c.q.SeededDialect(contextBackground()); err == nil {
51+
c.dialect = row.Name
52+
}
53+
})
54+
return c.dialect
55+
}

‎internal/core/schema/schema.go‎

Lines changed: 133 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package schema
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/sqlc-dev/sqlc/internal/core"
78
"github.com/sqlc-dev/sqlc/internal/core/analyzer"
@@ -27,6 +28,12 @@ func Apply(cat *core.Catalog, n ast.Node) error {
2728
return applyDropTable(cat, v)
2829
case *ast.CreateEnumStmt:
2930
return applyCreateEnum(cat, v)
31+
case *ast.CreateDomainStmt:
32+
return applyCreateDomain(cat, v)
33+
case *ast.CompositeTypeStmt:
34+
return applyCompositeType(cat, v)
35+
case *ast.CreateRangeStmt:
36+
return applyCreateRange(cat, v)
3037
case *ast.CreateExtensionStmt:
3138
if v.Extname == nil {
3239
return nil
@@ -166,7 +173,7 @@ func applyCreateTable(cat *core.Catalog, stmt *ast.CreateTableStmt) error {
166173
Name: col.Colname,
167174
TypeOID: typeOID,
168175
Num: i + 1,
169-
NotNull: col.IsNotNull || col.PrimaryKey,
176+
NotNull: col.IsNotNull || col.PrimaryKey || typeNotNull(cat, typeOID),
170177
IsPrimaryKey: col.PrimaryKey,
171178
DeclType: declType(col.TypeName),
172179
Hidden: col.IsHidden,
@@ -238,7 +245,7 @@ func applyAlterTable(cat *core.Catalog, stmt *ast.AlterTableStmt) error {
238245
Name: cmd.Def.Colname,
239246
TypeOID: typeOID,
240247
Num: num,
241-
NotNull: cmd.Def.IsNotNull || cmd.Def.PrimaryKey,
248+
NotNull: cmd.Def.IsNotNull || cmd.Def.PrimaryKey || typeNotNull(cat, typeOID),
242249
IsPrimaryKey: cmd.Def.PrimaryKey,
243250
DeclType: declType(cmd.Def.TypeName),
244251
}); err != nil {
@@ -350,21 +357,134 @@ func listItems(l *ast.List) []ast.Node {
350357
return l.Items
351358
}
352359

360+
// applyCreateEnum records an enum as a type whose arguments are its labels,
361+
// in order, the way pg_enum keeps them.
353362
func applyCreateEnum(cat *core.Catalog, stmt *ast.CreateEnumStmt) error {
354363
if stmt.TypeName == nil {
355364
return fmt.Errorf("create type with nil name")
356365
}
357-
t := core.TypeExprOfTypeName(stmt.TypeName)
358-
if t == nil {
366+
name := declaredTypeName(stmt.TypeName)
367+
if name == "" {
359368
return fmt.Errorf("create type with empty name")
360369
}
361-
if _, err := cat.TypeOID(t.Name); err == nil {
370+
if _, err := cat.TypeOID(name); err == nil {
371+
return nil
372+
}
373+
var labels []core.TypeArg
374+
for _, label := range listStrings(stmt.Vals) {
375+
l := label
376+
labels = append(labels, core.TypeArg{String: &l})
377+
}
378+
_, err := cat.CreateTypeWithArgs(core.TypeSpec{Name: name, Typtype: "e", Category: "E"}, labels)
379+
return err
380+
}
381+
382+
// applyCreateDomain records a domain: a type of its own that stands on its
383+
// base, which is what it resolves through, and that may forbid NULL.
384+
func applyCreateDomain(cat *core.Catalog, stmt *ast.CreateDomainStmt) error {
385+
name := strings.ToLower(strings.Join(listStrings(stmt.Domainname), "."))
386+
if name == "" || stmt.TypeName == nil {
387+
return fmt.Errorf("create domain: missing name or type")
388+
}
389+
if _, err := cat.TypeOID(name); err == nil {
362390
return nil
363391
}
364-
_, err := cat.CreateUserType(t.Name, "E")
392+
baseOID, err := cat.ResolveType(stmt.TypeName)
393+
if err != nil {
394+
return fmt.Errorf("domain %q: %w", name, err)
395+
}
396+
base, err := cat.LookupType(baseOID)
397+
if err != nil {
398+
return err
399+
}
400+
notNull := false
401+
for _, item := range listItems(stmt.Constraints) {
402+
if con, ok := item.(*ast.Constraint); ok && con.Contype == ast.ConstrTypeNotNull {
403+
notNull = true
404+
}
405+
}
406+
_, err = cat.CreateTypeWithArgs(core.TypeSpec{
407+
Name: name,
408+
Typtype: "d",
409+
Category: base.Category,
410+
BaseOID: baseOID,
411+
NotNull: notNull,
412+
}, nil)
365413
return err
366414
}
367415

416+
// applyCompositeType records a composite type as a type whose arguments are
417+
// its fields, labelled by name.
418+
func applyCompositeType(cat *core.Catalog, stmt *ast.CompositeTypeStmt) error {
419+
if stmt.TypeName == nil {
420+
return fmt.Errorf("create type with nil name")
421+
}
422+
name := declaredTypeName(stmt.TypeName)
423+
if name == "" {
424+
return fmt.Errorf("create type with empty name")
425+
}
426+
if _, err := cat.TypeOID(name); err == nil {
427+
return nil
428+
}
429+
var fields []core.TypeArg
430+
for _, item := range listItems(stmt.Coldeflist) {
431+
col, ok := item.(*ast.ColumnDef)
432+
if !ok || col.TypeName == nil {
433+
continue
434+
}
435+
t := core.ColumnTypeExpr(col)
436+
if t == nil {
437+
continue
438+
}
439+
fields = append(fields, core.TypeArg{Label: col.Colname, Type: t})
440+
}
441+
_, err := cat.CreateTypeWithArgs(core.TypeSpec{Name: name, Typtype: "c", Category: "C"}, fields)
442+
return err
443+
}
444+
445+
// applyCreateRange records a range type over its subtype, which is what a
446+
// bound of it has.
447+
func applyCreateRange(cat *core.Catalog, stmt *ast.CreateRangeStmt) error {
448+
name := strings.ToLower(strings.Join(listStrings(stmt.TypeName), "."))
449+
if name == "" {
450+
return fmt.Errorf("create type with empty name")
451+
}
452+
if _, err := cat.TypeOID(name); err == nil {
453+
return nil
454+
}
455+
spec := core.TypeSpec{Name: name, Typtype: "r", Category: "R"}
456+
for _, item := range listItems(stmt.Params) {
457+
def, ok := item.(*ast.DefElem)
458+
if !ok || def.Defname == nil || *def.Defname != "subtype" {
459+
continue
460+
}
461+
tn, ok := def.Arg.(*ast.TypeName)
462+
if !ok {
463+
continue
464+
}
465+
oid, err := cat.ResolveType(tn)
466+
if err != nil {
467+
return fmt.Errorf("range %q: %w", name, err)
468+
}
469+
spec.ElementOID = oid
470+
}
471+
_, err := cat.CreateTypeWithArgs(spec, nil)
472+
return err
473+
}
474+
475+
// declaredTypeName is the name a CREATE TYPE gives, qualified by its schema
476+
// when it names one.
477+
func declaredTypeName(tn *ast.TypeName) string {
478+
t := core.TypeExprOfTypeName(tn)
479+
if t == nil {
480+
return ""
481+
}
482+
if tn.Schema != "" && !strings.Contains(t.Name, ".") {
483+
return strings.ToLower(tn.Schema) + "." + t.Name
484+
}
485+
return t.Name
486+
}
487+
368488
func applyCreateFunction(cat *core.Catalog, stmt *ast.CreateFunctionStmt) error {
369489
// A procedure returns nothing, so there is no result for a query to
370490
// select and nothing worth recording.
@@ -421,6 +541,13 @@ func resolveOrCreateNamespace(cat *core.Catalog, schema string) (int64, error) {
421541
return cat.CreateNamespace(name)
422542
}
423543

544+
// typeNotNull reports whether a column of the type can never be NULL
545+
// because the type itself says so, as a domain declared NOT NULL does.
546+
func typeNotNull(cat *core.Catalog, typeOID int64) bool {
547+
info, err := cat.LookupType(typeOID)
548+
return err == nil && info.NotNull
549+
}
550+
424551
// columnTypeOID interns a column's type and returns its row.
425552
func columnTypeOID(cat *core.Catalog, col *ast.ColumnDef) (int64, error) {
426553
t := core.ColumnTypeExpr(col)

‎internal/core/typename.go‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,26 @@ func ColumnTypeExpr(col *ast.ColumnDef) *TypeExpr {
7474
return t
7575
}
7676

77-
// typmodArg reads one type modifier as an argument: an integer, a string, or
78-
// a bare word, which is an identifier such as the max of nvarchar(max).
77+
// typmodArg reads one type modifier as an argument: an integer, a quoted
78+
// string, or a bare word, which is an identifier such as the max of
79+
// nvarchar(max) or the day to second of an interval. A constant node holds
80+
// a literal; a bare String node holds a word.
7981
func typmodArg(n ast.Node) (TypeArg, bool) {
8082
switch v := n.(type) {
8183
case *ast.A_Const:
82-
return typmodArg(v.Val)
84+
switch val := v.Val.(type) {
85+
case *ast.String:
86+
s := val.Str
87+
return TypeArg{String: &s}, true
88+
default:
89+
return typmodArg(v.Val)
90+
}
8391
case *ast.Integer:
8492
i := v.Ival
8593
return TypeArg{Int: &i}, true
8694
case *ast.String:
87-
s := v.Str
88-
return TypeArg{String: &s}, true
95+
s := strings.ToLower(v.Str)
96+
return TypeArg{Ident: &s}, true
8997
case *ast.ColumnRef:
9098
parts := make([]string, 0, len(listItems(v.Fields)))
9199
for _, item := range listItems(v.Fields) {

0 commit comments

Comments
 (0)