Skip to content

Commit d17d8e7

Browse files
authored
Merge branch 'main' into fulghum-2c2ea65c
2 parents 0011910 + 7d94a69 commit d17d8e7

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

sql/planbuilder/ddl.go

Lines changed: 58 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,56 @@ 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+
colCopy := *c
941+
switch strings.ToLower(spec.Action) {
942+
case ast.SetStr:
943+
// Set NOT NULL constraint
944+
colCopy.Nullable = false
945+
case ast.DropStr:
946+
// Drop NOT NULL constraint
947+
colCopy.Nullable = true
948+
default:
949+
err := sql.ErrUnsupportedFeature.New(ast.String(ddl))
950+
b.handleErr(err)
951+
}
952+
953+
modifyColumn := plan.NewModifyColumnResolved(table, c.Name, colCopy, nil)
954+
outScope.node = b.modifySchemaTarget(inScope, modifyColumn, table.Schema())
955+
return
956+
}
957+
}
958+
err := sql.ErrTableColumnNotFound.New(table.Name(), spec.Column.String())
959+
b.handleErr(err)
960+
return
961+
}
962+
963+
func (b *Builder) buildAlterChangeColumnType(inScope *scope, ddl *ast.DDL, table *plan.ResolvedTable) (outScope *scope) {
964+
outScope = inScope
965+
spec := ddl.ColumnTypeSpec
966+
for _, c := range table.Schema() {
967+
if strings.EqualFold(c.Name, spec.Column.String()) {
968+
colCopy := *c
969+
typ, err := types.ColumnTypeToType(&spec.Type)
970+
if err != nil {
971+
b.handleErr(err)
972+
return
973+
}
974+
colCopy.Type = typ
975+
modifyColumn := plan.NewModifyColumnResolved(table, c.Name, colCopy, nil)
976+
outScope.node = b.modifySchemaTarget(inScope, modifyColumn, table.Schema())
977+
return
978+
}
979+
}
980+
err := sql.ErrTableColumnNotFound.New(table.Name(), spec.Column.String())
981+
b.handleErr(err)
982+
return
983+
}
984+
927985
func (b *Builder) buildAlterDefault(inScope *scope, ddl *ast.DDL, table *plan.ResolvedTable) (outScope *scope) {
928986
outScope = inScope
929987
switch strings.ToLower(ddl.DefaultSpec.Action) {

0 commit comments

Comments
 (0)