Skip to content

Commit 7e26155

Browse files
committed
Handle ORDER BY with ASC/DESC modifiers in CREATE TABLE (#120)
Parse ASC/DESC modifiers after non-parenthesized ORDER BY expressions in CREATE TABLE statements. When modifiers are present, output the column wrapped in StorageOrderByElement in EXPLAIN AST output.
1 parent e477f7d commit 7e26155

8 files changed

Lines changed: 20 additions & 33 deletions

File tree

internal/explain/statements.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,13 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
446446
if len(n.OrderBy) > 0 {
447447
if len(n.OrderBy) == 1 {
448448
if ident, ok := n.OrderBy[0].(*ast.Identifier); ok {
449-
fmt.Fprintf(sb, "%s Identifier %s\n", storageIndent, ident.Name())
449+
// When ORDER BY has modifiers (ASC/DESC), wrap in StorageOrderByElement
450+
if n.OrderByHasModifiers {
451+
fmt.Fprintf(sb, "%s StorageOrderByElement (children %d)\n", storageIndent, 1)
452+
fmt.Fprintf(sb, "%s Identifier %s\n", storageIndent, ident.Name())
453+
} else {
454+
fmt.Fprintf(sb, "%s Identifier %s\n", storageIndent, ident.Name())
455+
}
450456
} else if lit, ok := n.OrderBy[0].(*ast.Literal); ok && lit.Type == ast.LiteralTuple {
451457
// Handle tuple literal - for ORDER BY with modifiers (DESC/ASC),
452458
// ClickHouse outputs just "Function tuple" without children

parser/parser.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2626,7 +2626,13 @@ func (p *Parser) parseTableOptions(create *ast.CreateQuery) {
26262626
}
26272627
} else {
26282628
// Use ALIAS_PREC to avoid consuming AS keyword (for AS SELECT)
2629-
create.OrderBy = []ast.Expression{p.parseExpression(ALIAS_PREC)}
2629+
expr := p.parseExpression(ALIAS_PREC)
2630+
create.OrderBy = []ast.Expression{expr}
2631+
// Handle ASC/DESC modifier after single non-parenthesized ORDER BY expression
2632+
if p.currentIs(token.ASC) || p.currentIs(token.DESC) {
2633+
create.OrderByHasModifiers = true
2634+
p.nextToken()
2635+
}
26302636
}
26312637
}
26322638
case p.currentIs(token.PRIMARY):
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt5": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt5": true
4-
}
5-
}
1+
{}
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+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt2": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt2": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt14": true,
4-
"stmt36": true
5-
}
6-
}
1+
{}

0 commit comments

Comments
 (0)