mirror of
https://github.com/hanzo-ds/sqlparser.git
synced 2026-10-30 20:53:50 +00:00
This repo was previously a module-path-only rebrand; the CLI, build and docs still described the upstream project. The binary is now sqlparser. Makefile's PKG_FILES sed still stripped the UPSTREAM module prefix, so it matched nothing and left full import paths in the coverage list. main.go dropped its vendor import alias, and the package no longer shadows itself. README described the upstream project end to end: its CI, coverage and report card badges, its install path, and a brew formula we do not publish. Rewritten for this fork, and its Go examples were compiled and run rather than hand written. Fixture content that is merely arbitrary — a greeting string literal and a table name — was renamed, and the golden files REGENERATED with -update rather than edited, because they encode character offsets that shift with the text. Two classes of fixture content are deliberately left alone, and CI records why: ReplicatedMergeTree '/clickhouse/tables/...' ZooKeeper paths are persisted state (our own production server stores replicas under exactly that prefix), and SOURCE(CLICKHOUSE(...)) is a keyword in the server's dictionary grammar that this parser must accept. Renaming either would make the parser reject real SQL.
4.5 KiB
4.5 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
This is a Datastore SQL parser written in Go that parses Datastore SQL into AST (Abstract Syntax Tree) and provides SQL formatting capabilities. The project is inspired by memefish and is designed to work both as a Go library and a CLI tool.
Build and Development Commands
Build the CLI tool
make
# or
go build -o sqlparser main.go
Run tests
make test
# Runs tests with coverage, race detection, and compatible flag
Run compatible tests (for Datastore compatibility)
make test -compatible
# Tests against real Datastore SQL files from testdata/query/compatible/
Update test golden files
make update_test
# Updates expected output files in testdata/*/output/ directories
Run linting
make lint
# Uses golangci-lint with 20 minute timeout
Run benchmarks
go test -bench=. -benchmem ./parser
Architecture Overview
Core Components
Lexer (parser/lexer.go)
- Tokenizes Datastore SQL input into tokens
- Handles keywords, identifiers, operators, literals, and comments
- Supports various token types including strings, numbers, and punctuation
Parser (parser/parser_*.go)
- Modular parser split across multiple files by functionality:
parser_common.go- Core parser logic and utilitiesparser_query.go- SELECT statements and query parsingparser_table.go- CREATE TABLE and table-related DDLparser_alter.go- ALTER statementsparser_drop.go- DROP statementsparser_view.go- View-related statementsparser_column.go- Column definitions and operations
AST (parser/ast.go)
- Defines all AST node types implementing the
Exprinterface - Each node provides
Pos(),End(),String(), andAccept()methods - Supports visitor pattern for AST traversal
AST Traversal
- Walk Pattern (
parser/walk.go) - Recommended approach for AST traversalWalk(node, fn)- Depth-first traversalFind(root, predicate)- Find first matching nodeFindAll(root, predicate)- Find all matching nodesTransform(root, transformer)- Apply transformations
- Visitor Pattern (
parser/ast_visitor.go) - More complex but powerful traversal
Main Entry Point (main.go)
- CLI tool supporting parsing to AST JSON or formatting SQL
- Accepts input from command line arguments or files
Key Interfaces
Expr- Base interface for all AST nodesDDL- Interface for Data Definition Language statementsASTVisitor- Visitor pattern interface for AST traversalWalkFunc- Function type for Walk pattern traversal
Testing Strategy
The project uses a comprehensive testing approach:
Golden File Testing
- Test cases in
parser/testdata/organized by category:basic/- Simple test casesddl/- Data Definition Language testsdml/- Data Manipulation Language testsquery/- SELECT and query tests
- Expected outputs stored in
output/subdirectories as.golden.jsonfiles - Formatted SQL outputs in
format/subdirectories
Compatible Testing
- Real Datastore SQL files in
testdata/query/compatible/1_stateful/ - Run with
-compatibleflag to test against actual Datastore queries
Benchmark Testing
- Performance tests in
parser/benchmark_test.go - Tests parsing speed and memory allocation for various query types
Development Guidelines
Adding New SQL Features
- Add test cases to appropriate
testdata/subdirectory - Implement lexer tokens if needed in
lexer.go - Add AST node types to
ast.gowith all required methods - Implement parsing logic in appropriate
parser_*.gofile - Add visitor methods to
ast_visitor.goif using visitor pattern - Update Walk functions in
walk.gofor new node types - Run
make update_testto generate golden files
Parser Module Organization
- Keep parser functions organized by SQL statement type
- Use consistent naming:
parseXXX()for parsing functions - Implement proper error handling with descriptive messages
- Follow existing patterns for operator precedence and expression parsing
AST Node Implementation
- All nodes must implement
Pos(),End(),String(), andAccept()methods - String() method should regenerate valid Datastore SQL
- Accept() method must call visitor.Enter()/Leave() and visit all child nodes
** Walking the AST**
- For a new expression type, it should be also added to the
Walkfunction inwalk.go.