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

Query ID tracking and query management

# Get the last executed query ID
query_id = conn.last_query_id()
print(f"Last query ID: {query_id}")

# Execute a query and get its ID
await conn.query_row("SELECT 1")
query_id = conn.last_query_id()
print(f"Query ID: {query_id}")

# Kill a running query (if needed)
try:
    await conn.kill_query("some-query-id")
    print("Query killed successfully")
except Exception as e:
    print(f"Failed to kill query: {e}")

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

Exception Classes (PEP 249 Compliant)

The driver provides a complete set of exception classes that follow the PEP 249 standard for database interfaces:

# Base exceptions
class Warning(Exception): ...
class Error(Exception): ...

# Interface errors
class InterfaceError(Error): ...

# Database errors
class DatabaseError(Error): ...

# Specific database error types
class DataError(DatabaseError): ...
class OperationalError(DatabaseError): ...
class IntegrityError(DatabaseError): ...
class InternalError(DatabaseError): ...
class ProgrammingError(DatabaseError): ...
class NotSupportedError(DatabaseError): ...

These exceptions are automatically mapped from Databend error codes to appropriate PEP 249 exception types based on the nature of the error.

Note: stream_load and load_file support an optional method parameter, it accepts two string values:

  • stage: Data is first uploaded to a temporary stage and then loaded. This is the default behavior.
  • streaming: Data is directly streamed to the Databend server.

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: ...
    def last_query_id(self) -> str | None: ...
    async def kill_query(self, query_id: str) -> 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]], method: str = None) -> ServerStats: ...
    async def load_file(self, sql: str, file: str, method: str = 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 last_query_id(self) -> str | None: ...
    def kill_query(self, query_id: str) -> 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]], method: str = None) -> ServerStats: ...
    def load_file(self, sql: str, file: str, method: str = None, format_option: dict = None, 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

Release history Release notifications | RSS feed

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

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

databend_driver-0.33.4-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl (8.5 MB view details)

Uploaded PyPymanylinux: glibc 2.28+ ARM64

databend_driver-0.33.4-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl (8.5 MB view details)

Uploaded PyPymanylinux: glibc 2.28+ ARM64

databend_driver-0.33.4-cp39-abi3-manylinux_2_28_aarch64.whl (8.5 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

databend_driver-0.33.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.2 MB view details)

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

databend_driver-0.33.4-cp39-abi3-macosx_11_0_arm64.whl (8.1 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

databend_driver-0.33.4-cp39-abi3-macosx_10_12_x86_64.whl (8.7 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

databend_driver-0.33.4-cp38-cp38-manylinux_2_28_aarch64.whl (8.7 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.28+ ARM64

databend_driver-0.33.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.4 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

databend_driver-0.33.4-cp38-cp38-macosx_11_0_arm64.whl (8.2 MB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

databend_driver-0.33.4-cp38-cp38-macosx_10_12_x86_64.whl (8.8 MB view details)

Uploaded CPython 3.8macOS 10.12+ x86-64

File details

Details for the file databend_driver-0.33.4-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.4-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7bf4353ad42dd77a724806a7927ff8f57fd5d002d189ee08d9ed1649c7ae8559
MD5 3ed18af14fdb3a16a1c0c59e5095147a
BLAKE2b-256 c0f240440fb9128bc7410f460969606d465c46ae3e829ad25efd0582b0637d2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.4-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl:

Publisher: release.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.33.4-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.4-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8e694e28634df5c9c17204c5f5ed63194a819ba67928ca3e6dee1f7d56cef6b3
MD5 1e45651b1827ee7eaf23bfc5a0696250
BLAKE2b-256 e33a75788b8895ad12b509d593dc4c802ad68eb4c555a96c20ecf0cd48ffdbd8

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.4-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl:

Publisher: release.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.33.4-cp39-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.4-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 320f4c270c40aac40016a9a82853dc936fc9da8ce22adf145a310097fbbfcb16
MD5 3efc06341e2196c692306f8bbe36f4db
BLAKE2b-256 b61d860f73378b3e98386a904d5632615d15354045848e4bafdda9ce48f76f45

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.4-cp39-abi3-manylinux_2_28_aarch64.whl:

Publisher: release.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.33.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 46f3d94b2297e69ce21e7fe79d675419ab14a86dca61ea7d3a5151554b5b7832
MD5 2d8e0ad9c4bd04b8c49cc4ca48bcb4b3
BLAKE2b-256 506438d71b66d67d26415b6e04110a72fac0c95f875399a182804e9198fea804

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.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.33.4-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.4-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7bff3ddc705c73a2683e9c7707100aaf66f6a2d80ad181095b20120b06b30480
MD5 dcdbf0f798d1495332966e412abf2b67
BLAKE2b-256 e7a16afff2bbd58b56e692cdd7dcdf42ffd931f08c560b9795dcfa2d388295d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.4-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release.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.33.4-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.4-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 757a18b5b32abf1f7416968e990be8ccb14e162f75e4e770e1df7b55b1f0c205
MD5 ee61a0b10a25b2e78b91ab475ae488f5
BLAKE2b-256 960b9fccab7c8b73c77698e2b92dd84fda74d4f355215ee1683635c60b8d4213

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.4-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: release.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.33.4-cp38-cp38-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.4-cp38-cp38-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6a535f31a636a0d5ab5f9b5bbf91b5117cb0a87d2a5e288b1b693f897d4c9316
MD5 9cfc9a2eb63f8c300c9841be4f81c795
BLAKE2b-256 f1ec39ca43a5a5234690d4df62b594aa367776737c6379fe0ef1767c74d7083b

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.4-cp38-cp38-manylinux_2_28_aarch64.whl:

Publisher: release.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.33.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4a0e7856eaf4b8a89e39e251a6156cb395399312e0498465a7b2faed513da312
MD5 dcf00789f9bf244ca8be4aac7b704e19
BLAKE2b-256 16b881d1b50c1a080b3a24198cb2193bd103ff922048c19751e6511ab376879e

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.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.33.4-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.4-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8f9675fd4cdefa9936c651c20f1d60ef1ae7921807974a604441e95a9028ccdb
MD5 6ee2661d752fc5f22617fdf2b2e4a6ea
BLAKE2b-256 012b9eb8c64babefe21f2656a3260afc53ffb80ad296ee42ae2b5004c03a9bdf

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.4-cp38-cp38-macosx_11_0_arm64.whl:

Publisher: release.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.33.4-cp38-cp38-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.4-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 cbbb5c80869370bc63beb2d1282166d6e4c57a2bbfe83f0e304af9366c1b2e52
MD5 f74193434bb351ead5582dea514021aa
BLAKE2b-256 93078cd2c13b0c5612433690291ef38928a4332999c3dd2baa2f891ab3ba18be

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.4-cp38-cp38-macosx_10_12_x86_64.whl:

Publisher: release.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 Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page