Skip to main content

Databend Driver Python Binding

Project description

databend-driver

Databend Python Client

image License image

Usage

PEP 249 Cursor Object

from databend_driver import BlockingDatabendClient

client = BlockingDatabendClient('databend://root:root@localhost:8000/?sslmode=disable')
cursor = client.cursor()

cursor.execute(
    """
    CREATE TABLE test (
        i64 Int64,
        u64 UInt64,
        f64 Float64,
        s   String,
        s2  String,
        d   Date,
        t   DateTime
    )
    """
)
cursor.execute("INSERT INTO test VALUES (?, ?, ?, ?, ?, ?, ?)", (1, 1, 1.0, 'hello', 'world', '2021-01-01', '2021-01-01 00:00:00'))
cursor.execute("SELECT * FROM test")
rows = cursor.fetchall()
for row in rows:
    print(row.values())
cursor.close()

Blocking Connection Object

from databend_driver import BlockingDatabendClient

client = BlockingDatabendClient('databend://root:root@localhost:8000/?sslmode=disable')
conn = client.get_conn()
conn.exec(
    """
    CREATE TABLE test (
        i64 Int64,
        u64 UInt64,
        f64 Float64,
        s   String,
        s2  String,
        d   Date,
        t   DateTime
    )
    """
)
rows = conn.query_iter("SELECT * FROM test")
for row in rows:
    print(row.values())
conn.close()

Asyncio Connection Object

import asyncio
from databend_driver import AsyncDatabendClient

async def main():
    client = AsyncDatabendClient('databend://root:root@localhost:8000/?sslmode=disable')
    conn = await client.get_conn()
    await conn.exec(
        """
        CREATE TABLE test (
            i64 Int64,
            u64 UInt64,
            f64 Float64,
            s   String,
            s2  String,
            d   Date,
            t   DateTime
        )
        """
    )
    rows = await conn.query_iter("SELECT * FROM test")
    async for row in rows:
        print(row.values())
    await conn.close()

asyncio.run(main())

Parameter bindings

# Test with positional parameters
row = await context.conn.query_row("SELECT ?, ?, ?, ?", (3, False, 4, "55"))
row = await context.conn.query_row(
    "SELECT :a, :b, :c, :d", {"a": 3, "b": False, "c": 4, "d": "55"}
)
row = await context.conn.query_row(
    "SELECT ?", 3
)
row = await context.conn.query_row("SELECT ?, ?, ?, ?", params = (3, False, 4, "55"))

Type Mapping

Databend Types

General Data Types

Databend Python
BOOLEAN bool
TINYINT int
SMALLINT int
INT int
BIGINT int
FLOAT float
DOUBLE float
DECIMAL decimal.Decimal
DATE datetime.date
TIMESTAMP datetime.datetime
INTERVAL datetime.timedelta
VARCHAR str
BINARY bytes

Semi-Structured Data Types

Databend Python
ARRAY list
TUPLE tuple
MAP dict
VARIANT str
BITMAP str
GEOMETRY str
GEOGRAPHY str

Note: VARIANT is a json encoded string. Example:

CREATE TABLE example (
    data VARIANT
);
INSERT INTO example VALUES ('{"a": 1, "b": "hello"}');
row = await conn.query_row("SELECT * FROM example limit 1;")
data = row.values()[0]
value = json.loads(data)
print(value)

APIs

AsyncDatabendClient

class AsyncDatabendClient:
    def __init__(self, dsn: str): ...
    async def get_conn(self) -> AsyncDatabendConnection: ...

AsyncDatabendConnection

class AsyncDatabendConnection:
    async def info(self) -> ConnectionInfo: ...
    async def version(self) -> str: ...
    async def close(self) -> None: ...
    async def exec(self, sql: str, params: list[string] | tuple[string] | any = None) -> int: ...
    async def query_row(self, sql: str, params: list[string] | tuple[string] | any = None) -> Row: ...
    async def query_iter(self, sql: str, params: list[string] | tuple[string] | any = None) -> RowIterator: ...
    async def stream_load(self, sql: str, data: list[list[str]]) -> ServerStats: ...
    async def load_file(self, sql: str, file: str, format_option: dict, copy_options: dict = None) -> ServerStats: ...

BlockingDatabendClient

class BlockingDatabendClient:
    def __init__(self, dsn: str): ...
    def get_conn(self) -> BlockingDatabendConnection: ...
    def cursor(self) -> BlockingDatabendCursor: ...

BlockingDatabendConnection

class BlockingDatabendConnection:
    def info(self) -> ConnectionInfo: ...
    def version(self) -> str: ...
    def close(self) -> None: ...
    def exec(self, sql: str, params: list[string] | tuple[string] | any = None) -> int: ...
    def query_row(self, sql: str, params: list[string] | tuple[string] | any = None) -> Row: ...
    def query_iter(self, sql: str, params: list[string] | tuple[string] | any = None) -> RowIterator: ...
    def stream_load(self, sql: str, data: list[list[str]]) -> ServerStats: ...
    def load_file(self, sql: str, file: str, format_option: dict, copy_options: dict = None) -> ServerStats: ...

