Skip to content

Commit 9275c63

Browse files
committed
Introduce FormatSQL API to allow customizing different formats
1 parent e5d75c5 commit 9275c63

File tree

9 files changed

+2774
-3018
lines changed

9 files changed

+2774
-3018
lines changed

AGENTS.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Repository Guidelines
2+
3+
## Project Structure & Module Organization
4+
- `main.go` is the CLI entry point (`clickhouse-sql-parser`) for AST output and SQL formatting.
5+
- `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.).
6+
- Tests live next to source as `*_test.go` files, with fixtures under `parser/testdata/`.
7+
- Fixture groups are organized by SQL type (`basic/`, `query/`, `dml/`, `ddl/`), with generated expectations in `output/` (AST JSON) and `format/` (formatted SQL).
8+
9+
## Build, Test, and Development Commands
10+
- `make` (or `go build -o clickhouse-sql-parser main.go`): build the CLI binary.
11+
- `make test`: run full test suite with race detection, coverage output (`coverage.out`), and compatibility tests.
12+
- `make update_test`: regenerate golden fixtures after intentional parser/formatter output changes.
13+
- `make lint`: run `golangci-lint` using the repo configuration.
14+
- `go test -bench=. -benchmem ./parser`: run parser benchmarks.
15+
16+
## Coding Style & Naming Conventions
17+
- Use Go 1.21 conventions (`go.mod`) and keep code `gofmt`/`goimports` clean (enforced by lint).
18+
- Place parsing logic in the matching module by statement family (for example, query parsing in `parser/parser_query.go`).
19+
- Follow existing parser naming patterns such as `parseXxx` helpers and explicit AST type names.
20+
- Keep AST `String()` output deterministic; formatting changes must be reflected in golden files.
21+
22+
## Testing Guidelines
23+
- Use Go’s `testing` package with `testify/require` assertions and `goldie` snapshot comparisons.
24+
- Add new SQL cases as `.sql` files under the appropriate `parser/testdata/<category>/` directory.
25+
- If expected outputs change, run `make update_test` and commit updated files in both `output/` and/or `format/`.
26+
- Prefer descriptive test names (`TestParser_*`, `TestWalk_*`) and subtests for per-fixture coverage.
27+
28+
## Commit & Pull Request Guidelines
29+
- Match existing commit style: concise, imperative subjects like `Add support for ...` or `Fix parsing failure ...`, optionally with issue refs (for example `(#235)`).
30+
- Keep PRs focused; describe grammar/AST impact, include representative SQL examples, and note regenerated fixtures.
31+
- Before requesting review, run `make lint` and `make test` locally to mirror CI expectations.

CLAUDE.md

Lines changed: 0 additions & 135 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ if err != nil {
6767
return nil, err
6868
}
6969

70-
// Call the String method to unparsed AST into a SQL string
70+
// Format AST back into a SQL string
7171
for _, stmt := range statements {
72-
fmt.Println(stmt.String())
72+
fmt.Println(clickhouse.Format(stmt))
7373
}
7474
```
7575

main.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ import (
1212

1313
const VERSION = "0.4.17"
1414
const help = `
15-
Usage: clickhouse-sql-parser [YOUR SQL STRING] -f [YOUR SQL FILE] -format
15+
Usage: clickhouse-sql-parser [YOUR SQL STRING] -f [YOUR SQL FILE] -format -beautify
1616
`
1717

1818
var options struct {
19-
help bool
20-
file string
21-
format bool
22-
version bool
19+
help bool
20+
file string
21+
format bool
22+
beautify bool
23+
version bool
2324
}
2425

2526
func init() {
26-
flag.BoolVar(&options.format, "format", false, "Beautify print the ClickHouse SQL")
27+
flag.BoolVar(&options.format, "format", false, "Print formatted ClickHouse SQL")
28+
flag.BoolVar(&options.beautify, "beautify", false, "Beautify print the ClickHouse SQL")
2729
flag.StringVar(&options.file, "f", "", "Parse SQL from file")
2830
flag.BoolVar(&options.help, "h", false, "Print help message")
2931
flag.BoolVar(&options.version, "v", false, "Print version")
@@ -61,12 +63,19 @@ func main() {
6163
fmt.Fprintf(os.Stderr, "parse statements error: %s\n", err.Error())
6264
os.Exit(1)
6365
}
64-
if !options.format { // print AST
66+
if !options.format && !options.beautify { // print AST
6567
bytes, _ := json.MarshalIndent(stmts, "", " ") // nolint
6668
fmt.Println(string(bytes))
6769
} else { // format SQL
6870
for _, stmt := range stmts {
69-
fmt.Println(stmt.String())
71+
if options.beautify {
72+
formatter := clickhouse.NewFormatter()
73+
formatter.WithBeautify()
74+
formatter.WriteExpr(stmt)
75+
fmt.Println(formatter.String())
76+
} else {
77+
fmt.Println(clickhouse.Format(stmt))
78+
}
7079
}
7180
}
7281
}

0 commit comments

Comments
 (0)