Skip to main content

ClickHouse native protocol client — Python bindings

Project description

st-clickhouse-py

Python bindings for the ClickHouse native TCP protocol - 100% Rust core via PyO3.

A high-performance ClickHouse client that speaks the native protocol directly. No HTTP, no REST, no SQL parsing in Python - the entire protocol is implemented in Rust with Python bindings via PyO3.

Features

  • Native TCP protocol - direct connection to ClickHouse, no HTTP overhead
  • Sync + Async - Client (sync) and AsyncClient (async) APIs
  • Connection pooling - transparent pool (2-8 connections, health checks, idle reaper)
  • True streaming - Rust reader thread + bounded channel, zero thread pool overhead
  • Type-aware conversion - Date to datetime.date, DateTime to datetime.datetime, UUID to string, IPv4/IPv6 to strings
  • Column-oriented access - Block and Column objects for efficient columnar data access
  • GIL-free I/O - all blocking operations release the GIL during network reads/writes
  • Cancellation - proper asyncio.CancelledError handling with cleanup
  • uvloop compatible - standard asyncio APIs only
  • Python 3.12-3.14, including 3.14t/free-threaded builds. GIL builds use abi3; free-threaded builds use version-specific artifacts.
  • Compression - LZ4 and ZSTD support

Quick Start

import st_clickhouse as ch

# Sync
with ch.connect("127.0.0.1:9000") as client:
    rows = client.query("SELECT number FROM system.numbers LIMIT 5")
    for row in rows:
        print(row)

# Async
import asyncio

async def main():
    async with ch.connect_async(
        "127.0.0.1:9000",
        pool_min_size=2,
        pool_max_size=8,
    ) as client:
        rows = await client.query("SELECT 1 AS x")
        print(rows)

asyncio.run(main())

# Streaming (sync)
for block in client.query_stream("SELECT * FROM huge_table"):
    col_a = block["a"]
    values = col_a.to_list()  # → [1, 2, 3, ...]

# Streaming (async) — zero thread pool threads blocked
async for block in client.query_stream("SELECT * FROM huge_table"):
    process(block)

Installation

pip install st-clickhouse-py

Or build from source:

pip install maturin
cd st-clickhouse-py
maturin build --release
pip install target/wheels/st_clickhouse_py-*.whl

API Overview

Sync Client

Method Description
execute(query) DDL/DML, no result rows
query(query) SELECT → list of dicts
query_blocks(query) SELECT → list of Block objects
query_stream(query) SELECT → iterator of Block objects
insert(query, rows) INSERT from list of dicts
insert_blocks(query, table, blocks) INSERT from Block objects
insert_stream(query)InsertStream Streaming INSERT session
ping() Health check
cancel() Cancel running query
server_info() Server metadata (cached)
set_setting(name, value) Session setting

Async Client

Same methods prefixed with async/await, plus connection pooling:

client = AsyncClient(
    "127.0.0.1:9000",
    pool_min_size=2,
    pool_max_size=8,
    pool_acquire_timeout=30.0,
    pool_health_check_interval=30.0,
    pool_max_idle_time=300.0,
)

Connection Pool

The pool manages TCP connections transparently:

  • Acquire/release: each operation gets a connection, uses it, returns it
  • Lazy growth: connections created on demand up to pool_max_size
  • Health checks: idle connections are pinged before being served
  • Idle reaper: background thread closes stale connections above min_size
  • Backpressure: asyncio.Queue + Rust channel — if consumer is slow, the reader thread blocks on TCP write, telling ClickHouse to slow down

Architecture

┌─────────────────────────────────────────────────────────────┐
│                     PYTHON                                  │
│  AsyncClient                                                │
│    ├── query/execute → Pool.acquire() → client.query()      │
│    │                              (GIL released during I/O) │
│    └── query_stream → Pool.acquire() → [held for stream]    │
│         └── Rust Reader Thread → mpsc Channel → Forwarder   │
│             (TCP I/O, no GIL)    (bounded 32)  → Queue      │
│                                                    ↓        │
│                                              async for       │
└─────────────────────────────────────────────────────────────┘

Running Tests

Requires a running ClickHouse server:

# Using Docker
docker run -d -p 9000:9000 --name ch-test clickhouse/clickhouse-server

uv run --extra test maturin develop --release
uv run --extra test python -m pytest

