Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type SelectQuery struct {
Window []*WindowDefinition `json:"window,omitempty"`
OrderBy []*OrderByElement `json:"order_by,omitempty"`
Limit Expression `json:"limit,omitempty"`
LimitBy []Expression `json:"limit_by,omitempty"`
Offset Expression `json:"offset,omitempty"`
Settings []*SettingExpr `json:"settings,omitempty"`
IntoOutfile *IntoOutfileClause `json:"into_outfile,omitempty"`
Expand Down
3 changes: 3 additions & 0 deletions internal/explain/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ func explainSelectQuery(sb *strings.Builder, n *ast.SelectQuery, indent string,
if n.Limit != nil {
Node(sb, n.Limit, depth+1)
}
// Note: LIMIT BY expressions are stored in the AST but not output in EXPLAIN AST
// (ClickHouse doesn't output them in EXPLAIN AST)
// SETTINGS - output here if there's no FORMAT, otherwise it's at SelectWithUnionQuery level
if len(n.Settings) > 0 && n.Format == nil {
fmt.Fprintf(sb, "%s Set\n", indent)
Expand Down Expand Up @@ -214,6 +216,7 @@ func countSelectQueryChildren(n *ast.SelectQuery) int {
if n.Offset != nil {
count++
}
// Note: LimitBy is stored in AST but not counted in EXPLAIN AST children
// SETTINGS is counted here only if there's no FORMAT
// If FORMAT is present, SETTINGS is at SelectWithUnionQuery level
if len(n.Settings) > 0 && n.Format == nil {
Expand Down
11 changes: 11 additions & 0 deletions parser/CLAUDE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Parser Development Notes

## Test Data Files

The `testdata/` directory contains test cases from ClickHouse. Each test has:

- `query.sql` - The SQL query to parse
- `explain.txt` - **DO NOT MODIFY** - This is the ground truth output from ClickHouse's `EXPLAIN AST` command. Our parser must produce output that matches this exactly.
- `metadata.json` - Test metadata (todo, skip, etc.)
- `ast.json` - Optional golden file for AST regression testing

To fix a failing test, you must fix the **parser** to produce output matching `explain.txt`, never modify `explain.txt` itself.

## Running Tests

Always run parser tests with a 5 second timeout:
Expand Down
4 changes: 2 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,9 @@ func (p *Parser) parseSelect() *ast.SelectQuery {
// LIMIT BY clause (ClickHouse specific: LIMIT n BY expr1, expr2, ...)
if p.currentIs(token.BY) {
p.nextToken()
// Parse LIMIT BY expressions - skip them for now
// Parse LIMIT BY expressions
for !p.isEndOfExpression() {
p.parseExpression(LOWEST)
sel.LimitBy = append(sel.LimitBy, p.parseExpression(LOWEST))
if p.currentIs(token.COMMA) {
p.nextToken()
} else {
Expand Down
68 changes: 68 additions & 0 deletions parser/testdata/00017_aggregation_uninitialized_memory/ast.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"selects": [
{
"distinct": true,
"columns": [
{
"expr": {
"array": {
"name": "URLHierarchy",
"arguments": [
{
"parts": [
"URL"
]
}
]
},
"index": {
"type": "Integer",
"value": 1
}
},
"alias": "q"
},
{
"expr": {
"type": "String",
"value": "x"
},
"alias": "w"
}
],
"from": {
"tables": [
{
"table": {
"table": {
"database": "test",
"table": "hits"
}
}
}
]
},
"where": {
"left": {
"parts": [
"CounterID"
]
},
"op": "=",
"right": {
"type": "Integer",
"value": 14917930
}
},
"order_by": [
{
"expression": {
"parts": [
"URL"
]
}
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SelectWithUnionQuery (children 1)
ExpressionList (children 1)
SelectQuery (children 4)
ExpressionList (children 2)
Function arrayElement (alias q) (children 1)
Function arrayElement (children 1)
ExpressionList (children 2)
Function URLHierarchy (children 1)
ExpressionList (children 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"todo": true}
{}
80 changes: 80 additions & 0 deletions parser/testdata/00020_sorting_arrays/ast.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"selects": [
{
"columns": [
{
"name": "arrayJoin",
"arguments": [
{
"type": "Array",
"value": [
{
"type": "Array",
"value": [
{
"type": "Integer",
"value": 3
},
{
"type": "Integer",
"value": 4
},
{
"type": "Integer",
"value": 5
}
]
},
{
"type": "Array",
"value": [
{
"type": "Integer",
"value": 6
},
{
"type": "Integer",
"value": 7
}
]
},
{
"type": "Array",
"value": [
{
"type": "Integer",
"value": 2
}
]
},
{
"type": "Array",
"value": [
{
"type": "Integer",
"value": 1
},
{
"type": "Integer",
"value": 1
}
]
}
]
}
],
"alias": "x"
}
],
"order_by": [
{
"expression": {
"parts": [
"x"
]
}
}
]
}
]
}
7 changes: 6 additions & 1 deletion parser/testdata/00020_sorting_arrays/explain.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ SelectWithUnionQuery (children 1)
ExpressionList (children 1)
Function arrayJoin (alias x) (children 1)
ExpressionList (children 1)
Literal Array_[Array_[UInt64_3, UInt64_4, UInt64_5], Array_[UInt64_6, UInt64_7], Array_[UInt64_2], Array_[UInt64_1, UInt64_1]]
Function array (children 1)
ExpressionList (children 4)
Literal Array_[UInt64_3, UInt64_4, UInt64_5]
Literal Array_[UInt64_6, UInt64_7]
Literal Array_[UInt64_2]
Literal Array_[UInt64_1, UInt64_1]
ExpressionList (children 1)
OrderByElement (children 1)
Identifier x
2 changes: 1 addition & 1 deletion parser/testdata/00020_sorting_arrays/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"todo": true}
{}
47 changes: 47 additions & 0 deletions parser/testdata/00021_2_select_with_in/ast.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"selects": [
{
"columns": [
{
"name": "sum",
"arguments": [
{
"parts": [
"Sign"
]
}
]
}
],
"from": {
"tables": [
{
"table": {
"table": {
"database": "test",
"table": "visits"
}
}
}
]
},
"where": {
"expr": {
"parts": [
"CounterID"
]
},
"list": [
{
"type": "Integer",
"value": 942285
},
{
"type": "Integer",
"value": 577322
}
]
}
}
]
}
5 changes: 3 additions & 2 deletions parser/testdata/00021_2_select_with_in/explain.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SelectWithUnionQuery (children 1)
TableExpression (children 1)
TableIdentifier test.visits
Function in (children 1)
ExpressionList (children 2)
ExpressionList (children 3)
Identifier CounterID
Literal Tuple_(UInt64_942285, UInt64_577322)
Literal UInt64_942285
Literal UInt64_577322
2 changes: 1 addition & 1 deletion parser/testdata/00021_2_select_with_in/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"todo": true}
{}
28 changes: 28 additions & 0 deletions parser/testdata/00021_3_select_with_in/ast.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"selects": [
{
"columns": [
{
"expr": {
"type": "Integer",
"value": 1
},
"list": [
{
"type": "Integer",
"value": 1
},
{
"type": "Integer",
"value": 2
},
{
"type": "Integer",
"value": 3
}
]
}
]
}
]
}
6 changes: 4 additions & 2 deletions parser/testdata/00021_3_select_with_in/explain.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ SelectWithUnionQuery (children 1)
SelectQuery (children 1)
ExpressionList (children 1)
Function in (children 1)
ExpressionList (children 2)
ExpressionList (children 4)
Literal UInt64_1
Literal Tuple_(UInt64_1, UInt64_2, UInt64_3)
Literal UInt64_1
Literal UInt64_2
Literal UInt64_3
2 changes: 1 addition & 1 deletion parser/testdata/00021_3_select_with_in/metadata.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"todo": true}
{}
Loading