Skip to content

Commit d348bf0

Browse files
committed
Add CREATE INDEX TYPE/GRANULARITY and DROP INDEX support
1. CREATE INDEX: Added parsing for TYPE and GRANULARITY clauses. Updated explain output to include Function node for the index type. Fixed IF NOT EXISTS ordering (comes before index name). 2. DROP INDEX: Added Index field to DropQuery, parsing for "DROP INDEX name ON table", and DropIndexQuery explain output. This fixes all 10 statements in 02319_sql_standard_create_drop_index.
1 parent cfc05a6 commit d348bf0

4 files changed

Lines changed: 64 additions & 24 deletions

File tree

ast/ast.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ type DropQuery struct {
503503
Policy string `json:"policy,omitempty"` // For DROP POLICY
504504
RowPolicy string `json:"row_policy,omitempty"` // For DROP ROW POLICY
505505
SettingsProfile string `json:"settings_profile,omitempty"` // For DROP SETTINGS PROFILE
506+
Index string `json:"index,omitempty"` // For DROP INDEX
506507
Temporary bool `json:"temporary,omitempty"`
507508
OnCluster string `json:"on_cluster,omitempty"`
508509
DropDatabase bool `json:"drop_database,omitempty"`
@@ -1107,10 +1108,12 @@ func (d *DropWorkloadQuery) statementNode() {}
11071108

11081109
// CreateIndexQuery represents a CREATE INDEX statement.
11091110
type CreateIndexQuery struct {
1110-
Position token.Position `json:"-"`
1111-
IndexName string `json:"index_name"`
1112-
Table string `json:"table"`
1113-
Columns []Expression `json:"columns,omitempty"`
1111+
Position token.Position `json:"-"`
1112+
IndexName string `json:"index_name"`
1113+
Table string `json:"table"`
1114+
Columns []Expression `json:"columns,omitempty"`
1115+
Type string `json:"type,omitempty"` // Index type (minmax, bloom_filter, etc.)
1116+
Granularity int `json:"granularity,omitempty"` // GRANULARITY value
11141117
}
11151118

11161119
func (c *CreateIndexQuery) Pos() token.Position { return c.Position }

internal/explain/statements.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,14 @@ func explainDropQuery(sb *strings.Builder, n *ast.DropQuery, indent string, dept
473473
return
474474
}
475475

476+
// DROP INDEX - outputs as DropIndexQuery with two spaces before table name
477+
if n.Index != "" {
478+
fmt.Fprintf(sb, "%sDropIndexQuery %s (children %d)\n", indent, n.Table, 2)
479+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Index)
480+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
481+
return
482+
}
483+
476484
// Handle multiple tables: DROP TABLE t1, t2, t3
477485
if len(n.Tables) > 1 {
478486
fmt.Fprintf(sb, "%sDropQuery (children %d)\n", indent, 1)
@@ -1564,8 +1572,13 @@ func explainCreateIndexQuery(sb *strings.Builder, n *ast.CreateIndexQuery, inden
15641572
// Child 1: Index name
15651573
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.IndexName)
15661574

1567-
// Child 2: Index wrapper with columns
1568-
fmt.Fprintf(sb, "%s Index (children 1)\n", indent)
1575+
// Child 2: Index wrapper with columns and type
1576+
// Index has 1 child for columns-only, 2 children if TYPE is specified
1577+
indexChildren := 1
1578+
if n.Type != "" {
1579+
indexChildren = 2
1580+
}
1581+
fmt.Fprintf(sb, "%s Index (children %d)\n", indent, indexChildren)
15691582

15701583
// For single column, output as Identifier
15711584
// For multiple columns or if there are any special cases, output as Function tuple
@@ -1583,6 +1596,12 @@ func explainCreateIndexQuery(sb *strings.Builder, n *ast.CreateIndexQuery, inden
15831596
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
15841597
}
15851598

1599+
// Output TYPE as Function with empty ExpressionList
1600+
if n.Type != "" {
1601+
fmt.Fprintf(sb, "%s Function %s (children 1)\n", indent, n.Type)
1602+
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
1603+
}
1604+
15861605
// Child 3: Table name
15871606
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
15881607
}

