|
| 1 | +# doubleclick |
| 2 | + |
| 3 | +A ClickHouse SQL parser written in Go. Parses ClickHouse SQL syntax into an Abstract Syntax Tree (AST) and generates EXPLAIN output matching ClickHouse's format. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +go get github.com/sqlc-dev/doubleclick |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +```go |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/sqlc-dev/doubleclick/parser" |
| 23 | +) |
| 24 | + |
| 25 | +func main() { |
| 26 | + sql := `SELECT id, name FROM users WHERE active = 1 ORDER BY created_at DESC LIMIT 10` |
| 27 | + |
| 28 | + stmts, err := parser.Parse(context.Background(), strings.NewReader(sql)) |
| 29 | + if err != nil { |
| 30 | + panic(err) |
| 31 | + } |
| 32 | + |
| 33 | + // Serialize to JSON |
| 34 | + jsonBytes, _ := json.MarshalIndent(stmts[0], "", " ") |
| 35 | + fmt.Println(string(jsonBytes)) |
| 36 | + |
| 37 | + // Or print EXPLAIN AST output (matches ClickHouse format) |
| 38 | + fmt.Println(parser.Explain(stmts[0])) |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +JSON output: |
| 43 | + |
| 44 | +```json |
| 45 | +{ |
| 46 | + "selects": [ |
| 47 | + { |
| 48 | + "columns": [ |
| 49 | + { "parts": ["id"] }, |
| 50 | + { "parts": ["name"] } |
| 51 | + ], |
| 52 | + "from": { |
| 53 | + "tables": [ |
| 54 | + { "table": { "table": { "table": "users" } } } |
| 55 | + ] |
| 56 | + }, |
| 57 | + "where": { |
| 58 | + "left": { "parts": ["active"] }, |
| 59 | + "op": "=", |
| 60 | + "right": { "type": "Integer", "value": 1 } |
| 61 | + }, |
| 62 | + "order_by": [ |
| 63 | + { |
| 64 | + "expression": { "parts": ["created_at"] }, |
| 65 | + "descending": true |
| 66 | + } |
| 67 | + ], |
| 68 | + "limit": { "type": "Integer", "value": 10 } |
| 69 | + } |
| 70 | + ] |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +EXPLAIN output: |
| 75 | + |
| 76 | +``` |
| 77 | +SelectWithUnionQuery (children 1) |
| 78 | + ExpressionList (children 1) |
| 79 | + SelectQuery (children 4) |
| 80 | + ExpressionList (children 2) |
| 81 | + Identifier id |
| 82 | + Identifier name |
| 83 | + TablesInSelectQuery (children 1) |
| 84 | + TablesInSelectQueryElement (children 1) |
| 85 | + TableExpression (children 1) |
| 86 | + TableIdentifier (children 1) |
| 87 | + Identifier users |
| 88 | + Function equals (children 1) |
| 89 | + ExpressionList (children 2) |
| 90 | + Identifier active |
| 91 | + Literal UInt64_1 |
| 92 | + ExpressionList (children 1) |
| 93 | + OrderByElement (children 1) |
| 94 | + Identifier created_at |
| 95 | + Literal UInt64_10 |
| 96 | +``` |
| 97 | + |
| 98 | +## Features |
| 99 | + |
| 100 | +- Parses SELECT, INSERT, CREATE, DROP, ALTER, and other ClickHouse statements |
| 101 | +- Handles ClickHouse-specific syntax (Array types, PREWHERE, SAMPLE, etc.) |
| 102 | +- Supports JOINs, subqueries, CTEs, window functions, and complex expressions |
| 103 | +- Generates JSON-serializable AST nodes |
| 104 | +- Produces EXPLAIN AST output matching ClickHouse's format |
0 commit comments