Skip to main content

Apache Doris SQLAlchemy dialect

Project description

doris-python

Python SQLAlchemy 2.x License

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 专用类型TINYINTLARGEINTHLLBITMAPQUANTILE_STATEAGG_STATEARRAYMAPSTRUCTIPV4IPV6TIMEVARIANT
  • 三种 KEY 模型:DDL 编译器内置 DUPLICATE KEY / UNIQUE KEY / AGGREGATE KEY,支持 ENGINE / DISTRIBUTED BY ... BUCKETS N / PARTITION BY / PROPERTIES
  • Doris 专属 DDL 处理:自动剔除 AUTO_INCREMENTPRIMARY KEYFOREIGN KEYUNIQUE 等 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

致谢

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

doris_python-1.0.2.tar.gz (30.8 kB view details)

Uploaded Source

Built Distribution

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

doris_python-1.0.2-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

Details for the file doris_python-1.0.2.tar.gz.

File metadata

  • Download URL: doris_python-1.0.2.tar.gz
  • Upload date:
  • Size: 30.8 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

Hashes for doris_python-1.0.2.tar.gz
Algorithm Hash digest
SHA256 c379c6d681913a7de6670e4ec1ac7d3e44b241d29c781e0833118f6b60fbebc1
MD5 791558d908231562c9607a7c19a34b72
BLAKE2b-256 9a13af2df4a6188835c457e164609dde3b645d879f2637056cd2ba0bf0d3ff61

See more details on using hashes here.

File details

Details for the file doris_python-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: doris_python-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 24.5 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

Hashes for doris_python-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 404baa627fbde0cfa6b9256b5eb6ac29675bb3107476df6f3e8afb203ad56af7
MD5 7c0cd5b96e3326ba391c9af63e8dc84f
BLAKE2b-256 a68d4d6859d010503900a7da37eb206e60eda4909b101c901dbc170e1d53ea0c

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