Skip to content

Commit 85b92aa

Browse files
committed
core: a type in another namespace resolves there and is reported qualified
A column declared myschema.mood resolves in that namespace rather than by its bare name, and a type outside the default namespaces is reported as myschema.mood, the way format_type prints a type off the search path. A dialect names its own default schema in dialect.json, so SQL Server's dbo and DuckDB's main report unqualified. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qryf7a1doPFuCz2eGT3bCr
1 parent b0e1cf0 commit 85b92aa

8 files changed

Lines changed: 81 additions & 6 deletions

File tree

‎internal/core/dialect.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ const FlagPropagateNullable = "functions.propagate_nullable"
7777
// e.id.
7878
const FlagQualifyDuplicateColumns = "columns.qualify_duplicates"
7979

80+
// FlagDefaultSchema holds the schema the dialect puts an unqualified object
81+
// in, when that is not the catalog's own default: a type in it is reported
82+
// without its schema.
83+
const FlagDefaultSchema = "schema.default"
84+
8085
// FlagCastCategories holds the categories whose types are all implicitly
8186
// castable to one another, as the dialect's seed declared them, so that a type
8287
// arriving after the seed — an extension's, say — can join its category.
@@ -190,3 +195,16 @@ func (c *Catalog) QualifiesDuplicateColumns() bool {
190195
v, _ := c.DialectFlag(c.dialectOID, FlagQualifyDuplicateColumns)
191196
return v == "true"
192197
}
198+
199+
// DefaultNamespaces lists the namespaces a type is reported from without
200+
// qualification: the catalog's default, PostgreSQL's system catalog, and
201+
// the dialect's own default schema when it names one.
202+
func (c *Catalog) DefaultNamespaces() []string {
203+
out := []string{"public", "pg_catalog"}
204+
if c.dialectOID != 0 {
205+
if name, _ := c.DialectFlag(c.dialectOID, FlagDefaultSchema); name != "" {
206+
out = append(out, name)
207+
}
208+
}
209+
return out
210+
}

