Skip to content

Commit 799ef80

Browse files
EliSauderCopilot
andcommitted
fix(golang): look bare domain names up in the default schemas only
A type outside the default namespaces is reported qualified, so a bare name falling back to any schema made billing.text stand in for every ordinary text column, and recursed forever when its base was text. A domain whose base reports its own bare name stops there too. Addresses review comment r4556992324 on #2. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 13b8ca8 commit 799ef80

4 files changed

Lines changed: 42 additions & 18 deletions

File tree

‎internal/codegen/golang/domain_type.go‎

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package golang
22

33
import (
4+
"slices"
45
"strings"
56

67
"google.golang.org/protobuf/proto"
@@ -22,7 +23,7 @@ func domainColumn(req *plugin.GenerateRequest, col *plugin.Column) *plugin.Colum
2223
return out
2324
}
2425
d := findDomain(req, col.Type.GetSchema(), col.Type.GetName())
25-
if d == nil || d.Base == nil {
26+
if baseless(req, d) {
2627
return nil
2728
}
2829
out := proto.Clone(col).(*plugin.Column)
@@ -33,7 +34,7 @@ func domainColumn(req *plugin.GenerateRequest, col *plugin.Column) *plugin.Colum
3334
// resolveDomains replaces each domain in t with its base, keeping the
3435
// nullability t says each position has.
3536
func resolveDomains(req *plugin.GenerateRequest, t *plugin.TypeExpr) (*plugin.TypeExpr, bool) {
36-
if d := findDomain(req, "", t.GetName()); d != nil && d.Base != nil {
37+
if d := findDomain(req, "", t.GetName()); !baseless(req, d) {
3738
base := proto.Clone(d.Base).(*plugin.TypeExpr)
3839
base.Nullable = t.Nullable
3940
if resolved, ok := resolveDomains(req, base); ok {
@@ -55,9 +56,17 @@ func resolveDomains(req *plugin.GenerateRequest, t *plugin.TypeExpr) (*plugin.Ty
5556
return out, out != nil
5657
}
5758

59+
// baseless reports whether d is missing or has no other type to resolve to:
60+
// public.text over text reports its base as text, which a bare lookup
61+
// finds as the domain again rather than the built-in it is.
62+
func baseless(req *plugin.GenerateRequest, d *plugin.Domain) bool {
63+
return d == nil || d.Base == nil || findDomain(req, "", d.Base.GetName()) == d
64+
}
65+
5866
// findDomain looks a domain up by the name a column's type reports: a
59-
// qualified name in its schema, and a bare one in the default schema
60-
// first, as the type would resolve.
67+
// qualified name in its schema, and a bare one only in the namespaces a
68+
// type is reported from unqualified, since one elsewhere is reported
69+
// qualified and so is a different type.
6170
func findDomain(req *plugin.GenerateRequest, schema, name string) *plugin.Domain {
6271
if name == "" {
6372
return nil
@@ -67,21 +76,27 @@ func findDomain(req *plugin.GenerateRequest, schema, name string) *plugin.Domain
6776
schema, name = name[:i], name[i+1:]
6877
}
6978
}
70-
var fallback *plugin.Domain
79+
schemas := []string{schema}
80+
if schema == "" {
81+
schemas = []string{req.GetCatalog().GetDefaultSchema(), "pg_catalog", engineDefaultSchema[req.GetSettings().GetEngine()]}
82+
}
7183
for _, s := range req.GetCatalog().GetSchemas() {
84+
if !slices.Contains(schemas, s.Name) {
85+
continue
86+
}
7287
for _, d := range s.GetDomains() {
73-
if d.Name != name {
74-
continue
75-
}
76-
switch {
77-
case schema != "" && s.Name == schema:
78-
return d
79-
case schema == "" && s.Name == req.Catalog.DefaultSchema:
88+
if d.Name == name {
8089
return d
81-
case schema == "" && fallback == nil:
82-
fallback = d
8390
}
8491
}
8592
}
86-
return fallback
93+
return nil
94+
}
95+
96+
// engineDefaultSchema is the schema an engine puts an unqualified object
97+
// in when it is not the catalog's default, as its dialect seed names it.
98+
var engineDefaultSchema = map[string]string{
99+
engineClickHouse: "default",
100+
engineDuckDB: "main",
101+
engineMSSQL: "dbo",
87102
}

‎internal/endtoend/testdata/domain_types/postgresql/go/models.go‎

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

‎internal/endtoend/testdata/domain_types/postgresql/go/query.sql.go‎

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

‎internal/endtoend/testdata/domain_types/postgresql/schema.sql‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ CREATE DOMAIN title AS varchar(100) NOT NULL DEFAULT '';
22
CREATE DOMAIN price AS numeric(10, 2) CHECK (VALUE >= 0);
33
CREATE SCHEMA billing;
44
CREATE DOMAIN billing.ksuid AS char(27);
5+
-- Named like a built-in, but off the search path.
6+
CREATE DOMAIN billing.text AS integer;
57
CREATE DOMAIN dropped AS text;
68
DROP DOMAIN dropped;
79

@@ -15,5 +17,6 @@ CREATE TABLE books (
1517
CREATE TABLE shelves (
1618
id serial PRIMARY KEY,
1719
titles title[] NOT NULL,
18-
label title
20+
label title,
21+
note text
1922
);

0 commit comments

Comments
 (0)