Skip to content

Commit 8fce7a9

Browse files
committed
Add support for CREATE QUOTA, SET DEFAULT ROLE, and MODIFY COLUMN REMOVE COMMENT
Parser changes: - Add parseCreateQuota function returning CreateQuotaQuery - Add parseSetRole function for SET DEFAULT ROLE statements - Handle MODIFY COLUMN ... REMOVE COMMENT syntax - Add FORMAT handling for SHOW GRANTS, SHOW CREATE ROLE/USER/POLICY/QUOTA/SETTINGS PROFILE AST changes: - Add CreateQuotaQuery struct - Add SetRoleQuery struct - Add Format field to ShowGrantsQuery, ShowCreateRoleQuery, ShowCreateRowPolicyQuery, ShowCreateQuotaQuery, ShowCreateSettingsProfileQuery Explain changes: - Add handlers for new statement types - Fix ColumnDeclaration to omit (children 0) when no children - Fix ALTER command type mapping (CLEAR_COLUMN -> DROP_COLUMN, DELETE_WHERE -> DELETE) - Fix TRUNCATE/CHECK queries to output db/table as separate identifiers - Add FORMAT handling for DESCRIBE, SHOW queries Fixes all 20 statements in 01702_system_query_log and many others.
1 parent dbd2b5e commit 8fce7a9

File tree

78 files changed

+374
-371
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+374
-371
lines changed

ast/ast.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -693,18 +693,24 @@ func (s *ShowQuery) statementNode() {}
693693
type ShowType string
694694

695695
const (
696-
ShowTables ShowType = "TABLES"
697-
ShowDatabases ShowType = "DATABASES"
698-
ShowProcesses ShowType = "PROCESSLIST"
699-
ShowCreate ShowType = "CREATE"
700-
ShowCreateDB ShowType = "CREATE_DATABASE"
701-
ShowCreateDictionary ShowType = "CREATE_DICTIONARY"
702-
ShowCreateView ShowType = "CREATE_VIEW"
703-
ShowCreateUser ShowType = "CREATE_USER"
704-
ShowColumns ShowType = "COLUMNS"
705-
ShowDictionaries ShowType = "DICTIONARIES"
706-
ShowFunctions ShowType = "FUNCTIONS"
707-
ShowSettings ShowType = "SETTINGS"
696+
ShowTables ShowType = "TABLES"
697+
ShowDatabases ShowType = "DATABASES"
698+
ShowProcesses ShowType = "PROCESSLIST"
699+
ShowCreate ShowType = "CREATE"
700+
ShowCreateDB ShowType = "CREATE_DATABASE"
701+
ShowCreateDictionary ShowType = "CREATE_DICTIONARY"
702+
ShowCreateView ShowType = "CREATE_VIEW"
703+
ShowCreateUser ShowType = "CREATE_USER"
704+
ShowCreateRole ShowType = "CREATE_ROLE"
705+
ShowCreatePolicy ShowType = "CREATE_POLICY"
706+
ShowCreateRowPolicy ShowType = "CREATE_ROW_POLICY"
707+
ShowCreateQuota ShowType = "CREATE_QUOTA"
708+
ShowCreateSettingsProfile ShowType = "CREATE_SETTINGS_PROFILE"
709+
ShowColumns ShowType = "COLUMNS"
710+
ShowDictionaries ShowType = "DICTIONARIES"
711+
ShowFunctions ShowType = "FUNCTIONS"
712+
ShowSettings ShowType = "SETTINGS"
713+
ShowGrants ShowType = "GRANTS"
708714
)
709715

710716
// ExplainQuery represents an EXPLAIN statement.
@@ -866,6 +872,7 @@ func (g *GrantQuery) statementNode() {}
866872
// ShowGrantsQuery represents a SHOW GRANTS statement.
867873
type ShowGrantsQuery struct {
868874
Position token.Position `json:"-"`
875+
Format string `json:"format,omitempty"`
869876
}
870877

871878
func (s *ShowGrantsQuery) Pos() token.Position { return s.Position }
@@ -885,12 +892,23 @@ func (s *ShowPrivilegesQuery) statementNode() {}
885892
type ShowCreateQuotaQuery struct {
886893
Position token.Position `json:"-"`
887894
Name string `json:"name,omitempty"`
895+
Format string `json:"format,omitempty"`
888896
}
889897

890898
func (s *ShowCreateQuotaQuery) Pos() token.Position { return s.Position }
891899
func (s *ShowCreateQuotaQuery) End() token.Position { return s.Position }
892900
func (s *ShowCreateQuotaQuery) statementNode() {}
893901

