{"id":20516160,"url":"https://github.com/aftership/clickhouse-sql-parser","last_synced_at":"2025-12-25T06:13:23.507Z","repository":{"id":196217212,"uuid":"694946961","full_name":"AfterShip/clickhouse-sql-parser","owner":"AfterShip","description":"ClickHouse SQL Parser writing in Go","archived":false,"fork":false,"pushed_at":"2024-10-23T04:02:04.000Z","size":614,"stargazers_count":141,"open_issues_count":3,"forks_count":16,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-24T18:52:28.303Z","etag":null,"topics":["clickhouse","go","sql-parser"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AfterShip.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-22T02:56:16.000Z","updated_at":"2024-10-23T17:59:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"98d554b5-61aa-4312-b7b6-b5d542403c8c","html_url":"https://github.com/AfterShip/clickhouse-sql-parser","commit_stats":null,"previous_names":["aftership/clickhouse-sql-parser"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AfterShip%2Fclickhouse-sql-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AfterShip%2Fclickhouse-sql-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AfterShip%2Fclickhouse-sql-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AfterShip%2Fclickhouse-sql-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AfterShip","download_url":"https://codeload.github.com/AfterShip/clickhouse-sql-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247128747,"owners_count":20888235,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clickhouse","go","sql-parser"],"created_at":"2024-11-15T21:27:05.119Z","updated_at":"2025-12-25T06:13:23.494Z","avatar_url":"https://github.com/AfterShip.png","language":"Go","readme":"# ClickHouse SQL Parser \n![GitHub CI](https://github.com/AfterShip/clickhouse-sql-parser/actions/workflows/ci.yaml/badge.svg) [![Go Report Card](https://goreportcard.com/badge/github.com/AfterShip/clickhouse-sql-parser)](https://goreportcard.com/report/github.com/AfterShip/clickhouse-sql-parser) [![LICENSE](https://img.shields.io/github/license/AfterShip/clickhouse-sql-parser.svg)](https://github.com/AfterShip/clickhouse-sql-parser/blob/master/LICENSE) [![GoDoc](https://img.shields.io/badge/Godoc-reference-blue.svg)](https://godoc.org/github.com/AfterShip/clickhouse-sql-parser) [![Coverage Status](https://coveralls.io/repos/github/AfterShip/clickhouse-sql-parser/badge.svg?branch=master)](https://coveralls.io/github/AfterShip/clickhouse-sql-parser?branch=master) \u003ca href=\"https://hellogithub.com/repository/23597949cafa410bba6039ddb8867543\" target=\"_blank\"\u003e\u003cimg src=\"https://api.hellogithub.com/v1/widgets/recommend.svg?rid=23597949cafa410bba6039ddb8867543\u0026claim_uid=kyCYu1VAKgwD8rE\u0026theme=small\" alt=\"Featured｜HelloGitHub\" /\u003e\u003c/a\u003e\n\nThe goal of this project is to build a ClickHouse SQL parser in Go with the following key features:\n\n- Parse ClickHouse SQL into AST\n- Beautify ClickHouse SQL format\n\nThis project is inspired by [memefish](https://github.com/cloudspannerecosystem/memefish) which is a SQL parser for Spanner in Go.\n## How to use\n\nYou can use it as your Go library or CLI tool, see the following examples:\n\n- Use clickhouse-sql-parser as a Go library\n\n```Go\npackage main\n\nimport (\n    clickhouse \"github.com/AfterShip/clickhouse-sql-parser/parser\"\n)\n\nquery := \"SELECT * FROM clickhouse\"\nparser := clickhouse.NewParser(query)\n// Parse query into AST\nstatements, err := parser.ParseStmts()\nif err != nil {\n    return nil, err\n}\n```\n\n- Install clickhouse-sql-parser as a CLI tool\n\n\nOn Linux:\n\n```bash\n$ go install github.com/AfterShip/clickhouse-sql-parser@latest\n```\n\nOn macOS:\n\n```bash\n$ brew install clickhouse-sql-parser\n```\n\nParse ClickHouse SQL into AST or beautify ClickHouse SQL format:\n\n```bash\n## Parse query into AST\n$ clickhouse-sql-parser \"SELECT * FROM clickhouse WHERE a=100\"\n\n## Beautify query\n$ clickhouse-sql-parser -format \"SELECT * FROM clickhouse WHERE a=100\"\n\n## Parse query from file\n$ clickhouse-sql-parser -file ./test.sql\n```\n\n- Parsed tree(AST) back into a SQL statement\n\n```Go\nparser := clickhouse.NewParser(\"SELECT * FROM clickhouse\")\n// Parse query into AST\nstatements, err := parser.ParseStmts()\nif err != nil {\n    return nil, err\n}\n\n// Call the String method to unparsed AST into a SQL string\nfor _, stmt := range statements {\n  fmt.Println(stmt.String())\n}\n```\n\n## AST Traversal\n\n### Walk Pattern (Recommended)\n\nThe Walk pattern provides a simple and efficient way to traverse AST nodes. Use the `Walk` function to visit all nodes in the AST:\n\n```Go\nimport (\n    clickhouse \"github.com/AfterShip/clickhouse-sql-parser/parser\"\n)\n\nparser := clickhouse.NewParser(\"SELECT * FROM table WHERE id = 1\")\nstatements, err := parser.ParseStmts()\nif err != nil {\n    return err\n}\n\n// Walk through all nodes in the AST\nclickhouse.Walk(statements[0], func(node clickhouse.Expr) bool {\n    fmt.Printf(\"Node type: %T\\n\", node)\n    return true // return false to stop traversal for this subtree\n})\n```\n\n#### Walk Pattern Functions\n\n- **`Walk(node Expr, fn WalkFunc)`** - Traverses all nodes in depth-first order\n- **`WalkWithBreak(node Expr, fn WalkFunc)`** - Allows early termination of traversal\n- **`Find(root Expr, predicate func(Expr) bool)`** - Finds the first node matching a condition\n- **`FindAll(root Expr, predicate func(Expr) bool)`** - Finds all nodes matching a condition\n- **`Transform(root Expr, transformer func(Expr) Expr)`** - Applies transformations to nodes\n\n#### Examples\n\nFind all table identifiers:\n```Go\ntables := clickhouse.FindAll(stmt, func(node clickhouse.Expr) bool {\n    _, ok := node.(*clickhouse.TableIdentifier)\n    return ok\n})\n```\n\nFind the first WHERE clause:\n```Go\nwhereClause, found := clickhouse.Find(stmt, func(node clickhouse.Expr) bool {\n    _, ok := node.(*clickhouse.WhereClause)\n    return ok\n})\n```\n\n## Update test assets\n\nFor the files inside `output` and `format` dir are generated by the test cases,\n\nif you want to update them, you can run the following command:\n\n```bash\n$ make update_test\n```\n\n## Benchmarks\n\n```sh\ngo test -bench=. -benchmem ./parser\n```\n\nResults\n\n```\n$ go test -bench=. -benchmem ./parser\ngoos: linux\ngoarch: amd64\npkg: github.com/AfterShip/clickhouse-sql-parser/parser\ncpu: Intel(R) Xeon(R) CPU E5-2697 v3 @ 2.60GHz\nBenchmarkParseSQLFiles/access_tuple_with_dot.sql-28                23294             58467 ns/op           13448 B/op        293 allocs/op\nBenchmarkParseSQLFiles/query_with_expr_compare.sql-28              43560             25704 ns/op            6240 B/op        132 allocs/op\nBenchmarkParseSQLFiles/select_cast.sql-28                          75055             16518 ns/op            4648 B/op         92 allocs/op\nBenchmarkParseSQLFiles/select_column_alias_string.sql-28          499798              2785 ns/op             704 B/op         13 allocs/op\nBenchmarkParseSQLFiles/select_expr.sql-28                         488187              2448 ns/op             696 B/op         12 allocs/op\nBenchmarkParseSQLFiles/select_item_with_modifiers.sql-28           54124             23305 ns/op            6232 B/op        136 allocs/op\nBenchmarkParseSQLFiles/select_order_by_timestamp.sql-28           232302              5809 ns/op            1368 B/op         28 allocs/op\nBenchmarkParseSQLFiles/select_simple.sql-28                        30602             50022 ns/op            9920 B/op        216 allocs/op\nBenchmarkParseSQLFiles/select_simple_field_alias.sql-28           178126              6316 ns/op            1712 B/op         39 allocs/op\nBenchmarkParseSQLFiles/select_simple_with_bracket.sql-28           71902             16172 ns/op            3824 B/op         85 allocs/op\nBenchmarkParseSQLFiles/select_simple_with_cte_with_column_aliases.sql-28                   67050             19916 ns/op            4648 B/op        101 allocs/op\nBenchmarkParseSQLFiles/select_simple_with_group_by_with_cube_totals.sql-28                107047             10348 ns/op            2768 B/op         58 allocs/op\nBenchmarkParseSQLFiles/select_simple_with_is_not_null.sql-28                               55285             21957 ns/op            5224 B/op        111 allocs/op\nBenchmarkParseSQLFiles/select_simple_with_is_null.sql-28                                   66648             22412 ns/op            4728 B/op        102 allocs/op\nBenchmarkParseSQLFiles/select_simple_with_top_clause.sql-28                               269910              4166 ns/op            1088 B/op         22 allocs/op\nBenchmarkParseSQLFiles/select_simple_with_with_clause.sql-28                               58494             18417 ns/op            5144 B/op        109 allocs/op\nBenchmarkParseSQLFiles/select_table_alias_without_keyword.sql-28                          121261             10011 ns/op            2896 B/op         65 allocs/op\nBenchmarkParseSQLFiles/select_table_function_with_query.sql-28                             98017             14929 ns/op            4168 B/op         81 allocs/op\nBenchmarkParseSQLFiles/select_when_condition.sql-28                                       220394              5457 ns/op            1304 B/op         28 allocs/op\nBenchmarkParseSQLFiles/select_with_distinct.sql-28                                        172948              6531 ns/op            1560 B/op         33 allocs/op\nBenchmarkParseSQLFiles/select_with_join_only.sql-28                                       286346              5594 ns/op            1520 B/op         32 allocs/op\nBenchmarkParseSQLFiles/select_with_left_join.sql-28                                        88200             13627 ns/op            3880 B/op         75 allocs/op\nBenchmarkParseSQLFiles/select_with_literal_table_name.sql-28                              241094              5099 ns/op            1304 B/op         27 allocs/op\nBenchmarkParseSQLFiles/select_with_multi_join.sql-28                                       44700             31964 ns/op            8240 B/op        188 allocs/op\nBenchmarkParseSQLFiles/select_with_multi_line_comment.sql-28                              363499              4460 ns/op             824 B/op         18 allocs/op\nBenchmarkParseSQLFiles/select_with_multi_union.sql-28                                     146233              7827 ns/op            2176 B/op         36 allocs/op\nBenchmarkParseSQLFiles/select_with_number_field.sql-28                                    129945              8746 ns/op            2352 B/op         51 allocs/op\nBenchmarkParseSQLFiles/select_with_query_parameter.sql-28                                  33850             36346 ns/op            9936 B/op        209 allocs/op\nBenchmarkParseSQLFiles/select_with_string_expr.sql-28                                     142882              7530 ns/op            1880 B/op         34 allocs/op\nBenchmarkParseSQLFiles/select_with_union_distinct.sql-28                                  147031              9601 ns/op            2352 B/op         47 allocs/op\nBenchmarkParseSQLFiles/select_with_variable.sql-28                                        179158              6669 ns/op            1880 B/op         36 allocs/op\nBenchmarkParseSQLFiles/select_with_window_function.sql-28                                  54925             31320 ns/op            6720 B/op        136 allocs/op\nBenchmarkParseSQLFiles/select_with_placeholder.sql-28                                    196771              5145 ns/op            1272 B/op         26 allocs/op\nBenchmarkParseSQLFiles/set_simple.sql-28                                                  172419              7062 ns/op            2480 B/op         49 allocs/op\nBenchmarkParseComplexQueries/testdata/query/select_with_multi_join.sql-28                  39056             36897 ns/op            8240 B/op        188 allocs/op\nBenchmarkParseComplexQueries/testdata/query/select_with_window_function.sql-28             47629             29916 ns/op            6720 B/op        136 allocs/op\nBenchmarkParseComplexQueries/testdata/query/select_simple_with_with_clause.sql-28                  69210             19731 ns/op            5144 B/op        109 allocs/op\nBenchmarkParseComplexQueries/testdata/query/select_with_left_join.sql-28                           74576             15338 ns/op            3880 B/op         75 allocs/op\nBenchmarkParseComplexQueries/testdata/benchdata/posthog_huge_0.sql-28                                235           6231253 ns/op         1236189 B/op      26696 allocs/op\nBenchmarkParseComplexQueries/testdata/benchdata/posthog_huge_1.sql-28                                279           4438280 ns/op         1043374 B/op      22717 allocs/op\nPASS\nok      github.com/AfterShip/clickhouse-sql-parser/parser       66.547s\n```\n\n## Contact us\n\nFeel free to open an issue or discussion if you have any issues or questions.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faftership%2Fclickhouse-sql-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faftership%2Fclickhouse-sql-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faftership%2Fclickhouse-sql-parser/lists"}