Skip to main content

Apache Doris dialect for SQLAlchemy

Project description

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))

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_alchemy-0.2.10.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

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

doris_alchemy-0.2.10-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file doris_alchemy-0.2.10.tar.gz.

File metadata

  • Download URL: doris_alchemy-0.2.10.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for doris_alchemy-0.2.10.tar.gz
Algorithm Hash digest
SHA256 81e6082d757c252be234a06d08e122a1349fda37a7ab3dbf2e31fb521646aeaf
MD5 cd08b10d448cf5ceca9b84074238895a
BLAKE2b-256 bc69e2a9f1ccfc8806f86eea587c856f880a445e23b3b05d1763c04bad56f77a

See more details on using hashes here.

File details

Details for the file doris_alchemy-0.2.10-py3-none-any.whl.

File metadata

  • Download URL: doris_alchemy-0.2.10-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for doris_alchemy-0.2.10-py3-none-any.whl
Algorithm Hash digest
SHA256 924874d9d31b5e6863a2a95dd8543d8c673b6b897b060f0eeb29558ca1d66ac6
MD5 475823bcbc3a485ba6d72dab643a1ccb
BLAKE2b-256 1d79276512fb0462e0c04bad140a3115e49c17738c5c8042b9821f7ce098e3ee

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