902+
// CreateQuotaQuery represents a CREATE QUOTA statement.
903+
type CreateQuotaQuery struct {
904+
Position token.Position `json:"-"`
905+
Name string `json:"name,omitempty"`
906+
}
907+
908+
func (c *CreateQuotaQuery) Pos() token.Position { return c.Position }
909+
func (c *CreateQuotaQuery) End() token.Position { return c.Position }
910+
func (c *CreateQuotaQuery) statementNode() {}
911+
894912
// CreateSettingsProfileQuery represents a CREATE SETTINGS PROFILE statement.
895913
type CreateSettingsProfileQuery struct {
896914
Position token.Position `json:"-"`
@@ -926,6 +944,7 @@ func (d *DropSettingsProfileQuery) statementNode() {}
926944
type ShowCreateSettingsProfileQuery struct {
927945
Position token.Position `json:"-"`
928946
Names []string `json:"names,omitempty"`
947+
Format string `json:"format,omitempty"`
929948
}
930949

931950
func (s *ShowCreateSettingsProfileQuery) Pos() token.Position { return s.Position }
@@ -955,6 +974,7 @@ func (d *DropRowPolicyQuery) statementNode() {}
955974
// ShowCreateRowPolicyQuery represents a SHOW CREATE ROW POLICY statement.
956975
type ShowCreateRowPolicyQuery struct {
957976
Position token.Position `json:"-"`
977+
Format string `json:"format,omitempty"`
958978
}
959979

960980
func (s *ShowCreateRowPolicyQuery) Pos() token.Position { return s.Position }
@@ -985,12 +1005,22 @@ func (d *DropRoleQuery) statementNode() {}
9851005
type ShowCreateRoleQuery struct {
9861006
Position token.Position `json:"-"`
9871007
RoleCount int `json:"role_count,omitempty"` // Number of roles specified
1008+
Format string `json:"format,omitempty"`
9881009
}
9891010

9901011
func (s *ShowCreateRoleQuery) Pos() token.Position { return s.Position }
9911012
func (s *ShowCreateRoleQuery) End() token.Position { return s.Position }
9921013
func (s *ShowCreateRoleQuery) statementNode() {}
9931014

1015+
// SetRoleQuery represents a SET DEFAULT ROLE statement.
1016+
type SetRoleQuery struct {
1017+
Position token.Position `json:"-"`
1018+
}
1019+
1020+
func (s *SetRoleQuery) Pos() token.Position { return s.Position }
1021+
func (s *SetRoleQuery) End() token.Position { return s.Position }
1022+
func (s *SetRoleQuery) statementNode() {}
1023+
9941024
// CreateResourceQuery represents a CREATE RESOURCE statement.
9951025
type CreateResourceQuery struct {
9961026
Position token.Position `json:"-"`

internal/explain/explain.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ func Node(sb *strings.Builder, node interface{}, depth int) {
119119
explainExchangeQuery(sb, n, indent)
120120
case *ast.SetQuery:
121121
explainSetQuery(sb, indent)
122+
case *ast.SetRoleQuery:
123+
fmt.Fprintf(sb, "%sSetRoleQuery\n", indent)
122124
case *ast.SystemQuery:
123125
explainSystemQuery(sb, n, indent)
124126
case *ast.TransactionControlQuery:
@@ -130,7 +132,14 @@ func Node(sb *strings.Builder, node interface{}, depth int) {
130132
case *ast.ShowPrivilegesQuery:
131133
fmt.Fprintf(sb, "%sShowPrivilegesQuery\n", indent)
132134
case *ast.ShowCreateQuotaQuery:
133-
fmt.Fprintf(sb, "%sSHOW CREATE QUOTA query\n", indent)
135+
if n.Format != "" {
136+
fmt.Fprintf(sb, "%sSHOW CREATE QUOTA query (children 1)\n", indent)
137+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Format)
138+
} else {
139+
fmt.Fprintf(sb, "%sSHOW CREATE QUOTA query\n", indent)
140+
}
141+
case *ast.CreateQuotaQuery:
142+
fmt.Fprintf(sb, "%sCreateQuotaQuery\n", indent)
134143
case *ast.CreateSettingsProfileQuery:
135144
fmt.Fprintf(sb, "%sCreateSettingsProfileQuery\n", indent)
136145
case *ast.AlterSettingsProfileQuery:
@@ -140,27 +149,43 @@ func Node(sb *strings.Builder, node interface{}, depth int) {
140149
fmt.Fprintf(sb, "%sDROP SETTINGS PROFILE query\n", indent)
141150
case *ast.ShowCreateSettingsProfileQuery:
142151
// Use PROFILES (plural) when multiple profiles are specified
152+
queryName := "SHOW CREATE SETTINGS PROFILE query"
143153
if len(n.Names) > 1 {
144-
fmt.Fprintf(sb, "%sSHOW CREATE SETTINGS PROFILES query\n", indent)
154+
queryName = "SHOW CREATE SETTINGS PROFILES query"
155+
}
156+
if n.Format != "" {
157+
fmt.Fprintf(sb, "%s%s (children 1)\n", indent, queryName)
158+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Format)
145159
} else {
146-
fmt.Fprintf(sb, "%sSHOW CREATE SETTINGS PROFILE query\n", indent)
160+
fmt.Fprintf(sb, "%s%s\n", indent, queryName)
147161
}
148162
case *ast.CreateRowPolicyQuery:
149163
fmt.Fprintf(sb, "%sCREATE ROW POLICY or ALTER ROW POLICY query\n", indent)
150164
case *ast.DropRowPolicyQuery:
151165
fmt.Fprintf(sb, "%sDROP ROW POLICY query\n", indent)
152166
case *ast.ShowCreateRowPolicyQuery:
153-
fmt.Fprintf(sb, "%sSHOW CREATE ROW POLICY query\n", indent)
167+
// ClickHouse uses "ROW POLICIES" (plural) when FORMAT is present
168+
if n.Format != "" {
169+
fmt.Fprintf(sb, "%sSHOW CREATE ROW POLICIES query (children 1)\n", indent)
170+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Format)
171+
} else {
172+
fmt.Fprintf(sb, "%sSHOW CREATE ROW POLICY query\n", indent)
173+
}
154174
case *ast.CreateRoleQuery:
155175
fmt.Fprintf(sb, "%sCreateRoleQuery\n", indent)
156176
case *ast.DropRoleQuery:
157177
fmt.Fprintf(sb, "%sDROP ROLE query\n", indent)
158178
case *ast.ShowCreateRoleQuery:
159179
// Use ROLES (plural) when multiple roles are specified
180+
queryName := "SHOW CREATE ROLE query"
160181
if n.RoleCount > 1 {
161-
fmt.Fprintf(sb, "%sSHOW CREATE ROLES query\n", indent)
182+
queryName = "SHOW CREATE ROLES query"
183+
}
184+
if n.Format != "" {
185+
fmt.Fprintf(sb, "%s%s (children 1)\n", indent, queryName)
186+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Format)
162187
} else {
163-
fmt.Fprintf(sb, "%sSHOW CREATE ROLE query\n", indent)
188+
fmt.Fprintf(sb, "%s%s\n", indent, queryName)
164189
}
165190
case *ast.CreateResourceQuery:
166191
fmt.Fprintf(sb, "%sCreateResourceQuery %s (children 1)\n", indent, n.Name)
@@ -181,7 +206,12 @@ func Node(sb *strings.Builder, node interface{}, depth int) {
181206
case *ast.DropWorkloadQuery:
182207
fmt.Fprintf(sb, "%sDropWorkloadQuery\n", indent)
183208
case *ast.ShowGrantsQuery:
184-
fmt.Fprintf(sb, "%sShowGrantsQuery\n", indent)
209+
if n.Format != "" {
210+
fmt.Fprintf(sb, "%sShowGrantsQuery (children 1)\n", indent)
211+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Format)
212+
} else {
213+
fmt.Fprintf(sb, "%sShowGrantsQuery\n", indent)
214+
}
185215
case *ast.GrantQuery:
186216
fmt.Fprintf(sb, "%sGrantQuery\n", indent)
187217
case *ast.UseQuery:
@@ -285,7 +315,11 @@ func Column(sb *strings.Builder, col *ast.ColumnDeclaration, depth int) {
285315
if col.Codec != nil {
286316
children++
287317
}
288-
fmt.Fprintf(sb, "%sColumnDeclaration %s (children %d)\n", indent, col.Name, children)
318+
if children > 0 {
319+
fmt.Fprintf(sb, "%sColumnDeclaration %s (children %d)\n", indent, col.Name, children)
320+
} else {
321+
fmt.Fprintf(sb, "%sColumnDeclaration %s\n", indent, col.Name)
322+
}
289323
if col.Type != nil {
290324
Node(sb, col.Type, depth+1)
291325
}

0 commit comments

Comments
 (0)