Skip to content

Commit 613e3e5

Browse files
kyleconroyclaude
andcommitted
Output GROUP BY before ORDER BY in ProjectionSelectQuery
ClickHouse outputs GROUP BY before ORDER BY in projection EXPLAIN AST. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 35d1e5f commit 613e3e5

2 files changed

Lines changed: 8 additions & 11 deletions

File tree

internal/explain/statements.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,13 @@ func explainProjectionSelectQuery(sb *strings.Builder, q *ast.ProjectionSelectQu
19901990
Node(sb, col, depth+2)
19911991
}
19921992
}
1993+
// GROUP BY comes before ORDER BY in projection output
1994+
if len(q.GroupBy) > 0 {
1995+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(q.GroupBy))
1996+
for _, expr := range q.GroupBy {
1997+
Node(sb, expr, depth+2)
1998+
}
1999+
}
19932000
if len(q.OrderBy) > 0 {
19942001
if len(q.OrderBy) == 1 {
19952002
// Single column: just output as Identifier
@@ -2003,12 +2010,6 @@ func explainProjectionSelectQuery(sb *strings.Builder, q *ast.ProjectionSelectQu
20032010
}
20042011
}
20052012
}
2006-
if len(q.GroupBy) > 0 {
2007-
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(q.GroupBy))
2008-
for _, expr := range q.GroupBy {
2009-
Node(sb, expr, depth+2)
2010-
}
2011-
}
20122013
}
20132014

20142015
func explainStatisticsCommand(sb *strings.Builder, cmd *ast.AlterCommand, indent string, depth int) {
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)