Skip to content

Commit 16c9afd

Browse files
kyleconroyclaude
andauthored
feat: add ClickHouse support to sqlc parse (#4267)
* feat: add ClickHouse database engine support Add ClickHouse support using the github.com/sqlc-dev/doubleclick parser library. New files in internal/engine/clickhouse/: - parse.go: Parser implementation using doubleclick - convert.go: AST converter from doubleclick to sqlc AST - format.go: ClickHouse-specific SQL formatting - catalog.go: Catalog initialization - stdlib.go: Standard library functions - reserved.go: Reserved keywords - utils.go: Helper functions - parse_test.go: Unit tests Supported SQL operations: - SELECT with JOINs, subqueries, CTEs, window functions - INSERT with VALUES and SELECT subquery - UPDATE and DELETE - CREATE TABLE, ALTER TABLE, DROP TABLE, TRUNCATE Co-Authored-By: Claude <[email protected]> * chore: remove test file Co-Authored-By: Claude <[email protected]> * chore: return empty function set from stdlib Co-Authored-By: Claude <[email protected]> * refactor: move ClickHouse support to parse command only Remove ClickHouse from compiler/engine.go and config.go. Add --dialect clickhouse support to the sqlc parse command. Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
1 parent 2e0435c commit 16c9afd

File tree

10 files changed

+1367
-6
lines changed

10 files changed

+1367
-6
lines changed

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/sqlc-dev/sqlc
22

3-
go 1.24.0
4-
5-
toolchain go1.24.1
3+
go 1.24.7
64

75
require (
86
github.com/antlr4-go/antlr/v4 v4.13.1
@@ -48,6 +46,7 @@ require (
4846
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect
4947
github.com/pingcap/log v1.1.0 // indirect
5048
github.com/rogpeppe/go-internal v1.10.0 // indirect
49+
github.com/sqlc-dev/doubleclick v1.0.0 // indirect
5150
github.com/stoewer/go-strcase v1.2.0 // indirect
5251
github.com/wasilibs/wazero-helpers v0.0.0-20240620070341-3dff1577cd52 // indirect
5352
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiT
157157
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
158158
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
159159
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
160+
github.com/sqlc-dev/doubleclick v1.0.0 h1:2/OApfQ2eLgcfa/Fqs8WSMA6atH0G8j9hHbQIgMfAXI=
161+
github.com/sqlc-dev/doubleclick v1.0.0/go.mod h1:ODHRroSrk/rr5neRHlWMSRijqOak8YmNaO3VAZCNl5Y=
160162
github.com/sqlc-dev/mysql v0.0.0-20251129233104-d81e1cac6db2 h1:kmCAKKtOgK6EXXQX9oPdEASIhgor7TCpWxD8NtcqVcU=
161163
github.com/sqlc-dev/mysql v0.0.0-20251129233104-d81e1cac6db2/go.mod h1:TrDMWzjNTKvJeK2GC8uspG+PWyPLiY9QKvwdWpAdlZE=
162164
github.com/stoewer/go-strcase v1.2.0 h1:Z2iHWqGXH00XYgqDmNgQbIBxf3wrNq0F3feEy0ainaU=

internal/cmd/parse.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/spf13/cobra"
1010

11+
"github.com/sqlc-dev/sqlc/internal/engine/clickhouse"
1112
"github.com/sqlc-dev/sqlc/internal/engine/dolphin"
1213
"github.com/sqlc-dev/sqlc/internal/engine/postgresql"
1314
"github.com/sqlc-dev/sqlc/internal/engine/sqlite"
@@ -27,15 +28,18 @@ Examples:
2728
echo "SELECT * FROM users" | sqlc parse --dialect mysql
2829
2930
# Parse SQLite SQL
30-
sqlc parse --dialect sqlite queries.sql`,
31+
sqlc parse --dialect sqlite queries.sql
32+
33+
# Parse ClickHouse SQL
34+
sqlc parse --dialect clickhouse queries.sql`,
3135
Args: cobra.MaximumNArgs(1),
3236
RunE: func(cmd *cobra.Command, args []string) error {
3337
dialect, err := cmd.Flags().GetString("dialect")
3438
if err != nil {
3539
return err
3640
}
3741
if dialect == "" {
38-
return fmt.Errorf("--dialect flag is required (postgresql, mysql, or sqlite)")
42+
return fmt.Errorf("--dialect flag is required (postgresql, mysql, sqlite, or clickhouse)")
3943
}
4044

4145
// Determine input source
@@ -71,8 +75,11 @@ Examples:
7175
case "sqlite":
7276
parser := sqlite.NewParser()
7377
stmts, err = parser.Parse(input)
78+
case "clickhouse":
79+
parser := clickhouse.NewParser()
80+
stmts, err = parser.Parse(input)
7481
default:
75-
return fmt.Errorf("unsupported dialect: %s (use postgresql, mysql, or sqlite)", dialect)
82+
return fmt.Errorf("unsupported dialect: %s (use postgresql, mysql, sqlite, or clickhouse)", dialect)
7683
}
7784
if err != nil {
7885
return fmt.Errorf("parse error: %w", err)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package clickhouse
2+
3+
import (
4+
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
5+
)
6+
7+
func NewCatalog() *catalog.Catalog {
8+
def := "default" // ClickHouse default database
9+
return &catalog.Catalog{
10+
DefaultSchema: def,
11+
Schemas: []*catalog.Schema{
12+
defaultSchema(def),
13+
},
14+
Extensions: map[string]struct{}{},
15+
}
16+
}

0 commit comments

Comments
 (0)