Skip to content

Commit 7d94a69

Browse files
authored
Merge pull request #2681 from dolthub/fulghum/alter-table-extensions
Add support for setting/dropping column type and nullability
2 parents 07dfd6f + 5346e3d commit 7d94a69

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662
77
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71
88
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
9-
github.com/dolthub/vitess v0.0.0-20240919225659-2ad81685e772
9+
github.com/dolthub/vitess v0.0.0-20241001235747-8ed913ec76b7
1010
github.com/go-kit/kit v0.10.0
1111
github.com/go-sql-driver/mysql v1.7.2-0.20231213112541-0004702b931d
1212
github.com/gocraft/dbr/v2 v2.7.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81 h1:7/v8q9X
6060
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY=
6161
github.com/dolthub/vitess v0.0.0-20240919225659-2ad81685e772 h1:vDwBX7Lc8DnA8Zk0iRIu6slCw0GIUfYfFlYDYJQw8GQ=
6262
github.com/dolthub/vitess v0.0.0-20240919225659-2ad81685e772/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
63+
github.com/dolthub/vitess v0.0.0-20241001235747-8ed913ec76b7 h1:og740P9fUy1NzcmMhhAM/kTOFSjzy4ENzp6XUDc77uY=
64+
github.com/dolthub/vitess v0.0.0-20241001235747-8ed913ec76b7/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
6365
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
6466
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
6567
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=

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)