Skip to content

Commit f48af3a

Browse files
committed
Fix SYSTEM query EXPLAIN output and parsing
- Add table/database as children in SYSTEM query explain output - Exclude FLUSH LOGS command from showing table name as child - Add INDEX, INSERT, PRIMARY, KEY tokens to isSystemCommandKeyword - Add QUEUE to system command identifiers list
1 parent 2f9eff0 commit f48af3a

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

internal/explain/statements.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,31 @@ func explainSetQuery(sb *strings.Builder, indent string) {
516516
}
517517

518518
func explainSystemQuery(sb *strings.Builder, n *ast.SystemQuery, indent string) {
519-
fmt.Fprintf(sb, "%sSYSTEM query\n", indent)
519+
// Some commands like FLUSH LOGS don't show the log name as a child
520+
// For other commands, table/database names are shown as children
521+
isFlushLogs := strings.HasPrefix(strings.ToUpper(n.Command), "FLUSH LOGS")
522+
523+
// Count children - database and table are children if present and not FLUSH LOGS
524+
children := 0
525+
if !isFlushLogs {
526+
if n.Database != "" {
527+
children++
528+
}
529+
if n.Table != "" {
530+
children++
531+
}
532+
}
533+
if children > 0 {
534+
fmt.Fprintf(sb, "%sSYSTEM query (children %d)\n", indent, children)
535+
if n.Database != "" {
536+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
537+
}
538+
if n.Table != "" {
539+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
540+
}
541+
} else {
542+
fmt.Fprintf(sb, "%sSYSTEM query\n", indent)
543+
}
520544
}
521545

522546
func explainExplainQuery(sb *strings.Builder, n *ast.ExplainQuery, indent string, depth int) {

parser/parser.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4359,13 +4359,15 @@ func (p *Parser) parseSystem() *ast.SystemQuery {
43594359
// isSystemCommandKeyword returns true if current token is a keyword that can be part of SYSTEM command
43604360
func (p *Parser) isSystemCommandKeyword() bool {
43614361
switch p.current.Token {
4362-
case token.TTL, token.SYNC, token.DROP, token.FORMAT, token.FOR:
4362+
case token.TTL, token.SYNC, token.DROP, token.FORMAT, token.FOR, token.INDEX, token.INSERT,
4363+
token.PRIMARY, token.KEY:
43634364
return true
43644365
}
4365-
// Handle SCHEMA, CACHE as identifiers since they're not keyword tokens
4366+
// Handle SCHEMA, CACHE, QUEUE and other identifiers that are part of SYSTEM commands
43664367
if p.currentIs(token.IDENT) {
43674368
upper := strings.ToUpper(p.current.Value)
4368-
if upper == "SCHEMA" || upper == "CACHE" {
4369+
switch upper {
4370+
case "SCHEMA", "CACHE", "QUEUE":
43694371
return true
43704372
}
43714373
}
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+
{}

0 commit comments

Comments
 (0)