Skip to content

Conversation

@mgilbir
Copy link

@mgilbir mgilbir commented Dec 6, 2025

Overview

This PR introduces comprehensive support for ClickHouse as a database engine in sqlc. It enables users to write ClickHouse queries in .sql files and generate type-safe Go code using sqlc's familiar workflow.

Key Features

  • ClickHouse SQL Parser: Full SQL parsing support using the github.com/AfterShip/clickhouse-sql-parser library
  • Type Mapping: ClickHouse types mapped to Go types with support for ClickHouse-specific types (Array, LowCardinality, Map, etc.)
  • Code Generation: Generates Go database interfaces, model types, and query functions for ClickHouse operations
  • JOIN USING Support: New SQL feature supporting JOIN...USING syntax, working with both ClickHouse and PostgreSQL
  • Comprehensive Testing: 25+ end-to-end test cases covering basic operations, advanced SQL features, type handling, and functions

Production Status

This implementation has been in production use for approximately one month. The core parser and code generation have proven stable for our queries, but I'm eager for community feedback on edge cases and other query patterns.

Large PR Notice

⚠️ This is a substantial PR (~17,000 lines across 13 commits), but the impact on existing code is minimal:

  • New engine implementation is isolated in /internal/engine/clickhouse/
  • Code generation templates are isolated in /internal/codegen/golang/templates/clickhouse/
  • Core changes to existing files are limited to:
    • Adding ClickHouse engine registration in the compiler
    • Minor type system extensions for ClickHouse types
    • Refactoring and improving output_columns.go (backward compatible)
    • Adding JOIN...USING support (benefits multiple engines)

The majority of the PR size comes from test data and generated code examples, which are valuable for validation and user documentation.

Feedback Welcome

I'm committed to making this PR production-ready and welcome:

  • Architecture review of the engine implementation
  • Bug reports for queries that fail to parse or generate incorrectly
  • Feature requests for missing ClickHouse capabilities
  • Code quality feedback and suggestions for improvement
  • Test cases for any edge cases you encounter

If you have ClickHouse queries that don't work with this implementation, please share them. I'm particularly interested in:

  • Complex subqueries and CTEs
  • Advanced window functions
  • ClickHouse-specific features (PREWHERE, SAMPLE, array operations, etc.)
  • Edge cases in type resolution

Commit Structure

The 13 commits are organized to facilitate review, with test data grouped at the end:

1. Core Engine Implementation (2 commits)

  • Add ClickHouse engine: parser, converter, and catalog - Foundation for ClickHouse SQL parsing
  • Add ClickHouse engine unit tests - Comprehensive unit tests for the parser

2. Integration Layer (2 commits)

  • Register ClickHouse engine in compiler - Wires the engine into the compiler pipeline
  • Add ClickHouse type mapping for Go code generation - Maps ClickHouse types to Go types

3. Code Generation (2 commits)

  • Add ClickHouse code generation templates - Templates for generating Go code
  • Add JOIN USING support and refactor output column handling - New SQL feature and compiler improvements

4. Infrastructure & Documentation (2 commits)

  • Add ClickHouse test database adapters - Docker and local test containers
  • Add ClickHouse documentation and example project - User-facing docs and complete working example

5. Generated Code & Examples (1 commit)

  • Add ClickHouse example project generated code - Shows what users get from sqlc generate

6. End-to-End Tests (4 commits)

  • Add end-to-end test for JOIN...USING syntax - Validates feature works with PostgreSQL
  • Add end-to-end tests for ClickHouse core SQL features - SELECT, DML, pagination, etc.
  • Add end-to-end tests for ClickHouse advanced SQL features - JOINs, subqueries, CTEs, aggregates
  • Add end-to-end tests for ClickHouse types and functions - Type handling, arrays, functions, named parameters

Review Strategy: Early commits establish the core logic before test data, making the implementation easy to understand and validate.

What's Included

  • Full ClickHouse SQL parser integration
  • 25 end-to-end test cases with complete schemas and generated code
  • Working example project in /examples/clickhouse/
  • Getting started documentation and API reference updates
  • Docker Compose configuration for testing with ClickHouse
  • Support for: SELECT, INSERT, UPDATE, DELETE, CTEs, JOINs, subqueries, aggregates, type casting, built-in functions, and more

Review suggestion

  1. Start with commits 1-2 to understand the parser architecture
  2. Review commits 3-5 for integration and code generation
  3. Check commit 6 for the JOIN USING implementation (useful for all databases)
  4. Skim the test data commits (7-13) to see coverage breadth

Looking forward to your feedback and excited to bring ClickHouse support to sqlc!

Introduces ClickHouse SQL parser integration using github.com/AfterShip/clickhouse-sql-parser.
Includes AST converter to sqlc's internal representation, catalog initialization,
and type resolver. Foundation for ClickHouse support.
Comprehensive test coverage for parser and converter:
- Basic parsing functionality (parse_test.go)
- AST conversion correctness (new_conversions_test.go)
- Catalog initialization (catalog_test.go)
- Edge cases and boundary conditions (parse_boundary_test.go)
- Real-world queries (parse_actual_queries_test.go)
- Type handling, identifiers, joins, arrays
Add engine registration in compiler pipeline and configuration schema.
Enables compiler to recognize and process ClickHouse engine configurations.
Map ClickHouse types to Go types, update driver handling,
and add import management for ClickHouse types.
Templates for generating Go database code:
- Database interface template
- Interface declarations
- Query function implementations
- Import resolution tests
Implement JOIN...USING clause support for ClickHouse and PostgreSQL.
Refactor output_columns.go for improved type resolution.
Add comprehensive tests for output columns and type resolution.
Update quote character handling and catalog interface.
Docker and local connection adapters for integration testing.
Enables end-to-end testing with real ClickHouse instances.
Getting started guide, configuration and datatype reference updates,
example project with schema and queries, development guide updates,
Docker Compose configuration for ClickHouse service.
Generated Go code output from running sqlc generate on the
ClickHouse example project: database interface, model types,
and query functions (1264 lines of generated code).
PostgreSQL test case validating JOIN USING feature works
correctly with existing database engines.
Tests for basic ClickHouse functionality: SELECT queries, DML operations
(INSERT/UPDATE/DELETE), DISTINCT, ORDER BY, LIMIT/OFFSET pagination,
NULL handling, and string functions. Each test includes schema,
queries, and generated Go code.
Tests for complex SQL constructs: JOIN operations (explicit and implicit),
subqueries (including IN subqueries), common table expressions (CTEs),
aggregate functions, GROUP BY with HAVING, and EXISTS operator.
Each test includes schema, queries, and generated Go code.
Tests for type handling and built-in functions: array types, type casting,
string/math functions, named parameters, DELETE/UPDATE statements, and
advanced feature combinations. Includes test runner integration updates.
Final comprehensive validation of ClickHouse engine support.
@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. 🔧 golang labels Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XXL This PR changes 1000+ lines, ignoring generated files. 🔧 golang

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant