Skip to content

Commit 2f9eff0

Browse files
committed
Add SHOW CREATE VIEW and SETTINGS support
- Fix SHOW CREATE VIEW parsing to use token.VIEW instead of IDENT check - Add HasSettings field to ShowQuery AST for SETTINGS clause - Parse SETTINGS clause in SHOW queries - Update EXPLAIN output for ShowCreateTableQuery, ShowCreateViewQuery, and ShowCreateDictionaryQuery to include Set when SETTINGS is present
1 parent 1079b4e commit 2f9eff0

File tree

6 files changed

+92
-78
lines changed

6 files changed

+92
-78
lines changed

ast/ast.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -658,14 +658,15 @@ func (d *DescribeQuery) statementNode() {}
658658

659659
// ShowQuery represents a SHOW statement.
660660
type ShowQuery struct {
661-
Position token.Position `json:"-"`
662-
ShowType ShowType `json:"show_type"`
663-
Database string `json:"database,omitempty"`
664-
From string `json:"from,omitempty"`
665-
Like string `json:"like,omitempty"`
666-
Where Expression `json:"where,omitempty"`
667-
Limit Expression `json:"limit,omitempty"`
668-
Format string `json:"format,omitempty"`
661+
Position token.Position `json:"-"`
662+
ShowType ShowType `json:"show_type"`
663+
Database string `json:"database,omitempty"`
664+
From string `json:"from,omitempty"`
665+
Like string `json:"like,omitempty"`
666+
Where Expression `json:"where,omitempty"`
667+
Limit Expression `json:"limit,omitempty"`
668+
Format string `json:"format,omitempty"`
669+
HasSettings bool `json:"has_settings,omitempty"` // Whether SETTINGS clause was specified
669670
}
670671

671672
func (s *ShowQuery) Pos() token.Position { return s.Position }

internal/explain/statements.go

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -572,31 +572,73 @@ func explainShowQuery(sb *strings.Builder, n *ast.ShowQuery, indent string) {
572572
// SHOW CREATE DICTIONARY has special output format
573573
if n.ShowType == ast.ShowCreateDictionary && (n.Database != "" || n.From != "") {
574574
if n.Database != "" && n.From != "" {
575-
fmt.Fprintf(sb, "%sShowCreateDictionaryQuery %s %s (children 2)\n", indent, n.Database, n.From)
575+
children := 2
576+
if n.HasSettings {
577+
children++
578+
}
579+
fmt.Fprintf(sb, "%sShowCreateDictionaryQuery %s %s (children %d)\n", indent, n.Database, n.From, children)
576580
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
577581
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.From)
582+
if n.HasSettings {
583+
fmt.Fprintf(sb, "%s Set\n", indent)
584+
}
578585
} else if n.From != "" {
579-
fmt.Fprintf(sb, "%sShowCreateDictionaryQuery %s (children 1)\n", indent, n.From)
586+
children := 1
587+
if n.HasSettings {
588+
children++
589+
}
590+
fmt.Fprintf(sb, "%sShowCreateDictionaryQuery %s (children %d)\n", indent, n.From, children)
580591
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.From)
592+
if n.HasSettings {
593+
fmt.Fprintf(sb, "%s Set\n", indent)
594+
}
581595
} else if n.Database != "" {
582-
fmt.Fprintf(sb, "%sShowCreateDictionaryQuery %s (children 1)\n", indent, n.Database)
596+
children := 1
597+
if n.HasSettings {
598+
children++
599+
}
600+
fmt.Fprintf(sb, "%sShowCreateDictionaryQuery %s (children %d)\n", indent, n.Database, children)
583601
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
602+
if n.HasSettings {
603+
fmt.Fprintf(sb, "%s Set\n", indent)
604+
}
584605
}
585606
return
586607
}
587608

