Skip to content

Commit cf94656

Browse files
authored
Merge pull request #3156 from dolthub/angela/table_comments
Add support for altering table comments
2 parents 3693a55 + 8f4a368 commit cf94656

File tree

10 files changed

+156
-2
lines changed

10 files changed

+156
-2
lines changed

enginetest/queries/alter_table_queries.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,12 +1029,58 @@ var AlterTableScripts = []ScriptTest{
10291029
},
10301030
},
10311031
},
1032+
{
1033+
Name: "alter table comment",
1034+
SetUpScript: []string{
1035+
"create table t (i int)",
1036+
"create table tableWithComment (i int) comment = 'comment'",
1037+
},
1038+
Assertions: []ScriptTestAssertion{
1039+
{
1040+
Query: "show create table t",
1041+
Expected: []sql.Row{{
1042+
"t",
1043+
"CREATE TABLE `t` (\n `i` int\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin",
1044+
}},
1045+
},
1046+
{
1047+
Query: "show create table tableWithComment",
1048+
Expected: []sql.Row{{
1049+
"tableWithComment",
1050+
"CREATE TABLE `tableWithComment` (\n `i` int\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin COMMENT='comment'",
1051+
}},
1052+
},
1053+
{
1054+
Query: "alter table t comment = 'new comment'",
1055+
Expected: []sql.Row{{types.NewOkResult(0)}},
1056+
},
1057+
{
1058+
Query: "alter table tableWithComment comment = 'new comment'",
1059+
Expected: []sql.Row{{types.NewOkResult(0)}},
1060+
},
1061+
{
1062+
Query: "show create table t",
1063+
Expected: []sql.Row{{
1064+
"t",
1065+
"CREATE TABLE `t` (\n `i` int\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin COMMENT='new comment'",
1066+
}},
1067+
},
1068+
{
1069+
Query: "show create table tableWithComment",
1070+
Expected: []sql.Row{{
1071+
"tableWithComment",
1072+
"CREATE TABLE `tableWithComment` (\n `i` int\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin COMMENT='new comment'",
1073+
}},
1074+
},
1075+
},
1076+
},
10321077
{
10331078
Name: "alter table comments are escaped",
10341079
SetUpScript: []string{
10351080
"create table t (i int);",
10361081
`alter table t modify column i int comment "newline \n | return \r | backslash \\ | NUL \0 \x00 | ctrlz \Z \x1A"`,
10371082
`alter table t add column j int comment "newline \n | return \r | backslash \\ | NUL \0 \x00 | ctrlz \Z \x1A"`,
1083+
`alter table t comment = "newline \n | return \r | backslash \\ | NUL \0 \x00 | ctrlz \Z \x1A"`,
10381084
},
10391085
Assertions: []ScriptTestAssertion{
10401086
{
@@ -1043,7 +1089,7 @@ var AlterTableScripts = []ScriptTest{
10431089
"t",
10441090
"CREATE TABLE `t` (\n `i` int COMMENT 'newline \\n | return \\r | backslash \\\\ | NUL \\0 x00 | ctrlz \x1A x1A'," +
10451091
"\n `j` int COMMENT 'newline \\n | return \\r | backslash \\\\ | NUL \\0 x00 | ctrlz \x1A x1A'\n" +
1046-
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
1092+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin COMMENT='newline \\n | return \\r | backslash \\\\ | NUL \\0 x00 | ctrlz \u001A x1A'"}},
10471093
},
10481094
},
10491095
},

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-20250327004329-6799764f2dad
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-20250730174048-497aebb8cea7
9+
github.com/dolthub/vitess v0.0.0-20250813175212-45844169a751
1010
github.com/go-sql-driver/mysql v1.7.2-0.20231213112541-0004702b931d
1111
github.com/gocraft/dbr/v2 v2.7.2
1212
github.com/google/uuid v1.3.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81 h1:7/v8q9X
2020
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY=
2121
github.com/dolthub/vitess v0.0.0-20250730174048-497aebb8cea7 h1:l+mWO0xoh4eG1J9gMS87opL6N6WGAQitF36R/Lg4bWs=
2222
github.com/dolthub/vitess v0.0.0-20250730174048-497aebb8cea7/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
23+
github.com/dolthub/vitess v0.0.0-20250813175212-45844169a751 h1:BBQKyvyODewdQxS+ICklMn1d/fFj2pVlkmMN1QFY4ms=
24+
github.com/dolthub/vitess v0.0.0-20250813175212-45844169a751/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
2325
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
2426
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
2527
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=

memory/table.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ var _ sql.TruncateableTable = (*Table)(nil)
7575
var _ sql.AlterableTable = (*Table)(nil)
7676
var _ sql.IndexAlterableTable = (*Table)(nil)
7777
var _ sql.CollationAlterableTable = (*Table)(nil)
78+
var _ sql.CommentAlterableTable = (*Table)(nil)
7879
var _ sql.ForeignKeyTable = (*Table)(nil)
7980
var _ sql.CheckAlterableTable = (*Table)(nil)
8081
var _ sql.RewritableTable = (*Table)(nil)
@@ -2188,6 +2189,13 @@ func (t *Table) ModifyDefaultCollation(ctx *sql.Context, collation sql.Collation
21882189
return nil
21892190
}
21902191

2192+
// ModifyComment implements sql.CommentAlterableTable
2193+
func (t *Table) ModifyComment(ctx *sql.Context, comment string) error {
2194+
data := t.sessionTableData(ctx)
2195+
data.comment = comment
2196+
return nil
2197+
}
2198+
21912199
// Filters implements the sql.FilteredTable interface.
21922200
func (t *Table) Filters() []sql.Expression {
21932201
return t.filters

sql/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ var (
409409
// ErrAlterTableCollationNotSupported is thrown when the table doesn't support ALTER TABLE COLLATE statements
410410
ErrAlterTableCollationNotSupported = errors.NewKind("table %s cannot have its collation altered")
411411

412+
// ErrAlterTableCommentNotSupported is thrown when the table doesn't support ALTER TABLE COMMENT statements
413+
ErrAlterTableCommentNotSupported = errors.NewKind("table %s cannot have its comment altered")
414+
412415
// ErrCollationNotSupportedOnUniqueTextIndex is thrown when a unique index is created on a TEXT column, with no
413416
// prefix length specified, and the collation is case-insensitive or accent-insensitive, meaning we can't
414417
// reliably use a content-hashed field to detect uniqueness.

sql/plan/alter_table.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,3 +968,67 @@ func (atc *AlterTableCollation) WithChildren(children ...sql.Node) (sql.Node, er
968968
natc.Table = children[0]
969969
return &natc, nil
970970
}
971+
972+
type AlterTableComment struct {
973+
ddlNode
974+
Table sql.Node
975+
Comment string
976+
}
977+
978+
var _ sql.Node = (*AlterTableComment)(nil)
979+
var _ sql.Databaser = (*AlterTableComment)(nil)
980+
981+
func NewAlterTableComment(table *ResolvedTable, comment string) *AlterTableComment {
982+
return &AlterTableComment{
983+
ddlNode: ddlNode{Db: table.SqlDatabase},
984+
Table: table,
985+
Comment: comment,
986+
}
987+
}
988+
989+
// WithDatabase implements the interface sql.Databaser
990+
func (atc *AlterTableComment) WithDatabase(db sql.Database) (sql.Node, error) {
991+
natc := *atc
992+
natc.Db = db
993+
return &natc, nil
994+
}
995+
996+
// IsReadOnly implements the interface sql.Node
997+
func (atc *AlterTableComment) IsReadOnly() bool {
998+
return false
999+
}
1000+
1001+
// String implements the interface sql.Node
1002+
func (atc *AlterTableComment) String() string {
1003+
return fmt.Sprintf("alter table %s comment %s", atc.Table.String(), atc.Comment)
1004+
}
1005+
1006+
// DebugString implements the interface sql.Node
1007+
func (atc *AlterTableComment) DebugString() string {
1008+
return atc.String()
1009+
}
1010+
1011+
// Resolved implements the interface sql.Node
1012+
func (atc *AlterTableComment) Resolved() bool {
1013+
return atc.Table.Resolved() && atc.ddlNode.Resolved()
1014+
}
1015+
1016+
// Schema implements the interface sql.Node
1017+
func (atc *AlterTableComment) Schema() sql.Schema {
1018+
return atc.Table.Schema()
1019+
}
1020+
1021+
// Children implements the interface sql.Node
1022+
func (atc *AlterTableComment) Children() []sql.Node {
1023+
return []sql.Node{atc.Table}
1024+
}
1025+
1026+
// WithChildren implements the interface sql.Node
1027+
func (atc *AlterTableComment) WithChildren(children ...sql.Node) (sql.Node, error) {
1028+
if len(children) != 1 {
1029+
return nil, sql.ErrInvalidChildrenNumber.New(atc, len(children), 1)
1030+
}
1031+
natc := *atc
1032+
natc.Table = children[0]
1033+
return &natc, nil
1034+
}

sql/planbuilder/ddl.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,10 @@ func (b *Builder) buildAlterTableClause(inScope *scope, ddl *ast.DDL) []*scope {
650650
outScopes = append(outScopes, b.buildAlterCollationSpec(tableScope, ddl, rt))
651651
}
652652

653+
if ddl.AlterCommentSpec != nil {
654+
outScopes = append(outScopes, b.buildAlterCommentSpec(tableScope, ddl, rt))
655+
}
656+
653657
if ddl.NotNullSpec != nil {
654658
outScopes = append(outScopes, b.buildAlterNotNull(tableScope, ddl, rt))
655659
}
@@ -1176,6 +1180,12 @@ func (b *Builder) buildAlterCollationSpec(inScope *scope, ddl *ast.DDL, table *p
11761180
return
11771181
}
11781182

1183+
func (b *Builder) buildAlterCommentSpec(inScope *scope, ddl *ast.DDL, table *plan.ResolvedTable) (outScope *scope) {
1184+
outScope = inScope
1185+
outScope.node = plan.NewAlterTableComment(table, ddl.AlterCommentSpec.Comment)
1186+
return
1187+
}
1188+
11791189
func (b *Builder) buildDefaultExpression(inScope *scope, defaultExpr ast.Expr) *sql.ColumnDefaultValue {
11801190
if defaultExpr == nil {
11811191
return nil

sql/rowexec/ddl.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,18 @@ func (b *BaseBuilder) buildAlterTableCollation(ctx *sql.Context, n *plan.AlterTa
11961196
return rowIterWithOkResultWithZeroRowsAffected(), alterable.ModifyDefaultCollation(ctx, n.Collation)
11971197
}
11981198

1199+
func (b *BaseBuilder) buildAlterTableComment(ctx *sql.Context, n *plan.AlterTableComment, row sql.Row) (sql.RowIter, error) {
1200+
tbl, err := getTableFromDatabase(ctx, n.Database(), n.Table)
1201+
if err != nil {
1202+
return nil, err
1203+
}
1204+
alterable, ok := tbl.(sql.CommentAlterableTable)
1205+
if !ok {
1206+
return nil, sql.ErrAlterTableCommentNotSupported.New(tbl.Name())
1207+
}
1208+
return rowIterWithOkResultWithZeroRowsAffected(), alterable.ModifyComment(ctx, n.Comment)
1209+
}
1210+
11991211
func (b *BaseBuilder) buildCreateForeignKey(ctx *sql.Context, n *plan.CreateForeignKey, row sql.Row) (sql.RowIter, error) {
12001212
db, err := n.DbProvider.Database(ctx, n.FkDef.Database)
12011213
if err != nil {

sql/rowexec/node_builder.gen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func (b *BaseBuilder) buildNodeExecNoAnalyze(ctx *sql.Context, n sql.Node, row s
5050
return b.buildCreateForeignKey(ctx, n, row)
5151
case *plan.AlterTableCollation:
5252
return b.buildAlterTableCollation(ctx, n, row)
53+
case *plan.AlterTableComment:
54+
return b.buildAlterTableComment(ctx, n, row)
5355
case *plan.CreateRole:
5456
return b.buildCreateRole(ctx, n, row)
5557
case *plan.Loop:

sql/tables.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ type CollationAlterableTable interface {
249249
ModifyDefaultCollation(ctx *Context, collation CollationID) error
250250
}
251251

252+
// CommentAlterableTable represents a table that supports altering its comment.
253+
type CommentAlterableTable interface {
254+
Table
255+
// ModifyComment modifies the table's comment
256+
ModifyComment(ctx *Context, comment string) error
257+
}
258+
252259
// PrimaryKeyTable is a table with a primary key.
253260
type PrimaryKeyTable interface {
254261
// PrimaryKeySchema returns this table's PrimaryKeySchema

0 commit comments

Comments
 (0)