parser/parser.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,10 +1854,7 @@ func (p *Parser) parseCreateIndex(pos token.Position) *ast.CreateIndexQuery {
18541854
Position: pos,
18551855
}
18561856

1857-
// Parse index name
1858-
query.IndexName = p.parseIdentifierName()
1859-
1860-
// Skip IF NOT EXISTS if present
1857+
// Skip IF NOT EXISTS if present (comes before index name)
18611858
if p.currentIs(token.IF) {
18621859
p.nextToken() // IF
18631860
if p.currentIs(token.NOT) {
@@ -1868,6 +1865,9 @@ func (p *Parser) parseCreateIndex(pos token.Position) *ast.CreateIndexQuery {
18681865
}
18691866
}
18701867

1868+
// Parse index name
1869+
query.IndexName = p.parseIdentifierName()
1870+
18711871
// Expect ON
18721872
if p.currentIs(token.ON) {
18731873
p.nextToken()
@@ -1905,6 +1905,22 @@ func (p *Parser) parseCreateIndex(pos token.Position) *ast.CreateIndexQuery {
19051905
}
19061906
}
19071907

1908+
// Parse TYPE clause
1909+
if p.currentIs(token.IDENT) && strings.ToUpper(p.current.Value) == "TYPE" {
1910+
p.nextToken() // skip TYPE
1911+
query.Type = p.parseIdentifierName()
1912+
}
1913+
1914+
// Parse GRANULARITY clause
1915+
if p.currentIs(token.IDENT) && strings.ToUpper(p.current.Value) == "GRANULARITY" {
1916+
p.nextToken() // skip GRANULARITY
1917+
if p.currentIs(token.NUMBER) {
1918+
val, _ := strconv.Atoi(p.current.Value)
1919+
query.Granularity = val
1920+
p.nextToken()
1921+
}
1922+
}
1923+
19081924
return query
19091925
}
19101926

@@ -4021,6 +4037,7 @@ func (p *Parser) parseDrop() *ast.DropQuery {
40214037
dropFunction = true
40224038
p.nextToken()
40234039
case token.INDEX:
4040+
drop.Index = "_pending_"
40244041
p.nextToken()
40254042
case token.SETTINGS:
40264043
// DROP SETTINGS PROFILE
@@ -4116,6 +4133,20 @@ func (p *Parser) parseDrop() *ast.DropQuery {
41164133
drop.RowPolicy = tableName
41174134
} else if drop.SettingsProfile == "_pending_" {
41184135
drop.SettingsProfile = tableName
4136+
} else if drop.Index == "_pending_" {
4137+
drop.Index = tableName
4138+
// For DROP INDEX, parse ON table_name
4139+
if p.currentIs(token.ON) {
4140+
p.nextToken() // skip ON
4141+
tableNamePart := p.parseIdentifierName()
4142+
if p.currentIs(token.DOT) {
4143+
p.nextToken()
4144+
drop.Database = tableNamePart
4145+
drop.Table = p.parseIdentifierName()
4146+
} else {
4147+
drop.Table = tableNamePart
4148+
}
4149+
}
41194150
} else if dropDictionary {
41204151
drop.Dictionary = tableName
41214152
// Also set Table/Tables for backward compatibility with AST JSON
Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt14": true,
4-
"stmt15": true,
5-
"stmt16": true,
6-
"stmt21": true,
7-
"stmt22": true,
8-
"stmt3": true,
9-
"stmt4": true,
10-
"stmt5": true,
11-
"stmt8": true,
12-
"stmt9": true
13-
}
14-
}
1+
{}

0 commit comments

Comments
 (0)