Skip to main content

SQLAlchemy dialect for chDB — the in-process SQL OLAP engine powered by ClickHouse. Unlocks LangChain SQLDatabaseToolkit, CrewAI NL2SQLTool, pandas.read_sql, Apache Superset, and Django ORM.

Project description

chdb-sqlalchemy

SQLAlchemy dialect for chDB — the in-process OLAP SQL engine powered by ClickHouse.

chdb-sqlalchemy lets you use chDB as a SQLAlchemy backend, which in turn unlocks every Python data stack that already speaks SQLAlchemy: Django ORM, Flask-SQLAlchemy, pandas.read_sql(), Apache Superset, LangChain's SQLDatabaseToolkit, CrewAI's NL2SQLTool, and more.

v0.2.0 is live on PyPIhttps://pypi.org/project/chdb-sqlalchemy/

What this is

chDB already supports DB-API 2.0 through chdb.dbapi. chdb-sqlalchemy adds the layer above that: a dialect that handles connection URIs, table reflection, type mapping, and the introspection contract SQLAlchemy expects.

The dialect is a thin wrapper — chDB's SQL surface is ClickHouse SQL, so most of the dialect's job is type mapping and reflection, not query rewriting.

Install

pip install chdb-sqlalchemy

Usage

Basic connection

from sqlalchemy import create_engine, text

# In-memory
engine = create_engine("chdb:///:memory:")

# Persistent
engine = create_engine("chdb:////tmp/my_chdb")

with engine.connect() as conn:
    result = conn.execute(text("SELECT version()"))
    print(result.scalar())

With pandas

import pandas as pd
from sqlalchemy import create_engine

engine = create_engine("chdb:////tmp/my_chdb")
df = pd.read_sql("SELECT * FROM file('data.parquet') LIMIT 100", engine)

With LangChain SQLDatabaseToolkit

from langchain_community.utilities import SQLDatabase
from langchain_community.agent_toolkits import SQLDatabaseToolkit

db = SQLDatabase.from_uri("chdb:////tmp/my_chdb")
toolkit = SQLDatabaseToolkit(db=db, llm=llm)

With CrewAI NL2SQLTool

from crewai_tools import NL2SQLTool

nl2sql = NL2SQLTool(db_uri="chdb:////tmp/my_chdb")

URI format

chdb:///:memory:                 # in-memory session
chdb:////absolute/path/to/dir    # persistent session at that directory
chdb:///./relative/path          # persistent session at a relative path

There are no host, port, username, or password components — chDB runs in-process. Authentication is delegated to the surrounding application.

Type mapping

ClickHouse type SQLAlchemy type
String, FixedString String
UInt8UInt64, Int8Int64 Integer, BigInteger
Float32, Float64 Float
Decimal(P, S) Numeric
Date, Date32 Date
DateTime, DateTime64 DateTime
UUID Uuid
Array(T) ARRAY
Tuple(...), Map(K, V) JSON
JSON (native) JSON

Introspection support

Following the LangChain SQLDatabaseToolkit introspection contract, the dialect implements:

  • get_table_names() — list user tables visible to the session.
  • get_columns() — name, type, nullability, default for each column.
  • get_pk_constraint() — primary key columns (when a MergeTree ORDER BY is the de facto PK).
  • get_foreign_keys() — empty list; chDB does not enforce foreign keys.
  • get_indexes() — primary and secondary indexes from system.data_skipping_indices.

Roadmap

  • v0.1 — dialect registration, connection URI, basic type mapping, table reflection for SQLDatabase.from_uri() and pandas.read_sql().
  • v0.2 — full LangChain SQLDatabaseToolkit and CrewAI NL2SQLTool certification — both rely on introspection that the v0.1 surface does not yet cover.
  • v0.3remoteSecure() federated table support exposed as SQLAlchemy Table objects.

Milestones land incrementally; check back here or follow @chdb_io for releases.

License

Apache 2.0 — see LICENSE.

Related

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

chdb_sqlalchemy-0.2.1.tar.gz (113.1 kB view details)

Uploaded Source

Built Distribution

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

chdb_sqlalchemy-0.2.1-py3-none-any.whl (60.1 kB view details)

Uploaded Python 3

File details

Details for the file chdb_sqlalchemy-0.2.1.tar.gz.

File metadata

  • Download URL: chdb_sqlalchemy-0.2.1.tar.gz
  • Upload date:
  • Size: 113.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for chdb_sqlalchemy-0.2.1.tar.gz
Algorithm Hash digest
SHA256 330a0c82b6ea958f6ea32a7f49b9f652215f1eb0275685252e55362eff812051
MD5 9814bca8712fd2c10b5bd8df93de4707
BLAKE2b-256 faea4edb6cd72ac61aeccc51059480f348675f2732e98a5ec7dbd876cb96cca1

See more details on using hashes here.

File details

Details for the file chdb_sqlalchemy-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for chdb_sqlalchemy-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 62c666dbb1425e4ef88904fc655e5bda89d2b39a20e259cc97a926e683f26f8f
MD5 88c6e4d12606c0a4bfb178ded41902c3
BLAKE2b-256 ce69d94cb5de3c9682d2b47c630d5b6f78c0377350327ba357ddcf645cdc55b8

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