Skip to content

Commit 1c84e91

Browse files
committed
Adding support for setting/dropping column attributes: type and not null constraint (needed for Postgres' more modular ALTER TABLE syntax)
1 parent 07dfd6f commit 1c84e91

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

sql/planbuilder/ddl.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,14 @@ func (b *Builder) buildAlterTableClause(inScope *scope, ddl *ast.DDL) []*scope {
526526
outScopes = append(outScopes, b.buildAlterCollationSpec(tableScope, ddl, rt))
527527
}
528528

529+
if ddl.NotNullSpec != nil {
530+
outScopes = append(outScopes, b.buildAlterNotNull(tableScope, ddl, rt))
531+
}
532+
533+
if ddl.ColumnTypeSpec != nil {
534+
outScopes = append(outScopes, b.buildAlterChangeColumnType(tableScope, ddl, rt))
535+
}
536+
529537
for _, s := range outScopes {
530538
if ts, ok := s.node.(sql.SchemaTarget); ok {
531539
s.node = b.modifySchemaTarget(s, ts, rt.Schema())
@@ -924,6 +932,54 @@ func (b *Builder) buildAlterAutoIncrement(inScope *scope, ddl *ast.DDL, table *p
924932
return
925933
}
926934

935+
func (b *Builder) buildAlterNotNull(inScope *scope, ddl *ast.DDL, table *plan.ResolvedTable) (outScope *scope) {
936+
outScope = inScope
937+
spec := ddl.NotNullSpec
938+
for _, c := range table.Schema() {
939+
if strings.EqualFold(c.Name, spec.Column.String()) {
940+
switch strings.ToLower(spec.Action) {
941+
case ast.SetStr:
942+
// Set NOT NULL constraint
943+
c.Nullable = false
944+
case ast.DropStr:
945+
// Drop NOT NULL constraint
946+
c.Nullable = true
947+
default:
948+
err := sql.ErrUnsupportedFeature.New(ast.String(ddl))
949+
b.handleErr(err)
950+
}
951+
952+
modifyColumn := plan.NewModifyColumnResolved(table, c.Name, *c, nil)
953+
outScope.node = b.modifySchemaTarget(inScope, modifyColumn, table.Schema())
954+
return
955+
}
956+
}
957+
err := sql.ErrTableColumnNotFound.New(table.Name(), spec.Column.String())
958+
b.handleErr(err)
959+
return
960+
}
961+
962+
func (b *Builder) buildAlterChangeColumnType(inScope *scope, ddl *ast.DDL, table *plan.ResolvedTable) (outScope *scope) {
963+
outScope = inScope
964+
spec := ddl.ColumnTypeSpec
965+
for _, c := range table.Schema() {
966+
if strings.EqualFold(c.Name, spec.Column.String()) {
967+
typ, err := types.ColumnTypeToType(&spec.Type)
968+
if err != nil {
969+
b.handleErr(err)
970+
return
971+
}
972+
c.Type = typ
973+
modifyColumn := plan.NewModifyColumnResolved(table, c.Name, *c, nil)
974+
outScope.node = b.modifySchemaTarget(inScope, modifyColumn, table.Schema())
975+
return
976+
}
977+
}
978+
err := sql.ErrTableColumnNotFound.New(table.Name(), spec.Column.String())
979+
b.handleErr(err)
980+
return
981+
}
982+
927983
func (b *Builder) buildAlterDefault(inScope *scope, ddl *ast.DDL, table *plan.ResolvedTable) (outScope *scope) {
928984
outScope = inScope
929985
switch strings.ToLower(ddl.DefaultSpec.Action) {

0 commit comments

Comments
 (0)