Performance

  • Single query: ~50μs overhead over raw TCP (PyO3 FFI + type conversion)
  • Streaming: zero copy per block (Rust reader thread → Python async for)
  • Concurrent: up to pool_max_size parallel queries on separate TCP connections
  • GIL release: all I/O-bound operations release the GIL

License

Apache-2.0

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

st_clickhouse_py-0.2.0.tar.gz (356.0 kB view details)

Uploaded Source

Built Distributions

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

st_clickhouse_py-0.2.0-cp314-cp314t-manylinux_2_34_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.34+ x86-64

st_clickhouse_py-0.2.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ ARM64

st_clickhouse_py-0.2.0-cp313-cp313t-manylinux_2_34_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.13tmanylinux: glibc 2.34+ x86-64

st_clickhouse_py-0.2.0-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view details)

Uploaded CPython 3.13tmanylinux: glibc 2.17+ ARM64

st_clickhouse_py-0.2.0-cp312-abi3-manylinux_2_34_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.34+ x86-64

st_clickhouse_py-0.2.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

File details

Details for the file st_clickhouse_py-0.2.0.tar.gz.

File metadata

  • Download URL: st_clickhouse_py-0.2.0.tar.gz
  • Upload date:
  • Size: 356.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for st_clickhouse_py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b999ee056394f44a1b5a3207d873dd6a02a948cf392584e64e36e0ebb7d452b2
MD5 5d7000e70773e1678877a15ebd4108f1
BLAKE2b-256 fad5e235615f000bbf56d486be6d3089aec23487408c4ba8a62f5da45f1ff141

See more details on using hashes here.

File details

Details for the file st_clickhouse_py-0.2.0-cp314-cp314t-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for st_clickhouse_py-0.2.0-cp314-cp314t-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 717cb114662f6db3fae53c95acab8889a3d4f59df6df2d78e63fbfa84f5e914f
MD5 ef058d46cb69582d7ffbc540dd8d1a3b
BLAKE2b-256 4ec8c8f8c18edd5a2fd85dd6ef83f0d6d29eb94096125722e87b09b5bb1446d5

See more details on using hashes here.

File details

Details for the file st_clickhouse_py-0.2.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for st_clickhouse_py-0.2.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e273f85f811ee958d0fe68a360ffb3aa0a2aab1aba36df5394b57fd6f2678b25
MD5 e143287aa32712baf1a08731854a4bba
BLAKE2b-256 42158ca1f7a34f448666521ce0f2dcdb1855fe23f106aa85107726aa2fc7c2fc

See more details on using hashes here.

File details

Details for the file st_clickhouse_py-0.2.0-cp313-cp313t-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for st_clickhouse_py-0.2.0-cp313-cp313t-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 26d69e450c0de5e659fb83715150aca5d3c91dad213d485dd876b538d24c96a0
MD5 606c7c06d52471a10c36b18fb930f599
BLAKE2b-256 54e314dd67d9e45f95879a67edc14c42401d715c063f421087274d69c3419f63

See more details on using hashes here.

File details

Details for the file st_clickhouse_py-0.2.0-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for st_clickhouse_py-0.2.0-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a22b3f3ca575c4e529e9d93d1382dae9b4fe410b21f92dec3d78f53612a5ba3c
MD5 92c34ae39564088edf1d980dc216fe8e
BLAKE2b-256 4c0f43f7851b400fbdc0bef421f394b5e6723ada2bd0dad945dca2fdf8e1c42a

See more details on using hashes here.

File details

Details for the file st_clickhouse_py-0.2.0-cp312-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for st_clickhouse_py-0.2.0-cp312-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 2575b1abe3ba7cbbc2a85d47a308f64487bad387fc36db975ceec2a7f1bac524
MD5 ff767c0a79c9e6c99b1650a9ce78e0ff
BLAKE2b-256 e902fab3a65750fe5bc6a054a5e0c0c9fc6182fb6903efc90c144344dfaf7d11

See more details on using hashes here.

File details

Details for the file st_clickhouse_py-0.2.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for st_clickhouse_py-0.2.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e96e9e5c5be5707730e6094656e7c1bb41489c0497ffb7f78f66966bbe36bdc9
MD5 32bf298587094840cbe956cb6d2f377f
BLAKE2b-256 d9cd428ceafd1a845cdb2384872168875c5501920407e54ec0f4beb0beecf902

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