Skip to content

Commit 9f044c0

Browse files
committed
Add ALTER TABLE COMMENT COLUMN support
- Add COMMENT case to parseAlterCommand for COMMENT COLUMN syntax - Add Comment field to AlterCommand struct for storing column comments - Update explainAlterQuery to output database and table as separate identifiers when database is present - Update AlterCommentColumn explain output to include comment literal - Fix various tests that now pass with COMMENT COLUMN support
1 parent 83ff599 commit 9f044c0

File tree

13 files changed

+47
-38
lines changed

13 files changed

+47
-38
lines changed

ast/ast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ type AlterCommand struct {
531531
ProjectionName string `json:"projection_name,omitempty"` // For DROP/MATERIALIZE/CLEAR PROJECTION
532532
StatisticsColumns []string `json:"statistics_columns,omitempty"` // For ADD/DROP/CLEAR/MATERIALIZE STATISTICS
533533
StatisticsTypes []*FunctionCall `json:"statistics_types,omitempty"` // For ADD/MODIFY STATISTICS TYPE
534+
Comment string `json:"comment,omitempty"` // For COMMENT COLUMN
534535
}
535536

536537
// Projection represents a projection definition.

internal/explain/statements.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -955,18 +955,22 @@ func explainAlterQuery(sb *strings.Builder, n *ast.AlterQuery, indent string, de
955955
return
956956
}
957957

958-
name := n.Table
958+
children := 2 // ExpressionList + Identifier for table
959959
if n.Database != "" {
960-
name = n.Database + "." + n.Table
960+
children = 3 // ExpressionList + Identifier for database + Identifier for table
961+
fmt.Fprintf(sb, "%sAlterQuery %s %s (children %d)\n", indent, n.Database, n.Table, children)
962+
} else {
963+
fmt.Fprintf(sb, "%sAlterQuery %s (children %d)\n", indent, n.Table, children)
961964
}
962965

963-
children := 2
964-
fmt.Fprintf(sb, "%sAlterQuery %s (children %d)\n", indent, name, children)
965966
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Commands))
966967
for _, cmd := range n.Commands {
967968
explainAlterCommand(sb, cmd, indent+" ", depth+2)
968969
}
969-
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
970+
if n.Database != "" {
971+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
972+
}
973+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
970974
}
971975

972976
func explainAlterCommand(sb *strings.Builder, cmd *ast.AlterCommand, indent string, depth int) {
@@ -1015,6 +1019,9 @@ func explainAlterCommand(sb *strings.Builder, cmd *ast.AlterCommand, indent stri
10151019
if cmd.ColumnName != "" {
10161020
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.ColumnName)
10171021
}
1022+
if cmd.Comment != "" {
1023+
fmt.Fprintf(sb, "%s Literal \\'%s\\'\n", indent, escapeStringLiteral(cmd.Comment))
1024+
}
10181025
case ast.AlterAddIndex, ast.AlterDropIndex, ast.AlterClearIndex, ast.AlterMaterializeIndex:
10191026
if cmd.Index != "" {
10201027
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.Index)
@@ -1178,10 +1185,17 @@ func countAlterCommandChildren(cmd *ast.AlterCommand) int {
11781185
if cmd.AfterColumn != "" {
11791186
children++
11801187
}
1181-
case ast.AlterDropColumn, ast.AlterCommentColumn:
1188+
case ast.AlterDropColumn:
1189+
if cmd.ColumnName != "" {
1190+
children++
1191+
}
1192+
case ast.AlterCommentColumn:
11821193
if cmd.ColumnName != "" {
11831194
children++
11841195
}
1196+
if cmd.Comment != "" {
1197+
children++
1198+
}
11851199
case ast.AlterRenameColumn:
11861200
if cmd.ColumnName != "" {
11871201
children++

parser/parser.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,6 +3860,28 @@ func (p *Parser) parseAlterCommand() *ast.AlterCommand {
38603860
}
38613861
}
38623862
}
3863+
case token.COMMENT:
3864+
p.nextToken()
3865+
if p.currentIs(token.COLUMN) {
3866+
cmd.Type = ast.AlterCommentColumn
3867+
p.nextToken()
3868+
// Handle IF EXISTS
3869+
if p.currentIs(token.IF) {
3870+
p.nextToken()
3871+
p.expect(token.EXISTS)
3872+
cmd.IfExists = true
3873+
}
3874+
// Parse column name
3875+
if p.currentIs(token.IDENT) || p.current.Token.IsKeyword() {
3876+
cmd.ColumnName = p.current.Value
3877+
p.nextToken()
3878+
}
3879+
// Parse comment string
3880+
if p.currentIs(token.STRING) {
3881+
cmd.Comment = p.current.Value
3882+
p.nextToken()
3883+
}
3884+
}
38633885
case token.DETACH:
38643886
p.nextToken()
38653887
if p.currentIs(token.PARTITION) {

parser/testdata/00030_alter_table/metadata.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"stmt16": true,
66
"stmt19": true,
77
"stmt20": true,
8-
"stmt22": true,
98
"stmt9": true
109
}
1110
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
{
22
"explain_todo": {
33
"stmt13": true,
4-
"stmt17": true,
54
"stmt19": true,
65
"stmt21": true,
76
"stmt3": true,
8-
"stmt7": true,
97
"stmt9": true
108
}
119
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt2": true,
4-
"stmt5": true
3+
"stmt2": true
54
}
65
}

parser/testdata/01164_alter_memory_database/metadata.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt5": true,
43
"stmt6": true,
54
"stmt7": true
65
}

parser/testdata/01702_system_query_log/metadata.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
"stmt15": true,
44
"stmt19": true,
55
"stmt24": true,
6-
"stmt26": true,
7-
"stmt27": true,
86
"stmt28": true,
9-
"stmt29": true,
107
"stmt30": true,
11-
"stmt31": true,
12-
"stmt32": true,
13-
"stmt33": true,
148
"stmt34": true,
159
"stmt37": true,
1610
"stmt38": true,
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt13": true,
4-
"stmt19": true,
5-
"stmt7": true
3+
"stmt13": true
64
}
75
}

parser/testdata/02918_alter_temporary_table/metadata.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"stmt15": true,
66
"stmt18": true,
77
"stmt19": true,
8-
"stmt21": true,
98
"stmt5": true,
109
"stmt8": true
1110
}

0 commit comments

Comments
 (0)