Skip to content

Commit 15d203a

Browse files
committed
Add ClickHouse documentation and example project
Getting started guide, configuration and datatype reference updates, example project with schema and queries, development guide updates, Docker Compose configuration for ClickHouse service.
1 parent cec1ed9 commit 15d203a

File tree

15 files changed

+1334
-16
lines changed

15 files changed

+1334
-16
lines changed

CLAUDE.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ POSTGRESQL_SERVER_URI="postgres://postgres:mysecretpassword@localhost:5432/postg
189189
- `/cmd/` - Main binaries (sqlc, sqlc-gen-json)
190190
- `/internal/cmd/` - Command implementations (vet, generate, etc.)
191191
- `/internal/engine/` - Database engine implementations
192+
- `/clickhouse/` - ClickHouse parser and converter
192193
- `/postgresql/` - PostgreSQL parser and converter
193194
- `/dolphin/` - MySQL parser (uses TiDB parser)
194195
- `/sqlite/` - SQLite parser
@@ -289,20 +290,71 @@ git push --force-with-lease origin <feature-branch>
289290
- **CI Configuration:** `/.github/workflows/ci.yml`
290291
- **Docker Compose:** `/docker-compose.yml`
291292

293+
## ClickHouse Engine Support
294+
295+
### Implementation Details
296+
297+
The ClickHouse engine was added to support ClickHouse SQL parsing and code generation using the `github.com/AfterShip/clickhouse-sql-parser` library.
298+
299+
**Files involved:**
300+
- `internal/engine/clickhouse/parse.go` - Parser implementation
301+
- `internal/engine/clickhouse/convert.go` - AST converter
302+
- `internal/engine/clickhouse/catalog.go` - Catalog initialization
303+
- `internal/config/config.go` - Engine constant registration
304+
- `internal/compiler/engine.go` - Compiler integration
305+
- `internal/compiler/expand.go` - Quote character handling
306+
- `examples/clickhouse/` - Example project
307+
308+
**Key features:**
309+
- Parses ClickHouse SQL into sqlc's internal AST
310+
- Supports comment syntax: `--` and `/* */`
311+
- Uses backticks for identifier quoting (ClickHouse standard)
312+
- Handles ClickHouse-specific keywords
313+
314+
**Testing:**
315+
```bash
316+
# Test ClickHouse engine (unit tests)
317+
go test ./internal/engine/clickhouse -v
318+
319+
# Test compiler integration
320+
go test ./internal/compiler -v -run TestNewCompilerClickHouse
321+
322+
# Test code generation
323+
cd examples/clickhouse && /path/to/sqlc generate && go mod init github.com/example/clickhouse && go mod tidy && go build ./gen
324+
```
325+
326+
**Test Coverage:**
327+
- Basic SELECT queries with WHERE, ORDER BY, LIMIT
328+
- INSERT statements with VALUES
329+
- Aggregate queries (COUNT, SUM, AVG) with GROUP BY and HAVING
330+
- UNION and UNION ALL queries
331+
- Subqueries and derived tables
332+
- Multiple JOINs (INNER, LEFT, RIGHT, FULL)
333+
- Window functions with OVER clause
334+
- CAST expressions
335+
- CASE expressions
336+
- IS NULL / IS NOT NULL expressions
337+
- Unary expressions (NOT, negation)
338+
- Number literals (int, float, scientific notation)
339+
- String literals and functions
340+
- ClickHouse-specific features (PREWHERE, SAMPLE, array functions)
341+
292342
## Recent Fixes & Improvements
293343

294344
### Fixed Issues
295345

296346
1. **Typo in create_function_stmt.go** - Fixed "Undertand" → "Understand"
297347
2. **Race condition in vet.go** - Fixed Client initialization using `sync.Once`
298348
3. **Nil pointer dereference in parse.go** - Fixed unsafe type assertion in primary key parsing
349+
4. **ClickHouse engine addition** - Added full ClickHouse SQL support with AST parsing
299350