‎internal/core/seed/seed.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ type Settings struct {
105105
// enable_fts5 compile option adds.
106106
Modules map[string]string `json:"modules,omitempty"`
107107

108+
// DefaultSchema names the schema a dialect puts an unqualified object
109+
// in when it is not the catalog's own default: SQL Server's dbo,
110+
// DuckDB's main. A type in it is reported unqualified.
111+
DefaultSchema string `json:"default_schema,omitempty"`
112+
108113
// Alias says what an alias in types.jsonl is. "canonical", the default,
109114
// makes it another spelling of the type, which a column declared with
110115
// it is reported as, the way PostgreSQL reports int as integer. "base"
@@ -556,6 +561,11 @@ func (b *builder) consts() error {
556561
return err
557562
}
558563
}
564+
if b.settings.DefaultSchema != "" {
565+
if err := b.cat.SetDialectFlag(b.dialectOID, core.FlagDefaultSchema, strings.ToLower(b.settings.DefaultSchema)); err != nil {
566+
return err
567+
}
568+
}
559569
for key, name := range map[string]string{
560570
core.FlagBoolType: b.settings.Bool,
561571
core.FlagLimitType: b.settings.Limit,

‎internal/core/typename.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ func TypeExprOfTypeName(tn *ast.TypeName) *TypeExpr {
3737
if name == "" {
3838
return nil
3939
}
40+
// An engine that reports the schema apart from the name qualifies it
41+
// the way a dotted name does, so the type resolves in its namespace.
42+
if schema := strings.ToLower(tn.Schema); schema != "" && schema != "pg_catalog" && !strings.Contains(name, ".") {
43+
name = schema + "." + name
44+
}
4045
// A name an engine spelled with its own arguments or array suffix reads
4146
// the same way a spelling does.
4247
t := ParseTypeExpr(name)

‎internal/core/types.go‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"database/sql"
66
"errors"
77
"fmt"
8+
"slices"
89
"strings"
910
"sync"
1011

@@ -53,9 +54,26 @@ func (t TypeInfo) IsFamily() bool { return t.FamilyOID == 0 }
5354
// answer is good for the life of the catalog. Analysis runs concurrently on
5455
// a restored catalog, so the cache is locked.
5556
type typeCache struct {
56-
mu sync.RWMutex
57-
infos map[int64]TypeInfo
58-
exprs map[int64]*TypeExpr
57+
mu sync.RWMutex
58+
infos map[int64]TypeInfo
59+
exprs map[int64]*TypeExpr
60+
namespaces map[int64]string
61+
}
62+
63+
func (c *typeCache) namespace(oid int64) (string, bool) {
64+
c.mu.RLock()
65+
defer c.mu.RUnlock()
66+
name, ok := c.namespaces[oid]
67+
return name, ok
68+
}
69+
70+
func (c *typeCache) putNamespace(oid int64, name string) {
71+
c.mu.Lock()
72+
defer c.mu.Unlock()
73+
if c.namespaces == nil {
74+
c.namespaces = map[int64]string{}
75+
}
76+
c.namespaces[oid] = name
5977
}
6078

6179
func (c *typeCache) info(oid int64) (TypeInfo, bool) {
@@ -425,6 +443,22 @@ func (c *Catalog) LookupType(oid int64) (TypeInfo, error) {
425443
return info, nil
426444
}
427445

446+
// namespaceName is the name of a namespace row, remembered once read.
447+
func (c *Catalog) namespaceName(oid int64) (string, error) {
448+
if name, ok := c.types.namespace(oid); ok {
449+
return name, nil
450+
}
451+
namespaces, err := c.Namespaces()
452+
if err != nil {
453+
return "", err
454+
}
455+
for _, ns := range namespaces {
456+
c.types.putNamespace(ns.OID, ns.Name)
457+
}
458+
name, _ := c.types.namespace(oid)
459+
return name, nil
460+
}
461+
428462
// TypeExprOf is the expression a type row stands for, read back from its
429463
// arguments: the family's name for a family, the family applied to its
430464
// arguments for an instance. The result is the caller's to change.
@@ -437,6 +471,11 @@ func (c *Catalog) TypeExprOf(oid int64) (*TypeExpr, error) {
437471
return nil, err
438472
}
439473
expr := &TypeExpr{Name: info.Name}
474+
// A type outside the default namespaces is named with its namespace,
475+
// as format_type prints a type off the search path.
476+
if ns, err := c.namespaceName(info.NamespaceOID); err == nil && ns != "" && !slices.Contains(c.DefaultNamespaces(), ns) {
477+
expr.Name = ns + "." + info.Name
478+
}
440479
if !info.IsFamily() {
441480
rows, err := c.q.TypeArgs(context.Background(), oid)
442481
if err != nil {

‎internal/endtoend/testdata/analyze_types/postgresql/stdout.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@
250250
{
251251
"name": "mm",
252252
"type": {
253-
"name": "mood",
253+
"name": "myschema.mood",
254254
"nullable": true
255255
},
256256
"table": "things"
@@ -509,7 +509,7 @@
509509
{
510510
"name": "k",
511511
"type": {
512-
"name": "mood"
512+
"name": "myschema.mood"
513513
}
514514
}
515515
],
@@ -624,7 +624,7 @@
624624
"column": {
625625
"name": "",
626626
"type": {
627-
"name": "mood"
627+
"name": "myschema.mood"
628628
}
629629
}
630630
}

‎internal/engine/clickhouse/dialect/dialect.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"dialect": "clickhouse",
3+
"default_schema": "default",
34
"const": {
45
"integer": "Int64",
56
"float": "Float64",

‎internal/engine/duckdb/dialect/dialect.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"dialect": "duckdb",
3+
"default_schema": "main",
34
"const": {
45
"integer": "integer",
56
"float": "double",

‎internal/engine/mssql/dialect/dialect.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"dialect": "mssql",
3+
"default_schema": "dbo",
34
"const": {
45
"integer": "int",
56
"float": "float",

0 commit comments

Comments
 (0)