Repolode
AfterShip

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. 1

    Install the CLI with Go

    go install github.com/AfterShip/clickhouse-sql-parser@latest
  2. 2

    On macOS, install with Homebrew instead

    brew install clickhouse-sql-parser
  3. 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