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.7-cp39-abi3-manylinux_2_28_aarch64.whl (8.6 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

databend_driver-0.33.7-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.7-cp39-abi3-macosx_11_0_arm64.whl (8.1 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

databend_driver-0.33.7-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.7-cp38-cp38-manylinux_2_28_aarch64.whl (8.7 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.28+ ARM64

databend_driver-0.33.7-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.7-cp38-cp38-macosx_11_0_arm64.whl (8.2 MB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

databend_driver-0.33.7-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.7-cp39-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.7-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 30187cf54436b09e5dede65b37c9160e9a47742deaaad5b520019599467a55fd
MD5 4731c2943bc01ebee241d8ad797485db
BLAKE2b-256 b71403f82c28fb4f4c10e6cde35505d26e233e76e51baa9efc333ce99b1c3519

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.7-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.7-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.7-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bc33900f074a8bd3f897ed1dca7462a6680d7a9e16962b93e45b70aaeecd2714
MD5 04d149fa226c6a6e310943df93c0c0be
BLAKE2b-256 2f6963bdcdfc15406873c77c862ec76f5c422391d3af75d31602185b6ac45239

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.7-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.7-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.7-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d88e40cf59c94beb853565114029173d66b230dc6bbdc9ac2379d516eb178b02
MD5 af0618e80a4573b0e46228eaa06de6c7
BLAKE2b-256 c95afb6ee2cfc7fc1817cc236f1a1b9839e1551371a5312c6f8d473d33ddd5e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.7-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.7-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.7-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2ce53a71df7a808e4e54b4df23be1b39b015fbf437f22d1a67d538dbecd6749b
MD5 b7d6a8ce021c10118cc07a72e53ab50c
BLAKE2b-256 1ac427c3592ce9e39ac69fd386db7b8daa005129ed61d8a69bf352926dc3be7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.7-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.7-cp38-cp38-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.7-cp38-cp38-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6d6a9b425ea499487fb0c8499a659a7764e09d1c33459651b57029a89963b15c
MD5 2088197479ea077db298d36271f4a4f4
BLAKE2b-256 b39d5f0b14dc00243b6810aa19584345d5dfd224a5c47b1cc95a82566177c04a

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.7-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.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 71717364d5bed21b63971011c004d24deb8d963a9bdb6c0ede0034eb702cf0ac
MD5 62494b438aa99216cb428d6b7497ce03
BLAKE2b-256 88889e11b0c44a0da1cace85144350f45c2266b6f62944b51a3844940e6ebe57

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.7-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.7-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.7-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 89aa401fe59f00f7a7c7f7e1e7645e9d02dbb18649c480239bb3e3b8376dd20d
MD5 2a909870f82238cb9548ffdb692af653
BLAKE2b-256 c73426cee6ee3fd7f655c70493b0519bce3c4101d23ad87518649d205d0a1d8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.7-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.7-cp38-cp38-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for databend_driver-0.33.7-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 25657470114ca748c707a6ae44509fa976073cb31986c76bd1435bd1d2b12989
MD5 2b8aa591207160363c368223573a60ff
BLAKE2b-256 b65625e3687b09c67ef2ac5ba8c9227da85d37f56fe41da5389f6764b116f6e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for databend_driver-0.33.7-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