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) andAsyncClient(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 -
Datetodatetime.date,DateTimetodatetime.datetime,UUIDto string,IPv4/IPv6to strings - Column-oriented access -
BlockandColumnobjects for efficient columnar data access - GIL-free I/O - all blocking operations release the GIL during network reads/writes
- Cancellation - proper
asyncio.CancelledErrorhandling 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_sizeparallel queries on separate TCP connections - GIL release: all I/O-bound operations release the GIL
License
Apache-2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b999ee056394f44a1b5a3207d873dd6a02a948cf392584e64e36e0ebb7d452b2
|
|
| MD5 |
5d7000e70773e1678877a15ebd4108f1
|
|
| BLAKE2b-256 |
fad5e235615f000bbf56d486be6d3089aec23487408c4ba8a62f5da45f1ff141
|
File details
Details for the file st_clickhouse_py-0.2.0-cp314-cp314t-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: st_clickhouse_py-0.2.0-cp314-cp314t-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.14t, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
717cb114662f6db3fae53c95acab8889a3d4f59df6df2d78e63fbfa84f5e914f
|
|
| MD5 |
ef058d46cb69582d7ffbc540dd8d1a3b
|
|
| BLAKE2b-256 |
4ec8c8f8c18edd5a2fd85dd6ef83f0d6d29eb94096125722e87b09b5bb1446d5
|
File details
Details for the file st_clickhouse_py-0.2.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: st_clickhouse_py-0.2.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.14t, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e273f85f811ee958d0fe68a360ffb3aa0a2aab1aba36df5394b57fd6f2678b25
|
|
| MD5 |
e143287aa32712baf1a08731854a4bba
|
|
| BLAKE2b-256 |
42158ca1f7a34f448666521ce0f2dcdb1855fe23f106aa85107726aa2fc7c2fc
|
File details
Details for the file st_clickhouse_py-0.2.0-cp313-cp313t-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: st_clickhouse_py-0.2.0-cp313-cp313t-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.13t, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26d69e450c0de5e659fb83715150aca5d3c91dad213d485dd876b538d24c96a0
|
|
| MD5 |
606c7c06d52471a10c36b18fb930f599
|
|
| BLAKE2b-256 |
54e314dd67d9e45f95879a67edc14c42401d715c063f421087274d69c3419f63
|
File details
Details for the file st_clickhouse_py-0.2.0-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: st_clickhouse_py-0.2.0-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.13t, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a22b3f3ca575c4e529e9d93d1382dae9b4fe410b21f92dec3d78f53612a5ba3c
|
|
| MD5 |
92c34ae39564088edf1d980dc216fe8e
|
|
| BLAKE2b-256 |
4c0f43f7851b400fbdc0bef421f394b5e6723ada2bd0dad945dca2fdf8e1c42a
|
File details
Details for the file st_clickhouse_py-0.2.0-cp312-abi3-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: st_clickhouse_py-0.2.0-cp312-abi3-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.12+, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2575b1abe3ba7cbbc2a85d47a308f64487bad387fc36db975ceec2a7f1bac524
|
|
| MD5 |
ff767c0a79c9e6c99b1650a9ce78e0ff
|
|
| BLAKE2b-256 |
e902fab3a65750fe5bc6a054a5e0c0c9fc6182fb6903efc90c144344dfaf7d11
|
File details
Details for the file st_clickhouse_py-0.2.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: st_clickhouse_py-0.2.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.12+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e96e9e5c5be5707730e6094656e7c1bb41489c0497ffb7f78f66966bbe36bdc9
|
|
| MD5 |
32bf298587094840cbe956cb6d2f377f
|
|
| BLAKE2b-256 |
d9cd428ceafd1a845cdb2384872168875c5501920407e54ec0f4beb0beecf902
|