588609
// SHOW CREATE VIEW has special output format
589610
if n.ShowType == ast.ShowCreateView && (n.Database != "" || n.From != "") {
590611
if n.Database != "" && n.From != "" {
591-
fmt.Fprintf(sb, "%sShowCreateViewQuery %s %s (children 2)\n", indent, n.Database, n.From)
612+
children := 2
613+
if n.HasSettings {
614+
children++
615+
}
616+
fmt.Fprintf(sb, "%sShowCreateViewQuery %s %s (children %d)\n", indent, n.Database, n.From, children)
592617
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
593618
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.From)
619+
if n.HasSettings {
620+
fmt.Fprintf(sb, "%s Set\n", indent)
621+
}
594622
} else if n.From != "" {
595-
fmt.Fprintf(sb, "%sShowCreateViewQuery %s (children 1)\n", indent, n.From)
623+
children := 1
624+
if n.HasSettings {
625+
children++
626+
}
627+
fmt.Fprintf(sb, "%sShowCreateViewQuery %s (children %d)\n", indent, n.From, children)
596628
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.From)
629+
if n.HasSettings {
630+
fmt.Fprintf(sb, "%s Set\n", indent)
631+
}
597632
} else if n.Database != "" {
598-
fmt.Fprintf(sb, "%sShowCreateViewQuery %s (children 1)\n", indent, n.Database)
633+
children := 1
634+
if n.HasSettings {
635+
children++
636+
}
637+
fmt.Fprintf(sb, "%sShowCreateViewQuery %s (children %d)\n", indent, n.Database, children)
599638
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
639+
if n.HasSettings {
640+
fmt.Fprintf(sb, "%s Set\n", indent)
641+
}
600642
}
601643
return
602644
}
@@ -608,34 +650,52 @@ func explainShowQuery(sb *strings.Builder, n *ast.ShowQuery, indent string) {
608650
if n.Database != "" && n.From != "" {
609651
children := 2
610652
if n.Format != "" {
611-
children = 3
653+
children++
654+
}
655+
if n.HasSettings {
656+
children++
612657
}
613658
fmt.Fprintf(sb, "%sShowCreateTableQuery %s %s (children %d)\n", indent, n.Database, n.From, children)
614659
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
615660
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.From)
616661
if n.Format != "" {
617662
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Format)
618663
}
664+
if n.HasSettings {
665+
fmt.Fprintf(sb, "%s Set\n", indent)
666+
}
619667
} else if n.From != "" {
620668
children := 1
621669
if n.Format != "" {
622-
children = 2
670+
children++
671+
}
672+
if n.HasSettings {
673+
children++
623674
}
624675
fmt.Fprintf(sb, "%sShowCreateTableQuery %s (children %d)\n", indent, name, children)
625676
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
626677
if n.Format != "" {
627678
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Format)
628679
}
680+
if n.HasSettings {
681+
fmt.Fprintf(sb, "%s Set\n", indent)
682+
}
629683
} else if n.Database != "" {
630684
children := 1
631685
if n.Format != "" {
632-
children = 2
686+
children++
687+
}
688+
if n.HasSettings {
689+
children++
633690
}
634691
fmt.Fprintf(sb, "%sShowCreateTableQuery %s (children %d)\n", indent, n.Database, children)
635692
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
636693
if n.Format != "" {
637694
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Format)
638695
}
696+
if n.HasSettings {
697+
fmt.Fprintf(sb, "%s Set\n", indent)
698+
}
639699
} else {
640700
fmt.Fprintf(sb, "%sShow%s\n", indent, showType)
641701
}

parser/parser.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4071,7 +4071,7 @@ func (p *Parser) parseShow() ast.Statement {
40714071
} else if p.currentIs(token.IDENT) && strings.ToUpper(p.current.Value) == "DICTIONARY" {
40724072
show.ShowType = ast.ShowCreateDictionary
40734073
p.nextToken()
4074-
} else if p.currentIs(token.IDENT) && strings.ToUpper(p.current.Value) == "VIEW" {
4074+
} else if p.currentIs(token.VIEW) {
40754075
show.ShowType = ast.ShowCreateView
40764076
p.nextToken()
40774077
} else if p.currentIs(token.USER) {
@@ -4170,6 +4170,16 @@ func (p *Parser) parseShow() ast.Statement {
41704170
}
41714171
}
41724172

4173+
// Parse SETTINGS clause
4174+
if p.currentIs(token.SETTINGS) {
4175+
show.HasSettings = true
4176+
// Skip SETTINGS and all settings key-value pairs
4177+
p.nextToken()
4178+
for !p.currentIs(token.EOF) && !p.currentIs(token.SEMICOLON) && !p.currentIs(token.FORMAT) {
4179+
p.nextToken()
4180+
}
4181+
}
4182+
41734183
return show
41744184
}
41754185

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt34": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt19": true,
4-
"stmt21": true,
5-
"stmt24": true,
6-
"stmt25": true,
7-
"stmt26": true,
8-
"stmt27": true,
9-
"stmt28": true,
10-
"stmt30": true,
11-
"stmt31": true,
12-
"stmt32": true,
13-
"stmt33": true,
14-
"stmt34": true,
15-
"stmt36": true,
16-
"stmt37": true,
17-
"stmt38": true,
18-
"stmt39": true,
19-
"stmt40": true,
20-
"stmt42": true,
21-
"stmt43": true,
22-
"stmt44": true,
23-
"stmt45": true,
24-
"stmt46": true,
25-
"stmt48": true,
26-
"stmt49": true,
27-
"stmt50": true,
28-
"stmt51": true,
29-
"stmt52": true,
30-
"stmt54": true,
31-
"stmt55": true,
32-
"stmt56": true,
33-
"stmt57": true,
34-
"stmt58": true,
35-
"stmt60": true,
36-
"stmt61": true,
37-
"stmt62": true,
38-
"stmt63": true,
39-
"stmt64": true,
40-
"stmt66": true,
41-
"stmt67": true,
42-
"stmt68": true,
43-
"stmt69": true,
44-
"stmt70": true,
45-
"stmt72": true,
46-
"stmt73": true,
47-
"stmt74": true,
48-
"stmt75": true,
49-
"stmt76": true
50-
}
51-
}
1+
{}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
{
22
"explain_todo": {
33
"stmt11": true,
4-
"stmt12": true,
5-
"stmt14": true,
6-
"stmt15": true,
7-
"stmt18": true
4+
"stmt14": true
85
}
96
}

0 commit comments

Comments
 (0)