300351
These fixes demonstrate common patterns:
301352
- Using `sync.Once` for thread-safe lazy initialization
302353
- Using comma-ok idiom for safe type assertions: `if val, ok := x.(Type); ok { ... }`
303354
- Adding proper nil checks and defensive programming
355+
- Registering new database engines following the established pattern
304356

305357
---
306358

307-
**Last Updated:** 2025-10-21
359+
**Last Updated:** 2025-11-19
308360
**Maintainer:** Claude Code

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test:
1010
go test ./...
1111

1212
test-managed:
13-
MYSQL_SERVER_URI="invalid" POSTGRESQL_SERVER_URI="postgres://postgres:mysecretpassword@localhost:5432/postgres" go test -v ./...
13+
MYSQL_SERVER_URI="invalid" POSTGRESQL_SERVER_URI="postgres://postgres:mysecretpassword@localhost:5432/postgres" CLICKHOUSE_SERVER_URI="localhost:9000" go test ./...
1414

1515
vet:
1616
go vet ./...

docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ services:
1919
POSTGRES_DB: postgres
2020
POSTGRES_PASSWORD: mysecretpassword
2121
POSTGRES_USER: postgres
22+
23+
clickhouse:
24+
image: "clickhouse:lts"
25+
ports:
26+
- "9000:9000"
27+
- "8123:8123"
28+
restart: always

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ code ever again.
3535
tutorials/getting-started-mysql.md
3636
tutorials/getting-started-postgresql.md
3737
tutorials/getting-started-sqlite.md
38+
tutorials/getting-started-clickhouse.md
3839

3940
.. toctree::
4041
:maxdepth: 2

docs/reference/config.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Each mapping in the `sql` collection has the following keys:
3737
- `name`:
3838
- An human-friendly identifier for this query set. Optional.
3939
- `engine`:
40-
- One of `postgresql`, `mysql` or `sqlite`.
40+
- One of `postgresql`, `mysql`, `sqlite` or `clickhouse`.
4141
- `schema`:
4242
- Directory of SQL migrations or path to single SQL file; or a list of paths.
4343
- `queries`:
@@ -134,7 +134,8 @@ The `gen` mapping supports the following keys:
134134
- `out`:
135135
- Output directory for generated code.
136136
- `sql_package`:
137-
- Either `pgx/v4`, `pgx/v5` or `database/sql`. Defaults to `database/sql`.
137+
- Either `pgx/v4`, `pgx/v5`, `database/sql` or `clickhouse/v2`. Defaults to `database/sql`.
138+
- For ClickHouse, use `clickhouse/v2` for the native driver or omit for `database/sql` compatibility.
138139
- `sql_driver`:
139140
- Either `github.com/jackc/pgx/v4`, `github.com/jackc/pgx/v5`, `github.com/lib/pq` or `github.com/go-sql-driver/mysql`. No defaults. Required if query annotation `:copyfrom` is used.
140141
- `emit_db_tags`:
@@ -158,7 +159,7 @@ The `gen` mapping supports the following keys:
158159
- `emit_methods_with_db_argument`:
159160
- If true, generated methods will accept a DBTX argument instead of storing a DBTX on the `*Queries` struct. Defaults to `false`.
160161
- `emit_pointers_for_null_types`:
161-
- If true, generated types for nullable columns are emitted as pointers (ie. `*string`) instead of `database/sql` null types (ie. `NullString`). Currently only supported for PostgreSQL if `sql_package` is `pgx/v4` or `pgx/v5`, and for SQLite. Defaults to `false`.
162+
- If true, generated types for nullable columns are emitted as pointers (ie. `*string`) instead of `database/sql` null types (ie. `NullString`). Currently only supported for PostgreSQL if `sql_package` is `pgx/v4` or `pgx/v5`, SQLite, and ClickHouse if `sql_package` is `clickhouse/v2`. Defaults to `false`.
162163
- `emit_enum_valid_method`:
163164
- If true, generate a Valid method on enum types,
164165
indicating whether a string is a valid enum value.

docs/reference/datatypes.md

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ If you're unsatisfied with the default, you can override any type using the
88

99
## Arrays
1010

