Skip to main content

YDB Dialect for SQLAlchemy


License PyPI version API Reference Functional tests Style checks

This repository contains YQL dialect for SqlAlchemy 2.0.


Documentation: https://ydb-platform.github.io/ydb-sqlalchemy


Note: Dialect also works with SqlAlchemy 1.4, but it is not fully tested.

Installation

Via PyPI

To install ydb-sqlalchemy from PyPI:

$ pip install ydb-sqlalchemy

Installation from source code

To work with current ydb-sqlalchemy version clone this repo and run from source root:

$ pip install -U .

Getting started

Connect to local YDB using SqlAlchemy:

import sqlalchemy as sa


engine = sa.create_engine("yql+ydb://localhost:2136/local")

with engine.connect() as conn:
  rs = conn.execute(sa.text("SELECT 1 AS value"))
  print(rs.fetchone())

Authentication

To specify credentials, you should pass credentials object to connect_args argument of create_engine method.

Static Credentials

To use static credentials you should specify username and password as follows:

engine = sa.create_engine(
    "yql+ydb://localhost:2136/local",
    connect_args = {
        "credentials": {
            "username": "...",
            "password": "..."
        }
    }
)

Token Credentials

To use access token credentials you should specify token as follows:

engine = sa.create_engine(
    "yql+ydb://localhost:2136/local",
    connect_args = {
        "credentials": {
            "token": "..."
        }
    }
)

Service Account Credentials

To use service account credentials you should specify service_account_json as follows:

engine = sa.create_engine(
    "yql+ydb://localhost:2136/local",
    connect_args = {
        "credentials": {
            "service_account_json": {
                "id": "...",
                "service_account_id": "...",
                "created_at": "...",
                "key_algorithm": "...",
                "public_key": "...",
                "private_key": "..."
            }
        }
    }
)

Credentials from YDB SDK

To use any credentials that comes with ydb package, just pass credentials object as follows:

import ydb.iam

engine = sa.create_engine(
    "yql+ydb://localhost:2136/local",
    connect_args = {
        "credentials": ydb.iam.MetadataUrlCredentials()
    }
)

Migrations

To setup alembic to work with YDB please check this example.

Development

Run Tests:

Run the command from the root directory of the repository to start YDB in a local docker container.

$ docker-compose up

To run all tests execute the command from the root directory of the repository:

$ tox -e test-all

Run specific test:

$ tox -e test -- test/test_core.py

Check code style:

$ tox -e style

Reformat code:

$ tox -e isort
$ tox -e black-format

Run example (needs running local YDB):

$ python -m pip install virtualenv
$ virtualenv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
$ python examples/example.py

Additional Notes

Pandas

It is possible to use YDB SA engine with pandas fuctions to_sql() and read_sql. However, there are some limitations:

  • to_sql method can not be used with column tables, since it is impossible to specify NOT NULL columns with current to_sql arguments. YDB requires column tables to have NOT NULL attribute on PK columns.

  • to_sql is not fully optimized to load huge datasets. It is recommended to use method="multi" and avoid setting a very large chunksize.

  • read_sql is not fully optimized to load huge datasets and could lead to significant memory consumptions.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ydb_sqlalchemy-0.1.23.tar.gz (49.5 kB view details)

Uploaded Source

Built Distribution

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

ydb_sqlalchemy-0.1.23-py2.py3-none-any.whl (54.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file ydb_sqlalchemy-0.1.23.tar.gz.

File metadata

  • Download URL: ydb_sqlalchemy-0.1.23.tar.gz
  • Upload date:
  • Size: 49.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.14

File hashes

Hashes for ydb_sqlalchemy-0.1.23.tar.gz
Algorithm Hash digest
SHA256 cd82d433c4f564782ceb41f4dc9d5d59e6b60f3aca12fbb5c226fe3f05eb3ee0
MD5 706015ce5b6690003901315edd049962
BLAKE2b-256 f860ffab5631152d7aca8b1b1d5953db52c353d67a9d49a7d2c4eae7a0b329d1

See more details on using hashes here.

File details

Details for the file ydb_sqlalchemy-0.1.23-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for ydb_sqlalchemy-0.1.23-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 44f1567d2129d24c82c3dee50ad5e7ee45a390a603af53a0ae2bda5dd438ecab
MD5 ffa72c75562527e31a7d8e3f65982778
BLAKE2b-256 9b2cee667d742721c712f2c0aa0ba45cb6276aba93cbbdec6fe4c2836212dcc1

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.23 This release

2 files

0.1.22

2 files

0.1.21

2 files

0.1.20

2 files

0.1.19

2 files

0.1.18

2 files

0.1.17

2 files

0.1.16

2 files

0.1.15

2 files

0.1.14

2 files

0.1.13

2 files

0.1.12

2 files

0.1.11

2 files

0.1.10

2 files

0.1.9

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

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