BlockingDatabendCursor

class BlockingDatabendCursor:
    @property
    def description(self) -> list[tuple[str, str, int | None, int | None, int | None, int | None, bool | None]] | None: ...
    @property
    def rowcount(self) -> int: ...
    def close(self) -> None: ...
    def execute(self, operation: str, params: list[string] | tuple[string] = None) -> None | int: ...
    def executemany(self, operation: str, params: list[string] | tuple[string] = None, values: list[list[string] | tuple[string]]) -> None | int: ...
    def fetchone(self) -> Row | None: ...
    def fetchmany(self, size: int = 1) -> list[Row]: ...
    def fetchall(self) -> list[Row]: ...

    # Optional DB API Extensions
    def next(self) -> Row: ...
    def __next__(self) -> Row: ...
    def __iter__(self) -> BlockingDatabendCursor: ...

Row

class Row:
    def values(self) -> tuple: ...
    def __len__(self) -> int: ...
    def __iter__(self) -> Row: ...
    def __next__(self) -> any: ...
    def __dict__(self) -> dict: ...
    def __getitem__(self, key: int | str) -> any: ...

RowIterator

class RowIterator:
    def schema(self) -> Schema: ...

    def __iter__(self) -> RowIterator: ...
    def __next__(self) -> Row: ...

    def __aiter__(self) -> RowIterator: ...
    async def __anext__(self) -> Row: ...

Field

class Field:
    @property
    def name(self) -> str: ...
    @property
    def data_type(self) -> str: ...

Schema

class Schema:
    def fields(self) -> list[Field]: ...

ServerStats

class ServerStats:
    @property
    def total_rows(self) -> int: ...
    @property
    def total_bytes(self) -> int: ...
    @property
    def read_rows(self) -> int: ...
    @property
    def read_bytes(self) -> int: ...
    @property
    def write_rows(self) -> int: ...
    @property
    def write_bytes(self) -> int: ...
    @property
    def running_time_ms(self) -> float: ...

ConnectionInfo

class ConnectionInfo:
    @property
    def handler(self) -> str: ...
    @property
    def host(self) -> str: ...
    @property
    def port(self) -> int: ...
    @property
    def user(self) -> str: ...
    @property
    def database(self) -> str | None: ...
    @property
    def warehouse(self) -> str | None: ...

Development

cd tests
make up
cd bindings/python
uv sync
source .venv/bin/activate
maturin develop --uv

behave tests/asyncio
behave tests/blocking
behave tests/cursor

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

databend_driver-0.27.6-cp37-abi3-manylinux_2_28_aarch64.whl (6.2 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.28+ ARM64

databend_driver-0.27.6-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.7 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ x86-64

databend_driver-0.27.6-cp37-abi3-macosx_11_0_arm64.whl (5.9 MB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

databend_driver-0.27.6-cp37-abi3-macosx_10_12_x86_64.whl (6.5 MB view details)

Uploaded CPython 3.7+macOS 10.12+ x86-64

File details

Details for the file databend_driver-0.27.6-cp37-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for databend_driver-0.27.6-cp37-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2db3e3f9b16fe39f6abdfbce86be2f6de5e51795b713f4904cf90148799bf712
MD5 9b6e7ad17a958da8228a08546c3abe6b
BLAKE2b-256 cc1ec2546ae3a605bd507ac51d0beee84d8e396813506ffee9c2ea29ee75e287

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.27.6-cp37-abi3-manylinux_2_28_aarch64.whl:

Publisher: bindings.python.yml on databendlabs/bendsql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file databend_driver-0.27.6-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for databend_driver-0.27.6-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5ef28e41507b33736528057ef7effbfef102e058acff7bdf5e233d1477df7150
MD5 9b10cdf6fbcbd70fa5f0d67cd8d4cdd2
BLAKE2b-256 e48854161f257fdff448b832723c91e4cd8de5ba22bdea6c4afc57e6adfd6103

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.27.6-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: bindings.python.yml on databendlabs/bendsql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file databend_driver-0.27.6-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for databend_driver-0.27.6-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 126ebfc4d7e79aab50015ed847431e63da5fbc35bddd2b3d589b8e9b96380bc6
MD5 21a6ec3943b76a6def39c310ed4d16c7
BLAKE2b-256 e1f3ae6014cff5be6428c61f9849024309ce9d306bd1612bde723f279f8a2971

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.27.6-cp37-abi3-macosx_11_0_arm64.whl:

Publisher: bindings.python.yml on databendlabs/bendsql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file databend_driver-0.27.6-cp37-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for databend_driver-0.27.6-cp37-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 50a490a39896781bb5f209aebc486900c9626d9474a27b727b3c82e7385904c9
MD5 2eb4f05e413c37527933bb0c1e1be240
BLAKE2b-256 8258a49e6d74fb4b5ca10d489cf5bfac90f3693c720615493c6845ee022ccf17

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.27.6-cp37-abi3-macosx_10_12_x86_64.whl:

Publisher: bindings.python.yml on databendlabs/bendsql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page