A Java SQL parser that turns SQL statements into a traversable tree of Java objects and can render them back to SQL text.
AfterShip/clickhouse-sql-parser
A Go library and CLI that parses ClickHouse-dialect SQL into a typed AST and formats or beautifies queries.
- Stars
- 255
- Forks
- 62
- Open issues
- 1
- Last commit
- 3 days ago
What is it?
clickhouse-sql-parser turns ClickHouse SQL statements into a typed abstract syntax tree (AST) in Go. It can be imported as a library or run as a CLI that prints the AST, outputs a compact single-line format, or beautifies queries with indentation and line breaks.
The library exposes a parser with ParseStmts, a formatter that turns AST nodes back into SQL, and traversal helpers such as Walk, WalkWithBreak, Find, FindAll and Transform. These make it possible to inspect or rewrite queries programmatically without writing a grammar yourself.
The project is inspired by memefish, a Spanner SQL parser in Go. Its test assets for output and format are generated by test cases and validated against clickhouse-local when new syntax is added.
Key features
- 01Parses ClickHouse SQL statements into a typed AST in Go.
- 02Formats AST nodes back into SQL strings.
- 03Beautifies queries with indentation and line breaks.
- 04Provides Walk, Find, FindAll and Transform helpers for traversing and rewriting trees.
- 05Available as a Go library and a CLI, installable via go install or Homebrew.
Use cases
- Linting or statically analyzing ClickHouse queries.
- Enforcing access control by inspecting tables referenced in a query.
- Rewriting queries programmatically before execution.
- Formatting ClickHouse SQL in editors or pipelines.
Quick start
- 1
Install the CLI with Go
go install github.com/AfterShip/clickhouse-sql-parser@latest - 2
On macOS, install with Homebrew instead
brew install clickhouse-sql-parser - 3
Parse or format a query
clickhouse-sql-parser -beautify "SELECT * FROM clickhouse WHERE a=100"
Things to know
- Parsing covers ClickHouse dialect syntax only, so newer or unusual SQL may not be supported.
In collections
Topics
FAQ
- What is clickhouse-sql-parser?
- A Go library and CLI that parses ClickHouse-dialect SQL into a typed AST and formats or beautifies queries. clickhouse-sql-parser turns ClickHouse SQL statements into a typed abstract syntax tree (AST) in Go. It can be imported as a library or run as a CLI that prints the AST, outputs a compact single-line format, or beautifies queries with indentation and line breaks.
- Is clickhouse-sql-parser free to use? What license does it have?
- clickhouse-sql-parser is open source under the MIT license. Check the license terms before using it in commercial products.
- Is clickhouse-sql-parser still maintained?
- Actively maintained. The last commit to clickhouse-sql-parser was on Nov 4, 2026.
- What can I use clickhouse-sql-parser for?
- Linting or statically analyzing ClickHouse queries. Enforcing access control by inspecting tables referenced in a query. Rewriting queries programmatically before execution. Formatting ClickHouse SQL in editors or pipelines.
- How do I install clickhouse-sql-parser?
- Install the CLI with Go: go install github.com/AfterShip/clickhouse-sql-parser@latest On macOS, install with Homebrew instead: brew install clickhouse-sql-parser Parse or format a query: clickhouse-sql-parser -beautify "SELECT * FROM clickhouse WHERE a=100"
- Where can I get clickhouse-sql-parser?
- The source code and installation instructions are on GitHub: https://github.com/AfterShip/clickhouse-sql-parser
Summary generated by AI from the project's README. GitHub stats refresh daily.
Related projects
Rust/WASM SQL transpiler that parses, generates, transpiles and formats SQL across more than 30 dialects, inspired by sqlglot.
A PHP parser written in PHP that turns PHP source code into an abstract syntax tree for static analysis and code manipulation.
A CLI tool that searches, lints, and rewrites code by matching abstract syntax tree patterns instead of plain text.
A TypeScript wrapper around the TypeScript Compiler API for analyzing and programmatically changing TypeScript and JavaScript code.
A Java 1 to Java 25 parser that builds an abstract syntax tree, plus a symbol solver for resolving declarations and types.