1010// operators.jsonl operator overloads beyond the ones the rules generate
1111// casts.jsonl casts beyond the ones the rules generate
1212// functions.jsonl the functions it ships with
13+ // relations.jsonl the system tables and views it ships with
1314//
1415// The lists are JSONL — one record per line — and are applied as they are
1516// read, so a dialect whose function list runs to thousands of entries is never
1617// held in memory as a whole. Any of the lists may be left out.
18+ //
19+ // Relations are the exception to "applied as they are read": they are loaded
20+ // the first time a query names a schema the catalog does not yet have, since
21+ // most queries never touch a system catalog.
1722package seed
1823
1924import (
@@ -38,6 +43,7 @@ const (
3843 OperatorsFile = "operators.jsonl"
3944 CastsFile = "casts.jsonl"
4045 FunctionsFile = "functions.jsonl"
46+ RelationsFile = "relations.jsonl"
4147)
4248
4349// Settings is dialect.json: what the dialect is called and the rules that
@@ -105,6 +111,28 @@ type Function struct {
105111 Nullable bool `json:"nullable,omitempty"`
106112}
107113
114+ // Relation is a table or view the dialect ships with, such as one of
115+ // PostgreSQL's system catalogs. Kind is 'r' for a table or 'v' for a view, and
116+ // defaults to a table.
117+ type Relation struct {
118+ // Catalog is the database the relation belongs to, which PostgreSQL
119+ // reports for its own schemas and which the legacy catalog carries.
120+ Catalog string `json:"catalog,omitempty"`
121+ Schema string `json:"schema"`
122+ Name string `json:"name"`
123+ Kind string `json:"kind,omitempty"`
124+ Columns []Column `json:"columns"`
125+ }
126+
127+ // Column is one of a relation's columns.
128+ type Column struct {
129+ Name string `json:"name"`
130+ Type string `json:"type"`
131+ NotNull bool `json:"not_null,omitempty"`
132+ Array bool `json:"array,omitempty"`
133+ Length int `json:"length,omitempty"`
134+ }
135+
108136// Arg is one of a function's parameters. Mode is 'i'n, 'o'ut, 'b'oth, 't'able
109137// or 'v'ariadic, and defaults to in.
110138type Arg struct {
@@ -132,10 +160,11 @@ func apply(cat *core.Catalog, fsys fs.FS) error {
132160 return err
133161 }
134162 b := & builder {
135- cat : cat ,
136- settings : settings ,
137- oids : map [string ]int64 {},
138- seenCasts : map [[2 ]int64 ]bool {},
163+ cat : cat ,
164+ settings : settings ,
165+ oids : map [string ]int64 {},
166+ namespaces : map [string ]int64 {},
167+ seenCasts : map [[2 ]int64 ]bool {},
139168 }
140169 if b .dialectOID , err = cat .CreateDialect (settings .Dialect ); err != nil {
141170 return err
@@ -160,7 +189,16 @@ func apply(cat *core.Catalog, fsys fs.FS) error {
160189 if err := b .categoryCasts (); err != nil {
161190 return err
162191 }
163- return stream (fsys , FunctionsFile , b .addFunction )
192+ if err := stream (fsys , FunctionsFile , b .addFunction ); err != nil {
193+ return err
194+ }
195+
196+ // A dialect's system catalogs are thousands of columns that a query only
197+ // occasionally names, so they wait until one does.
198+ cat .SeedLater (func (* core.Catalog ) error {
199+ return stream (fsys , RelationsFile , b .addRelation )
200+ })
201+ return nil
164202}
165203
166204func loadSettings (fsys fs.FS ) (Settings , error ) {
@@ -245,6 +283,44 @@ func Functions(fsys fs.FS, dir string) ([]*catalog.Function, error) {
245283 return out , nil
246284}
247285
286+ // Relations streams the relations a dialect ships with in the named schema
287+ // into the form the engine catalogs use.
288+ func Relations (fsys fs.FS , dir , schema string ) ([]* catalog.Table , error ) {
289+ sub , err := fs .Sub (fsys , dir )
290+ if err != nil {
291+ return nil , fmt .Errorf ("seed: %s: %w" , dir , err )
292+ }
293+ var out []* catalog.Table
294+ err = stream (sub , RelationsFile , func (rel Relation ) error {
295+ if rel .Schema != schema {
296+ return nil
297+ }
298+ table := & catalog.Table {
299+ Rel : & ast.TableName {Catalog : rel .Catalog , Schema : rel .Schema , Name : rel .Name },
300+ Columns : make ([]* catalog.Column , 0 , len (rel .Columns )),
301+ }
302+ for _ , col := range rel .Columns {
303+ column := & catalog.Column {
304+ Name : col .Name ,
305+ Type : ast.TypeName {Name : col .Type },
306+ IsNotNull : col .NotNull ,
307+ IsArray : col .Array ,
308+ }
309+ if col .Length > 0 {
310+ length := col .Length
311+ column .Length = & length
312+ }
313+ table .Columns = append (table .Columns , column )
314+ }
315+ out = append (out , table )
316+ return nil
317+ })
318+ if err != nil {
319+ return nil , err
320+ }
321+ return out , nil
322+ }
323+
248324// argModes maps the mode a record carries to the one the catalog uses. The
249325// letters are PostgreSQL's, which is where the generated function lists come
250326// from.
@@ -285,6 +361,10 @@ type builder struct {
285361 oids map [string ]int64
286362 categories []categorized
287363
364+ // namespaces maps a schema name to its OID, so that a run of relations in
365+ // the same schema resolves it once.
366+ namespaces map [string ]int64
367+
288368 // seenCasts records the pairs already registered. The alias rules and the
289369 // category rules overlap, and a cast pair is unique in the catalog.
290370 seenCasts map [[2 ]int64 ]bool
@@ -509,6 +589,80 @@ func (b *builder) addFunction(fn Function) error {
509589 return nil
510590}
511591
592+ func (b * builder ) addRelation (rel Relation ) error {
593+ if rel .Name == "" {
594+ return errors .New ("relation has no name" )
595+ }
596+ nsOID , err := b .namespace (rel .Schema )
597+ if err != nil {
598+ return err
599+ }
600+ kind := rel .Kind
601+ if kind == "" {
602+ kind = "r"
603+ }
604+ classOID , err := b .cat .CreateClass (nsOID , rel .Name , kind )
605+ if err != nil {
606+ return err
607+ }
608+ for i , col := range rel .Columns {
609+ name := col .Type
610+ if col .Array {
611+ name += core .ArraySuffix
612+ }
613+ typeOID , err := b .columnType (name )
614+ if err != nil {
615+ return fmt .Errorf ("relation %q column %q: %w" , rel .Name , col .Name , err )
616+ }
617+ if err := b .cat .CreateAttributeSpec (core.AttributeSpec {
618+ ClassOID : classOID ,
619+ Name : col .Name ,
620+ TypeOID : typeOID ,
621+ Num : i + 1 ,
622+ NotNull : col .NotNull ,
623+ DeclType : col .Type ,
624+ TypeLength : col .Length ,
625+ }); err != nil {
626+ return fmt .Errorf ("relation %q column %q: %w" , rel .Name , col .Name , err )
627+ }
628+ }
629+ return nil
630+ }
631+
632+ // namespace resolves the schema a relation belongs to, creating it the first
633+ // time it is named. A relation with no schema belongs to the default one.
634+ func (b * builder ) namespace (schema string ) (int64 , error ) {
635+ if schema == "" {
636+ schema = "public"
637+ }
638+ if oid , ok := b .namespaces [schema ]; ok {
639+ return oid , nil
640+ }
641+ oid , err := b .cat .NamespaceOID (schema )
642+ if err != nil {
643+ if oid , err = b .cat .CreateNamespace (schema ); err != nil {
644+ return 0 , err
645+ }
646+ }
647+ b .namespaces [schema ] = oid
648+ return oid , nil
649+ }
650+
651+ // columnType resolves a column's type, which unlike a function signature may
652+ // name an array.
653+ func (b * builder ) columnType (name string ) (int64 , error ) {
654+ key := strings .ToLower (name )
655+ if oid , ok := b .oids [key ]; ok {
656+ return oid , nil
657+ }
658+ oid , err := b .cat .ResolveTypeName (key )
659+ if err != nil {
660+ return 0 , err
661+ }
662+ b .oids [key ] = oid
663+ return oid , nil
664+ }
665+
512666// funcType resolves the type a function signature names. Signatures reference
513667// pseudo types ("any", "record") and types no dialect bothers to list, so an
514668// unknown name is registered rather than rejected.
0 commit comments