Skip to content

Commit 0d9f7c9

Browse files
kyleconroyclaude
andcommitted
Add ALTER TABLE MODIFY QUERY support (#123)
Added parsing and explain support for the ALTER TABLE MODIFY QUERY statement which modifies the SELECT query of a materialized view. Changes: - Added AlterModifyQuery command type to ast/ast.go - Added Query field to AlterCommand struct - Added parsing logic for MODIFY QUERY in parser.go - Added explain output handling in statements.go Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 7bf233f commit 0d9f7c9

File tree

10 files changed

+24
-35
lines changed

10 files changed

+24
-35
lines changed

ast/ast.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ type AlterCommand struct {
624624
OrderByExpr []Expression `json:"order_by_expr,omitempty"` // For MODIFY ORDER BY
625625
SampleByExpr Expression `json:"sample_by_expr,omitempty"` // For MODIFY SAMPLE BY
626626
ResetSettings []string `json:"reset_settings,omitempty"` // For MODIFY COLUMN ... RESET SETTING
627+
Query Statement `json:"query,omitempty"` // For MODIFY QUERY
627628
}
628629

629630
// Projection represents a projection definition.
@@ -702,6 +703,7 @@ const (
702703
AlterModifyComment AlterCommandType = "MODIFY_COMMENT"
703704
AlterModifyOrderBy AlterCommandType = "MODIFY_ORDER_BY"
704705
AlterModifySampleBy AlterCommandType = "MODIFY_SAMPLE_BY"
706+
AlterModifyQuery AlterCommandType = "MODIFY_QUERY"
705707
AlterRemoveSampleBy AlterCommandType = "REMOVE_SAMPLE_BY"
706708
AlterApplyDeletedMask AlterCommandType = "APPLY_DELETED_MASK"
707709
)

internal/explain/statements.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,11 @@ func explainAlterCommand(sb *strings.Builder, cmd *ast.AlterCommand, indent stri
19311931
if cmd.SampleByExpr != nil {
19321932
Node(sb, cmd.SampleByExpr, depth+1)
19331933
}
1934+
case ast.AlterModifyQuery:
1935+
// MODIFY QUERY: output the SELECT statement
1936+
if cmd.Query != nil {
1937+
Node(sb, cmd.Query, depth+1)
1938+
}
19341939
case ast.AlterResetSetting:
19351940
// RESET SETTING outputs ExpressionList with Identifier children
19361941
if len(cmd.ResetSettings) > 0 {
@@ -2192,6 +2197,11 @@ func countAlterCommandChildren(cmd *ast.AlterCommand) int {
21922197
if cmd.SampleByExpr != nil {
21932198
children = 1
21942199
}
2200+
case ast.AlterModifyQuery:
2201+
// MODIFY QUERY: SELECT statement (1 child)
2202+
if cmd.Query != nil {
2203+
children = 1
2204+
}
21952205
case ast.AlterResetSetting:
21962206
// RESET SETTING: ExpressionList with setting names (1 child)
21972207
if len(cmd.ResetSettings) > 0 {

parser/parser.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5809,6 +5809,11 @@ func (p *Parser) parseAlterCommand() *ast.AlterCommand {
58095809
p.nextToken() // skip BY
58105810
}
58115811
cmd.SampleByExpr = p.parseExpression(LOWEST)
5812+
} else if p.currentIs(token.IDENT) && strings.ToUpper(p.current.Value) == "QUERY" {
5813+
// MODIFY QUERY SELECT ...
5814+
cmd.Type = ast.AlterModifyQuery
5815+
p.nextToken() // skip QUERY
5816+
cmd.Query = p.parseSelectWithUnion()
58125817
}
58135818
case token.RENAME:
58145819
p.nextToken()
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt9": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt3": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt8": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt4": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt8": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt8": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt9": true
4-
}
5-
}
1+
{}

0 commit comments

Comments
 (0)