This document outlines guidance for AI coding agent including project structure, coding style, testing, and contribution practices for the ClickHouse SQL Parser project.
# Build the CLI binary
make
# Run tests
make test
# Update golden fixtures after intentional output changes
make update_testAfter editing code, use goimports and gofmt to maintain code style, and run make lint to check for any issues before committing or requesting a review.
main.gois the CLI entry point (clickhouse-sql-parser) for AST output and SQL formatting.parser/contains core parser code: lexer (lexer.go), AST definitions (ast.go), traversal helpers (walk.go), and grammar-specific parser files (parser_query.go,parser_table.go,parser_alter.go, etc.).- Tests live next to source as
*_test.gofiles, with fixtures underparser/testdata/. - Fixture groups are organized by SQL type (
basic/,query/,dml/,ddl/), with generated expectations inoutput/(AST JSON) andformat/(formatted SQL).
- Use Go 1.21 conventions (
go.mod) and keep codegofmt/goimportsclean (enforced by lint). - Naming is the most important style aspect, try you best to choose a clear and descriptive name for variables, functions, types, and files. For example, use
parseSelectfor a function that parses a SELECT statement, andSelectStatementfor the corresponding AST node type. - Place parsing logic in the matching module by statement family (for example, query parsing in
parser/parser_query.go). - Follow existing parser naming patterns such as
parseXxxhelpers and explicit AST type names. - Keep AST
FormatSQL()output deterministic; formatting changes must be reflected in golden files. - You must go through the repository before adding new code to ensure consistency with existing patterns and styles. If you are unsure about where to place new code or how to format it, please refer to the existing codebase or ask for guidance.
- Reusing existing code and patterns is encouraged to maintain consistency and reduce redundancy. If you find a similar function or pattern in the codebase, consider adapting it for your needs instead of creating something new from scratch.
- Use Go’s
testingpackage withtestify/requireassertions andgoldiesnapshot comparisons. - Add new SQL cases as
.sqlfiles under the appropriateparser/testdata/<category>/directory. - If expected outputs change, run
make update_testand commit updated files in bothoutput/and/orformat/. - Prefer descriptive test names (
TestParser_*,TestWalk_*) and subtests for per-fixture coverage.
- Match existing commit style: concise, imperative subjects like
Add support for ...orFix parsing failure ..., optionally with issue refs (for example(#235)). - Keep PRs focused; describe grammar/AST impact, include representative SQL examples, and note regenerated fixtures.
- Before requesting review, run
make lintandmake testlocally to mirror CI expectations.
- You must confirm it's correctly added to
visitor.go,walk.goandformat.gowhen adding a new expression or statement type. This ensures that the new AST node is properly traversed and formatted. - Newly added test cases must be concise and cover the core functionality being tested first.