From 5f245756d81c950c743e3c13cee4852480dd3cfa Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 22 Dec 2025 23:24:57 +0000 Subject: [PATCH 1/4] Add README with usage example Document project purpose, installation, and provide a working code example that demonstrates parsing SQL and generating EXPLAIN output. --- README.md | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 000000000..39ac741ec --- /dev/null +++ b/README.md @@ -0,0 +1,71 @@ +# doubleclick + +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. + +## Installation + +```bash +go get github.com/kyleconroy/doubleclick +``` + +## Usage + +```go +package main + +import ( + "context" + "fmt" + "strings" + + "github.com/kyleconroy/doubleclick/parser" +) + +func main() { + sql := `SELECT id, name FROM users WHERE active = 1 ORDER BY created_at DESC LIMIT 10` + + stmts, err := parser.Parse(context.Background(), strings.NewReader(sql)) + if err != nil { + panic(err) + } + + // Print EXPLAIN AST output (matches ClickHouse format) + fmt.Println(parser.Explain(stmts[0])) +} +``` + +Output: + +``` +SelectWithUnionQuery (children 1) + ExpressionList (children 1) + SelectQuery (children 4) + ExpressionList (children 2) + Identifier id + Identifier name + TablesInSelectQuery (children 1) + TablesInSelectQueryElement (children 1) + TableExpression (children 1) + TableIdentifier (children 1) + Identifier users + Function equals (children 1) + ExpressionList (children 2) + Identifier active + Literal UInt64_1 + ExpressionList (children 1) + OrderByElement (children 1) + Identifier created_at + Literal UInt64_10 +``` + +## Features + +- Parses SELECT, INSERT, CREATE, DROP, ALTER, and other ClickHouse statements +- Handles ClickHouse-specific syntax (Array types, PREWHERE, SAMPLE, etc.) +- Supports JOINs, subqueries, CTEs, window functions, and complex expressions +- Generates JSON-serializable AST nodes +- Produces EXPLAIN AST output matching ClickHouse's format + +## License + +MIT From 05f321b5064dfb79753bc557d864ca61804b8655 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 22 Dec 2025 23:28:45 +0000 Subject: [PATCH 2/4] Fix import path to sqlc-dev/doubleclick --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 39ac741ec..2cdf842b9 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A ClickHouse SQL parser written in Go. Parses ClickHouse SQL syntax into an Abst ## Installation ```bash -go get github.com/kyleconroy/doubleclick +go get github.com/sqlc-dev/doubleclick ``` ## Usage @@ -18,7 +18,7 @@ import ( "fmt" "strings" - "github.com/kyleconroy/doubleclick/parser" + "github.com/sqlc-dev/doubleclick/parser" ) func main() { From 3317a59e832b68694d487ae0c958d6fa6db889b3 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 22 Dec 2025 23:30:54 +0000 Subject: [PATCH 3/4] Add JSON output example and remove License section --- README.md | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2cdf842b9..f378f1658 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ package main import ( "context" + "encoding/json" "fmt" "strings" @@ -31,6 +32,10 @@ func main() { // Print EXPLAIN AST output (matches ClickHouse format) fmt.Println(parser.Explain(stmts[0])) + + // Or serialize to JSON + jsonBytes, _ := json.MarshalIndent(stmts[0], "", " ") + fmt.Println(string(jsonBytes)) } ``` @@ -58,6 +63,38 @@ SelectWithUnionQuery (children 1) Literal UInt64_10 ``` +JSON output: + +```json +{ + "selects": [ + { + "columns": [ + { "parts": ["id"] }, + { "parts": ["name"] } + ], + "from": { + "tables": [ + { "table": { "table": { "table": "users" } } } + ] + }, + "where": { + "left": { "parts": ["active"] }, + "op": "=", + "right": { "type": "Integer", "value": 1 } + }, + "order_by": [ + { + "expression": { "parts": ["created_at"] }, + "descending": true + } + ], + "limit": { "type": "Integer", "value": 10 } + } + ] +} +``` + ## Features - Parses SELECT, INSERT, CREATE, DROP, ALTER, and other ClickHouse statements @@ -65,7 +102,3 @@ SelectWithUnionQuery (children 1) - Supports JOINs, subqueries, CTEs, window functions, and complex expressions - Generates JSON-serializable AST nodes - Produces EXPLAIN AST output matching ClickHouse's format - -## License - -MIT From 384d758f1dad0e14b703f04677f880fa082f47ea Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 22 Dec 2025 23:33:20 +0000 Subject: [PATCH 4/4] Reorder output: JSON first, then EXPLAIN --- README.md | 56 +++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index f378f1658..588c281fd 100644 --- a/README.md +++ b/README.md @@ -30,37 +30,13 @@ func main() { panic(err) } - // Print EXPLAIN AST output (matches ClickHouse format) - fmt.Println(parser.Explain(stmts[0])) - - // Or serialize to JSON + // Serialize to JSON jsonBytes, _ := json.MarshalIndent(stmts[0], "", " ") fmt.Println(string(jsonBytes)) -} -``` -Output: - -``` -SelectWithUnionQuery (children 1) - ExpressionList (children 1) - SelectQuery (children 4) - ExpressionList (children 2) - Identifier id - Identifier name - TablesInSelectQuery (children 1) - TablesInSelectQueryElement (children 1) - TableExpression (children 1) - TableIdentifier (children 1) - Identifier users - Function equals (children 1) - ExpressionList (children 2) - Identifier active - Literal UInt64_1 - ExpressionList (children 1) - OrderByElement (children 1) - Identifier created_at - Literal UInt64_10 + // Or print EXPLAIN AST output (matches ClickHouse format) + fmt.Println(parser.Explain(stmts[0])) +} ``` JSON output: @@ -95,6 +71,30 @@ JSON output: } ``` +EXPLAIN output: + +``` +SelectWithUnionQuery (children 1) + ExpressionList (children 1) + SelectQuery (children 4) + ExpressionList (children 2) + Identifier id + Identifier name + TablesInSelectQuery (children 1) + TablesInSelectQueryElement (children 1) + TableExpression (children 1) + TableIdentifier (children 1) + Identifier users + Function equals (children 1) + ExpressionList (children 2) + Identifier active + Literal UInt64_1 + ExpressionList (children 1) + OrderByElement (children 1) + Identifier created_at + Literal UInt64_10 +``` + ## Features - Parses SELECT, INSERT, CREATE, DROP, ALTER, and other ClickHouse statements