@@ -59,7 +59,7 @@ func (c *Catalog) addColumn(table *Table, cmd *ast.AlterTableCmd) error {
5959 return nil
6060}
6161
62- func (table * Table ) alterColumnType (cmd * ast.AlterTableCmd ) error {
62+ func (c * Catalog ) alterColumnType (table * Table , cmd * ast.AlterTableCmd ) error {
6363 index , err := table .isExistColumn (cmd )
6464 if err != nil {
6565 return err
@@ -68,6 +68,7 @@ func (table *Table) alterColumnType(cmd *ast.AlterTableCmd) error {
6868 table .Columns [index ].Type = * cmd .Def .TypeName
6969 table .Columns [index ].IsArray = cmd .Def .IsArray
7070 table .Columns [index ].ArrayDims = cmd .Def .ArrayDims
71+ c .setDomainNotNull (table .Columns [index ])
7172 }
7273 return nil
7374}
@@ -95,13 +96,15 @@ func (c *Catalog) dropColumn(table *Table, cmd *ast.AlterTableCmd) error {
9596 return nil
9697}
9798
98- func (table * Table ) dropNotNull (cmd * ast.AlterTableCmd ) error {
99+ func (c * Catalog ) dropNotNull (table * Table , cmd * ast.AlterTableCmd ) error {
99100 index , err := table .isExistColumn (cmd )
100101 if err != nil {
101102 return err
102103 }
103104 if index >= 0 {
104105 table .Columns [index ].IsNotNull = false
106+ table .Columns [index ].domainNotNull = false
107+ c .setDomainNotNull (table .Columns [index ])
105108 }
106109 return nil
107110}
@@ -113,6 +116,7 @@ func (table *Table) setNotNull(cmd *ast.AlterTableCmd) error {
113116 }
114117 if index >= 0 {
115118 table .Columns [index ].IsNotNull = true
119+ table .Columns [index ].domainNotNull = false
116120 }
117121 return nil
118122}
@@ -136,6 +140,9 @@ type Column struct {
136140 TypeExpr * core.TypeExpr
137141
138142 linkedType bool
143+ // domainNotNull says IsNotNull comes from the column's domain alone,
144+ // so a change to the type or the domain takes it back.
145+ domainNotNull bool
139146}
140147
141148// An interface is used to resolve a circular import between the catalog and compiler packages.
@@ -206,15 +213,15 @@ func (c *Catalog) alterTable(stmt *ast.AlterTableStmt) error {
206213 return err
207214 }
208215 case ast .AT_AlterColumnType :
209- if err := table .alterColumnType (cmd ); err != nil {
216+ if err := c .alterColumnType (table , cmd ); err != nil {
210217 return err
211218 }
212219 case ast .AT_DropColumn :
213220 if err := c .dropColumn (table , cmd ); err != nil {
214221 return err
215222 }
216223 case ast .AT_DropNotNull :
217- if err := table .dropNotNull (cmd ); err != nil {
224+ if err := c .dropNotNull (table , cmd ); err != nil {
218225 return err
219226 }
220227 case ast .AT_SetNotNull :
@@ -357,15 +364,7 @@ func (c *Catalog) defineColumn(table *ast.TableName, col *ast.ColumnDef) (*Colum
357364 Comment : col .Comment ,
358365 Length : col .Length ,
359366 }
360- // A domain declared NOT NULL forbids NULL in every column of it. An
361- // array of one may still be NULL itself.
362- if ! col .IsArray && col .TypeName != nil {
363- if typ , _ , err := c .getType (col .TypeName ); err == nil {
364- if d , ok := typ .(* Domain ); ok && d .NotNull {
365- tc .IsNotNull = true
366- }
367- }
368- }
367+ c .setDomainNotNull (tc )
369368 if col .Vals != nil {
370369 typeName := ast.TypeName {
371370 Name : fmt .Sprintf ("%s_%s" , table .Name , col .Colname ),
@@ -380,6 +379,23 @@ func (c *Catalog) defineColumn(table *ast.TableName, col *ast.ColumnDef) (*Colum
380379 return tc , nil
381380}
382381
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
384+ // earlier type gave it. An array of one may still be NULL itself.
385+ func (c * Catalog ) setDomainNotNull (col * Column ) {
386+ if col .domainNotNull {
387+ col .IsNotNull , col .domainNotNull = false , false
388+ }
389+ if col .IsNotNull || col .IsArray {
390+ return
391+ }
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+ }
396+ }
397+ }
398+
383399func (c * Catalog ) dropTable (stmt * ast.DropTableStmt ) error {
384400 for _ , name := range stmt .Tables {
385401 ns := name .Schema
0 commit comments