Skip to main content

Apache Doris Dialect for SQLAlchemy

This is a fork of sqlalchemy-doris project. Which is in turn - a fork of pydoris

This implementation fixes a bunch of issues with typing. And adds support for sqlalchemy ORM.

Features

  • support SQLAlchemy 2.
  • support pymysql and mysqlclient as driver.
  • support SQLAlchemy table creation
  • support for SQLALchemy ORM
  • convenient DorisBase class for declaring ORM models

Installation

Use

pip install doris-alchemy[pymysql]

for pymysql.

Or

pip install doris-alchemy[mysqldb]

for mysqlclient.

Note doris-alchemy uses pymysql as default connector for compatibility. If both pymysql and mysqlclient are installed, mysqlclient is preferred.

Usage

from sqlalchemy import create_engine

engine = create_engine(f"doris+pymysql://{user}:{password}@{host}:{port}/{database}?charset=utf8mb4")
# or
engine = create_engine(f"doris+mysqldb://{user}:{password}@{host}:{port}/{database}?charset=utf8mb4")

Create Table (Imperative style)

import sqlalchemy as sa
from sqlalchemy import create_engine
from doris_alchemy import datatype
from doris_alchemy import HASH, RANGE

engine = create_engine(f"doris://{user}:{password}@{host}:{port}/{database}?charset=utf8mb4")


metadata_obj = sa.MetaData()
table = Table(
    'dummy_table',
    METADATA,
    Column('id', Integer, primary_key=True),
    Column('name', String(64), nullable=False),
    Column('description', Text),
    Column('date', DateTime),
    
    doris_unique_key=('id'),
    doris_partition_by=RANGE('id'),
    doris_distributed_by=HASH('id'),
    doris_properties={"replication_allocation": "tag.location.default: 1"},
)

table.create(engine)

SQL is

CREATE TABLE dummy_table (
        id INTEGER NOT NULL, 
        name VARCHAR(64) NOT NULL, 
        description TEXT, 
        date DATETIME
)
UNIQUE KEY (`id`)
PARTITION BY RANGE(`id`) ()
DISTRIBUTED BY HASH(`id`) BUCKETS auto
PROPERTIES (
    "replication_allocation" = "tag.location.default: 1"
)

Create Table (Declarative style / ORM)

from sqlalchemy import create_engine
from doris_alchemy import datatype, DorisBase
from doris_alchemy import HASH, RANGE

engine = create_engine(f"doris://{user}:{password}@{host}:{port}/{database}?charset=utf8mb4")

class Dummy(DorisBase):
    __tablename__ = 'dummy_two'
    
    id:             Mapped[int] = mapped_column(BigInteger, primary_key=True)
    name:           Mapped[str] = mapped_column(String(127))
    description:    Mapped[str]
    date:           Mapped[datetime]
    
    __table_args__ = {
        'doris_properties': {"replication_allocation": "tag.location.default: 1"}
        }
    doris_unique_key = 'id'
    doris_distributed_by = HASH('id')
    doris_partition_by = RANGE('id')


DorisBase.metadata.create_all(engine)

SQL is

CREATE TABLE dummy_two (
        id BIGINT NOT NULL, 
        name VARCHAR(127) NOT NULL, 
        description TEXT NOT NULL, 
        date DATETIME NOT NULL
)
UNIQUE KEY (`id`)
PARTITION BY RANGE(`id`) ()
DISTRIBUTED BY HASH(`id`) BUCKETS auto
PROPERTIES (
    "replication_allocation" = "tag.location.default: 1"
)

Insertin and selecting

from sqlalchemy.orm import Session
from sqlalchemy import select, insert, create_engine
from datetime import datetime

engine = create_engine(f"doris+mysqldb://{USER}:{PWD}@{HOST}:{PORT}/{DB}")

row = {
        'id': 0,
        'name': 'Airbus',
        'description': 'Construction bureau',
        'date': datetime(2024, 2, 10)
    }
    
with Session(engine) as s:
    q = insert(Dummy).values([row])
    s.execute(q)
    sel = select(Dummy)
    res = s.execute(sel)
    print(list(res))

Metadata

Release files for doris-alchemy 0.2.14

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for doris-alchemy 0.2.14
File Size Uploaded Provenance Actions
doris_alchemy-0.2.14.tar.gz 20.1 kB Without provenance Details for doris_alchemy-0.2.14.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for doris-alchemy 0.2.14
File Interpreter ABI Platform Actions
doris_alchemy-0.2.14-py3-none-any.whl Python 3 none any Details for doris_alchemy-0.2.14-py3-none-any.whl

Total release size: 39.0 kB

Release files / doris_alchemy-0.2.14.tar.gz

Download URL doris_alchemy-0.2.14.tar.gz
Size 20.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a87ffd485374fa78b67ab606c03b4e38c1cf7d5424324e46796a044a9919e2da
BLAKE2b-256 checksum
How to use checksums
ffca284f62b15e352a42ee196a02f99ec539972d66944b53f3cdbeca06529404
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release files / doris_alchemy-0.2.14-py3-none-any.whl

Download URL doris_alchemy-0.2.14-py3-none-any.whl
Size 18.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de94500e3938a0ba52f8214069765797f00e0520efc73e00eb17c0916bd61342
BLAKE2b-256 checksum
How to use checksums
817c422cca39b2d185c8465eeb71b91e4320d9654f900846f0e7bb60c8778ceb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Security for doris-alchemy 0.2.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page