Skip to content

Commit b429383

Browse files
EliSauderCopilot
andcommitted
fix(postgresql): carry NOT NULL from the domain a domain stands on
A domain over a NOT NULL domain forbids NULL too, but only the domain's own constraint made its columns NOT NULL. Follow the domain's base while it is a domain, stopping at an array, whose elements' constraint does not apply to the array itself. Addresses review comment r4556992619 on #2. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent bc5a65b commit b429383

5 files changed

Lines changed: 57 additions & 18 deletions

File tree

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

Lines changed: 12 additions & 10 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: 3 additions & 1 deletion
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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ CREATE DOMAIN billing.ksuid AS char(27);
77
CREATE DOMAIN billing.text AS integer;
88
CREATE DOMAIN relaxed AS text NOT NULL;
99
CREATE DOMAIN tightened AS text;
10+
-- A domain carries the NOT NULL of the domain it stands on, an array
11+
-- of that domain does not.
12+
CREATE DOMAIN required_text AS text NOT NULL;
13+
CREATE DOMAIN derived AS required_text;
14+
CREATE DOMAIN derived_list AS required_text[];
1015
CREATE DOMAIN dropped AS text;
1116
DROP DOMAIN dropped;
1217

@@ -27,7 +32,9 @@ CREATE TABLE shelves (
2732
retyped title,
2833
strict title NOT NULL,
2934
relaxed relaxed,
30-
tightened tightened
35+
tightened tightened,
36+
derived derived,
37+
derived_list derived_list
3138
);
3239

3340
-- The domain's NOT NULL leaves with it, and outlasts the column's own.

‎internal/sql/catalog/table.go‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ func (c *Catalog) defineColumn(table *ast.TableName, col *ast.ColumnDef) (*Colum
379379
return tc, nil
380380
}
381381

382-
// setDomainNotNull makes col NOT NULL when its type is a domain declared
383-
// NOT NULL, which forbids NULL in every column of it, and drops what an
382+
// setDomainNotNull makes col NOT NULL when its type is a NOT NULL domain,
383+
// which forbids NULL in every column of it, and drops what an
384384
// earlier type gave it. An array of one may still be NULL itself.
385385
func (c *Catalog) setDomainNotNull(col *Column) {
386386
if col.domainNotNull {
@@ -389,10 +389,8 @@ func (c *Catalog) setDomainNotNull(col *Column) {
389389
if col.IsNotNull || col.IsArray {
390390
return
391391
}
392-
if typ, _, err := c.getType(&col.Type); err == nil {
393-
if d, ok := typ.(*Domain); ok && d.NotNull {
394-
col.IsNotNull, col.domainNotNull = true, true
395-
}
392+
if c.domainNotNull(&col.Type) {
393+
col.IsNotNull, col.domainNotNull = true, true
396394
}
397395
}
398396

‎internal/sql/catalog/types.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ type Domain struct {
4747
Base *core.TypeExpr
4848
NotNull bool
4949
Comment string
50+
51+
// base names the type the domain stands on, unless it is an array,
52+
// so a domain over a NOT NULL domain is NOT NULL too.
53+
base *ast.TypeName
5054
}
5155

5256
func (d *Domain) isType() {
@@ -128,6 +132,9 @@ func (c *Catalog) createDomain(stmt *ast.CreateDomainStmt) error {
128132
Name: name.Name,
129133
Base: core.TypeExprOfTypeName(stmt.TypeName),
130134
}
135+
if !isArrayTypeName(stmt.TypeName) {
136+
domain.base = domainName(stmt.TypeName.Names)
137+
}
131138
if stmt.Constraints != nil {
132139
for _, item := range stmt.Constraints.Items {
133140
if con, ok := item.(*ast.Constraint); ok && con.Contype == ast.ConstrTypeNotNull {
@@ -139,6 +146,29 @@ func (c *Catalog) createDomain(stmt *ast.CreateDomainStmt) error {
139146
return nil
140147
}
141148

149+
func isArrayTypeName(tn *ast.TypeName) bool {
150+
return tn.ArrayBounds != nil && len(tn.ArrayBounds.Items) > 0
151+
}
152+
153+
// domainNotNull reports whether tn is a domain that is NOT NULL, declared
154+
// so or standing on one that is.
155+
func (c *Catalog) domainNotNull(tn *ast.TypeName) bool {
156+
seen := map[*Domain]bool{}
157+
for tn != nil {
158+
typ, _, err := c.getType(tn)
159+
d, ok := typ.(*Domain)
160+
if err != nil || !ok || seen[d] {
161+
return false
162+
}
163+
if d.NotNull {
164+
return true
165+
}
166+
seen[d] = true
167+
tn = d.base
168+
}
169+
return false
170+
}
171+
142172
func domainName(list *ast.List) *ast.TypeName {
143173
names := stringSlice(list)
144174
if len(names) == 0 {

0 commit comments

Comments
 (0)