Apache Doris SQLAlchemy dialect
Project description
doris-python
Apache Doris 的 SQLAlchemy 2.x 方言。
doris-python提供 Doris 专属的 SQLAlchemy 方言,支持同步与异步驱动, 可直接通过pip install doris-python安装,并自动注册doris://系列 URL。
项目定位
doris-python 是一个独立维护的 SQLAlchemy 方言包,方言源码就在本仓库的
src/doris_python/sqlalchemy/ 目录下。
⚠️ 本仓库不依赖任何名为
pydoris的 PyPI 包,也没有把pydoris列为 运行时依赖。方言源码借鉴自 Apache Doris 官方仓库下的samples/doris-python/pydoris目录,但已在doris_python.*命名空间下独立迭代与扩展,与上游已经脱钩。
| 角色 | 仓库 | 作用 |
|---|---|---|
| 借鉴来源 | apache/doris/samples/doris-python/pydoris |
早期方言实现的灵感来源 |
本仓库 doris-python |
zoulee24/doris-python | 独立维护、扩展、打包并发布到 PyPI |
安装
doris-python不会拉取pydoris包;用户只需使用标准doris:///doris+pymysql://等 URL 即可连接 Apache Doris。 如果你已经安装了pydoris,建议卸载以避免 SQLAlchemy entry-points 冲突。
功能特性
- ✅ 多驱动支持:
mysqldb/pymysql/aiomysql/asyncmy - ✅ 同步与异步:兼容 SQLAlchemy 2.x 同步 API 与
asyncio异步 API - ✅ 自动方言注册:通过
entry-points自动暴露doris://系列 URL - ✅ Doris 专用类型:
TINYINT、LARGEINT、HLL、BITMAP、QUANTILE_STATE、AGG_STATE、ARRAY、MAP、STRUCT、IPV4、IPV6、TIME、VARIANT - ✅ 三种 KEY 模型:DDL 编译器内置
DUPLICATE KEY/UNIQUE KEY/AGGREGATE KEY,支持ENGINE/DISTRIBUTED BY ... BUCKETS N/PARTITION BY/PROPERTIES - ✅ Doris 专属 DDL 处理:自动剔除
AUTO_INCREMENT、PRIMARY KEY、FOREIGN KEY、UNIQUE等 Doris 不支持的约束 - ✅ PyPI 标准打包:可直接
pip install doris-python
安装
pip install doris-python
开发安装
cd /path/to/doris-python
pip install -e ".[dev]"
# 或使用 uv(推荐)
uv sync --extra dev
快速开始
同步示例(SQLAlchemy 2.x)
from sqlalchemy import create_engine, text
engine = create_engine(
"doris+pymysql://user:password@host:9030/demo",
pool_size=10,
pool_recycle=3600,
)
with engine.connect() as conn:
result = conn.execute(text("SELECT VERSION()"))
print(result.scalar())
异步示例(asyncio)
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text
engine = create_async_engine(
"doris+asyncmy://user:password@host:9030/demo",
)
async with engine.connect() as conn:
result = await conn.execute(text("SELECT VERSION()"))
print(result.scalar())
ORM 模型示例(包含 Doris 特有语法)
from sqlalchemy import Column, Integer, String, declarative_base
# 注意:类型与方言都来自 doris_python,不依赖 pydoris
from doris_python.sqlalchemy.datatype import TINYINT, LARGEINT, HLL
Base = declarative_base()
class UserEvent(Base):
__tablename__ = "user_event"
# Doris 专属 Table 选项(参考 src/doris_python/sqlalchemy/dialect.py
# 中 DorisDDLCompiler.post_create_table 的实现)
__table_args__ = {
"doris_engine": "OLAP",
"doris_key_type": "DUPLICATE",
"doris_key_columns": ["event_id"],
"doris_distributed_by": "HASH(`event_id`)",
"doris_buckets": 16,
"doris_properties": {
"replication_num": "3",
"storage_medium": "SSD",
},
}
event_id = Column(LARGEINT, primary_key=True)
user_id = Column(Integer)
flag = Column(TINYINT)
profile = Column(HLL)
上面生成的 DDL 大致为:
CREATE TABLE user_event ( event_id LARGEINT NOT NULL, user_id INT NOT NULL, flag TINYINT NOT NULL, profile HLL NOT NULL ) ENGINE = OLAP DUPLICATE KEY(`event_id`) DISTRIBUTED BY HASH(`event_id`) BUCKETS 16 PROPERTIES ("replication_num" = "3", "storage_medium" = "SSD")
支持的 URL Schemes
| URL | 驱动 | 同步/异步 |
|---|---|---|
doris:// / doris+mysqldb:// |
mysqlclient | 同步 |
doris+pymysql:// |
PyMySQL | 同步 |
doris+aiomysql:// |
aiomysql | 异步 |
doris+asyncmy:// |
asyncmy | 异步 |
默认端口
9030(Doris FE 查询端口)。
开发
# 克隆仓库
git clone https://github.com/zoulee24/doris-python.git
cd doris-python
# 使用 uv(推荐)
uv sync --extra dev
# 或使用 pip
pip install -e ".[dev]"
# 单元测试(默认,无需数据库)
pytest
# 单元测试 + 集成测试(需要真实 Doris)
pytest --run-integration
# 代码风格
ruff check .
ruff format .
测试约定:
- 默认
pytest只跑单元测试(约 102 个),不需要任何外部依赖; pytest --run-integration会额外跑 4 个需要真实 Doris 实例的连通性测试, 配置从项目根目录的.env文件读取(参见.env.example)。
路线图
- 单元测试覆盖:方言注册、类型映射、DDL 编译
- GitHub Actions:lint + pytest
- GitHub Actions:tag 触发打包并发布到 PyPI
- 动态版本号(从 git tag 读取)
- 补充方言反射(reflection)相关单元测试
- CI 多 Python 版本矩阵(3.9 / 3.10 / 3.11 / 3.12 / 3.13)
许可证
本项目遵循 Apache License 2.0
致谢
- Apache Doris — 实时分析型数据库
- SQLAlchemy — Python SQL 工具包与 ORM
- 方言实现的早期版本借鉴自
apache/doris/samples/doris-python/pydoris
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 Distribution
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 doris_python-1.0.4.tar.gz.
File metadata
- Download URL: doris_python-1.0.4.tar.gz
- Upload date:
- Size: 31.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc78745924bd9f4951257b63f9af2f56a39fd51333677a216b0f6fe6ebc766ce
|
|
| MD5 |
03c09c3c04e23c936b153d778bfb71f2
|
|
| BLAKE2b-256 |
4cc07b3aae8d90a0cb4a1f03812a236bdb38df4317235f5ab09724f859b8effb
|
File details
Details for the file doris_python-1.0.4-py3-none-any.whl.
File metadata
- Download URL: doris_python-1.0.4-py3-none-any.whl
- Upload date:
- Size: 26.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07e5c548ff764b9c19009076f86cbf4ca69a3c45dbb6c87ef5a2fa4d4f67dc65
|
|
| MD5 |
0fbc655b764a63b8665db84a89543636
|
|
| BLAKE2b-256 |
2acd74336fdbd912f502fb87d5e09a17637dfd4d14642259f2b31b6fb9a7b436
|