Skip to content

Commit

Permalink
Merge branch 'main' into fulghum-2c2ea65c
Browse files Browse the repository at this point in the history
  • Loading branch information
fulghum authored Oct 2, 2024
2 parents 0011910 + 7d94a69 commit d17d8e7
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions sql/planbuilder/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,14 @@ func (b *Builder) buildAlterTableClause(inScope *scope, ddl *ast.DDL) []*scope {
outScopes = append(outScopes, b.buildAlterCollationSpec(tableScope, ddl, rt))
}

if ddl.NotNullSpec != nil {
outScopes = append(outScopes, b.buildAlterNotNull(tableScope, ddl, rt))
}

if ddl.ColumnTypeSpec != nil {
outScopes = append(outScopes, b.buildAlterChangeColumnType(tableScope, ddl, rt))
}

for _, s := range outScopes {
if ts, ok := s.node.(sql.SchemaTarget); ok {
s.node = b.modifySchemaTarget(s, ts, rt.Schema())
Expand Down Expand Up @@ -924,6 +932,56 @@ func (b *Builder) buildAlterAutoIncrement(inScope *scope, ddl *ast.DDL, table *p
return
}

func (b *Builder) buildAlterNotNull(inScope *scope, ddl *ast.DDL, table *plan.ResolvedTable) (outScope *scope) {
outScope = inScope
spec := ddl.NotNullSpec
for _, c := range table.Schema() {
if strings.EqualFold(c.Name, spec.Column.String()) {
colCopy := *c
switch strings.ToLower(spec.Action) {
case ast.SetStr:
// Set NOT NULL constraint
colCopy.Nullable = false
case ast.DropStr:
// Drop NOT NULL constraint
colCopy.Nullable = true
default:
err := sql.ErrUnsupportedFeature.New(ast.String(ddl))
b.handleErr(err)
}

modifyColumn := plan.NewModifyColumnResolved(table, c.Name, colCopy, nil)
outScope.node = b.modifySchemaTarget(inScope, modifyColumn, table.Schema())
return
}
}
err := sql.ErrTableColumnNotFound.New(table.Name(), spec.Column.String())
b.handleErr(err)
return
}

func (b *Builder) buildAlterChangeColumnType(inScope *scope, ddl *ast.DDL, table *plan.ResolvedTable) (outScope *scope) {
outScope = inScope
spec := ddl.ColumnTypeSpec
for _, c := range table.Schema() {
if strings.EqualFold(c.Name, spec.Column.String()) {
colCopy := *c
typ, err := types.ColumnTypeToType(&spec.Type)
if err != nil {
b.handleErr(err)
return
}
colCopy.Type = typ
modifyColumn := plan.NewModifyColumnResolved(table, c.Name, colCopy, nil)
outScope.node = b.modifySchemaTarget(inScope, modifyColumn, table.Schema())
return
}
}
err := sql.ErrTableColumnNotFound.New(table.Name(), spec.Column.String())
b.handleErr(err)
return
}

func (b *Builder) buildAlterDefault(inScope *scope, ddl *ast.DDL, table *plan.ResolvedTable) (outScope *scope) {
outScope = inScope
switch strings.ToLower(ddl.DefaultSpec.Action) {
Expand Down

0 comments on commit d17d8e7

Please sign in to comment.