Skip to main content

Type-safe ClickHouse query parameter codegen from SQL files

Project description

chty

End-to-end type-safe ClickHouse query codegen from SQL files.

Documentation License: MIT

Installation

# Run directly without installation
uvx chty

# Or install globally
uv tool install chty

# Or use pip/pipx
pip install chty

Quick Start

Create a .sql file with ClickHouse parameterized queries:

-- queries/example.sql
SELECT number, number * {multiplier:Int32} AS result
FROM system.numbers
WHERE number < {limit:Int32}

Generate typed Python code:

# Local ClickHouse
chty generate queries/ --output generated/ --db-url clickhouse://user:pass@host:port

# ClickHouse Cloud
chty generate queries/ --output generated/ --db-url https://user:pass@host.clickhouse.cloud:8443

Use the generated code:

from generated.example import ExampleParams, ExampleQuery
import clickhouse_connect

client = clickhouse_connect.get_client(host="localhost")
params = ExampleParams(multiplier=3, limit=5)
query = ExampleQuery(client)

results = query.execute(params)
for row in results:
    print(f"Number: {row['number']}, Result: {row['result']}")

Validate generated code against your database schema:

# Detect schema drift in CI/CD or before deployment
chty validate generated/ --db-url clickhouse://user:pass@host:port

# Returns exit code 0 if valid, 1 if schema has changed

Features

  • Type-safe parameters - Catch parameter errors at development time
  • Type-safe results - Full autocomplete for result fields (with --db-url)
  • Schema validation - Detect schema drift with chty validate
  • Multiple execution methods - execute() and execute_df() for DataFrames
  • Optional runtime validation - Validate result schema at runtime
  • Zero runtime overhead - TypedDict is just type annotations

Why chty?

ClickHouse queries in Python lack type safety. chty connects your SQL queries directly to Python's type system:

Without chty:

  • ❌ No autocomplete for query parameters or results
  • ❌ Errors only caught at runtime
  • ❌ No way to detect schema drift

With chty:

  • ✅ Full type safety from SQL to Python
  • ✅ IDE autocomplete everywhere
  • ✅ Catch errors at development time
  • ✅ Detect schema changes in CI/CD

Documentation

Full documentation is available at https://treygilliland.github.io/chty/

Development

Common development tasks are available via make. See Makefile for more.

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

License

MIT License - see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chty-0.1.2.tar.gz (116.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chty-0.1.2-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file chty-0.1.2.tar.gz.

File metadata

  • Download URL: chty-0.1.2.tar.gz
  • Upload date:
  • Size: 116.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for chty-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7bc08d16b4decc83a763e7a1328ad13c21446a4db33ea657fccb1e26357df449
MD5 0bc21db1a378bc4a955637e642ed99ab
BLAKE2b-256 db48e52a912051a373e76a1bb16f80e74ddf846218b0e0d2ff46dc66bfaa76d8

See more details on using hashes here.

File details

Details for the file chty-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: chty-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for chty-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5350959c990040f4e533612768f7d46ed685e5bcbbb2b9f6358f7a3f02c3cb53
MD5 e84903e7a221beabc8cafd91986d1a75
BLAKE2b-256 dd678a0714a3c071f5d0ed5bdd76b1d63c2bf512adb38ca10e4789752b0d69ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page