11-
PostgreSQL [arrays](https://www.postgresql.org/docs/current/arrays.html) are
12-
materialized as Go slices.
11+
PostgreSQL [arrays](https://www.postgresql.org/docs/current/arrays.html) are materialized as Go slices.
1312

1413
```sql
1514
CREATE TABLE places (
@@ -27,6 +26,24 @@ type Place struct {
2726
}
2827
```
2928

29+
ClickHouse [array types](https://clickhouse.com/docs/en/sql-reference/data-types/array) are similarly mapped to Go slices:
30+
31+
```sql
32+
CREATE TABLE data (
33+
tags Array(String),
34+
ids Array(UInt32)
35+
);
36+
```
37+
38+
```go
39+
package db
40+
41+
type Data struct {
42+
Tags []string
43+
IDs []uint32
44+
}
45+
```
46+
3047
## Dates and times
3148

3249
All date and time types are returned as `time.Time` structs. For
@@ -60,6 +77,28 @@ type Author struct {
6077
}
6178
```
6279

80+
ClickHouse `DateTime` and `Date` types are also mapped to `time.Time`:
81+
82+
```sql
83+
CREATE TABLE events (
84+
created_at DateTime,
85+
event_date Date
86+
);
87+
```
88+
89+
```go
90+
package db
91+
92+
import (
93+
"time"
94+
)
95+
96+
type Event struct {
97+
CreatedAt time.Time
98+
EventDate time.Time
99+
}
100+
```
101+
63102
## Enums
64103

65104
PostgreSQL [enums](https://www.postgresql.org/docs/current/datatype-enum.html) are
@@ -120,6 +159,46 @@ type Author struct {
120159
}
121160
```
122161

162+
ClickHouse uses `Nullable(T)` for nullable columns. When using the native `clickhouse/v2`
163+
driver with `emit_pointers_for_null_types: true`, nullable fields are represented as
164+
pointers. With `database/sql`, they use the standard `sql.Null*` types:
165+
166+
```sql
167+
CREATE TABLE articles (
168+
id UInt64,
169+
title String,
170+
bio Nullable(String)
171+
);
172+
```
173+
174+
With `clickhouse/v2` and `emit_pointers_for_null_types: true`:
175+
176+
```go
177+
package db
178+
179+
type Article struct {
180+
ID uint64
181+
Title string
182+
Bio *string
183+
}
184+
```
185+
186+
With `database/sql`:
187+
188+
```go
189+
package db
190+
191+
import (
192+
"database/sql"
193+
)
194+
195+
type Article struct {
196+
ID uint64
197+
Title string
198+
Bio sql.NullString
199+
}
200+
```
201+
123202
## UUIDs
124203

125204
The Go standard library does not come with a `uuid` package. For UUID support,

docs/reference/language-support.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
Database and language support
22
#############################
33

4-
========== ======================= ============ ============ ===============
5-
Language Plugin MySQL PostgreSQL SQLite
6-
========== ======================= ============ ============ ===============
7-
Go (built-in) Stable Stable Beta
8-
Go `sqlc-gen-go`_ Stable Stable Beta
9-
Kotlin `sqlc-gen-kotlin`_ Beta Beta Not implemented
10-
Python `sqlc-gen-python`_ Beta Beta Not implemented
11-
TypeScript `sqlc-gen-typescript`_ Beta Beta Not implemented
12-
========== ======================= ============ ============ ===============
4+
========== ======================= ============ ============ =============== ===============
5+
Language Plugin MySQL PostgreSQL SQLite ClickHouse
6+
========== ======================= ============ ============ =============== ===============
7+
Go (built-in) Stable Stable Beta Beta
8+
Go `sqlc-gen-go`_ Stable Stable Beta Beta
9+
Kotlin `sqlc-gen-kotlin`_ Beta Beta Not implemented Not implemented
10+
Python `sqlc-gen-python`_ Beta Beta Not implemented Not implemented
11+
TypeScript `sqlc-gen-typescript`_ Beta Beta Not implemented Not implemented
12+
========== ======================= ============ ============ =============== ===============
1313

1414
Community language support
1515
**************************

0 commit comments

Comments
 (0)