Skip to main content

ADBC (Arrow Database Connectivity) driver for Google Cloud Spanner

Project description

adbc-driver-spanner

A Python ADBC driver for Google Cloud Spanner.

It bundles the prebuilt native driver (a Rust cdylib) and exposes it through adbc_driver_manager, so you get a standard DBAPI 2.0 connection whose results come back as Apache Arrow — ready for pandas, polars, DuckDB, or PyArrow with no per-row conversion.

Install

pip install adbc-driver-spanner
# for the DataFrame / Arrow helpers used below:
pip install adbc-driver-spanner[dbapi] pandas

Prebuilt wheels are published for Linux (x86-64, aarch64), macOS (arm64, x86-64), and Windows (x86-64, arm64).

Quickstart

import adbc_driver_spanner.dbapi as spanner

with spanner.connect(
    database="projects/my-project/instances/my-instance/databases/my-db",
) as conn:
    with conn.cursor() as cur:
        cur.execute("SELECT SingerId, FirstName FROM Singers")
        df = cur.fetch_df()          # -> pandas.DataFrame

Connection options

Options mirror the driver's adbc.spanner.* keys:

kwarg driver option
database= adbc.spanner.database
endpoint= adbc.spanner.endpoint
emulator= adbc.spanner.emulator
keyfile= adbc.spanner.keyfile
keyfile_json= adbc.spanner.keyfile_json

Credentials default to Application Default Credentials; pass keyfile= / keyfile_json= for a service account, or point at the emulator:

# docs-test: skip
spanner.connect(database="projects/p/instances/i/databases/d",
                endpoint="localhost:9010", emulator=True)

Cookbook

Every snippet below is executed against the Spanner emulator in CI, so they stay correct. They assume a Singers(SingerId INT64, FirstName STRING) table.

Two things to know:

  • DBAPI is autocommit-off by default, so DML and ingest need a conn.commit() (or pass autocommit=True). Reads need neither.
  • The DataFrame / Arrow paths need the [dbapi] extra (pyarrow).

pyarrow — zero-copy Arrow:

import adbc_driver_spanner.dbapi as spanner

with spanner.connect(
    database="projects/my-project/instances/my-instance/databases/my-db",
) as conn:
    with conn.cursor() as cur:
        cur.execute("SELECT SingerId, FirstName FROM Singers ORDER BY SingerId")
        table = cur.fetch_arrow_table()      # -> pyarrow.Table

polars — read straight from the connection:

import polars as pl
import adbc_driver_spanner.dbapi as spanner

with spanner.connect(
    database="projects/my-project/instances/my-instance/databases/my-db",
) as conn:
    df = pl.read_database(
        "SELECT SingerId, FirstName FROM Singers ORDER BY SingerId",
        connection=conn,                     # an ADBC connection, not a URI
    )

DuckDB — query the fetched Arrow table in-process:

import duckdb
import adbc_driver_spanner.dbapi as spanner

with spanner.connect(
    database="projects/my-project/instances/my-instance/databases/my-db",
) as conn:
    with conn.cursor() as cur:
        cur.execute("SELECT SingerId, FirstName FROM Singers")
        singers = cur.fetch_arrow_table()

# `singers` is a pyarrow.Table; DuckDB queries it by variable name, no copy.
top = duckdb.sql("SELECT COUNT(*) AS n, MIN(FirstName) AS first FROM singers").fetchone()

Insert a DataFrame (bulk ingest):

import pandas as pd
import pyarrow as pa
import adbc_driver_spanner.dbapi as spanner

frame = pd.DataFrame({"SingerId": [10, 11], "FirstName": ["Carol", "Dave"]})

with spanner.connect(
    database="projects/my-project/instances/my-instance/databases/my-db",
    autocommit=True,                         # apply immediately; returns the row count
) as conn:
    with conn.cursor() as cur:
        # The target table must already exist — only append mode is supported.
        rows = cur.adbc_ingest("Singers", pa.Table.from_pandas(frame), mode="append")

Partitioned reads and Data Boost

A large scan can be split into independent partitions and read in parallel — optionally on Spanner's serverless Data Boost compute, so the work is isolated from your provisioned instance. This uses the ADBC partitioned-execution extension (adbc_execute_partitions / adbc_read_partition):

import adbc_driver_spanner.dbapi as spanner

with spanner.connect(
    database="projects/my-project/instances/my-instance/databases/my-db",
) as conn:
    with conn.cursor() as cur:
        # Optional statement options, set on the underlying ADBC statement:
        cur.adbc_statement.set_options(**{
            "adbc.spanner.data_boost_enabled": "true",  # run on Data Boost
            "adbc.spanner.max_partitions": "8",          # cap the partition count
        })
        partitions, schema = cur.adbc_execute_partitions("SELECT SingerId FROM Singers")

    # Each descriptor is opaque bytes; it can be shipped to another worker,
    # process, or connection and read independently.
    for token in partitions:
        with conn.cursor() as cur:
            cur.adbc_read_partition(token)
            table = cur.fetch_arrow_table()
            ...

The Data Boost choice is baked into each descriptor, so it is honoured wherever the partition is read. Only single-table scans are partitionable — queries with an ORDER BY or aggregation are not.

How this package is built

The wheel is data-only: it does not compile anything at install time and links nothing against Python. CI (.github/workflows/libraries.yml) builds the native library per platform, drops it into adbc_driver_spanner/, and packages a py3-none-<platform> wheel. See that workflow for the release wiring.

Project details


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.

adbc_driver_spanner-0.5.0-py3-none-win_arm64.whl (6.2 MB view details)

Uploaded Python 3Windows ARM64

adbc_driver_spanner-0.5.0-py3-none-win_amd64.whl (6.5 MB view details)

Uploaded Python 3Windows x86-64

adbc_driver_spanner-0.5.0-py3-none-manylinux_2_35_x86_64.whl (6.4 MB view details)

Uploaded Python 3manylinux: glibc 2.35+ x86-64

adbc_driver_spanner-0.5.0-py3-none-manylinux_2_35_aarch64.whl (6.1 MB view details)

Uploaded Python 3manylinux: glibc 2.35+ ARM64

adbc_driver_spanner-0.5.0-py3-none-macosx_11_0_arm64.whl (5.9 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

adbc_driver_spanner-0.5.0-py3-none-macosx_10_12_x86_64.whl (6.1 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

File details

Details for the file adbc_driver_spanner-0.5.0-py3-none-win_arm64.whl.

File metadata

File hashes

Hashes for adbc_driver_spanner-0.5.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 bfd9f8c138b3d37a44cf6da5810fbc65457afe6484adcb63e4ea8efab2a50687
MD5 aab269ce02ce4f34d60d861b0f3606c0
BLAKE2b-256 80f0dc0b38816d011afe6aefa59968bfb803ed3ad1a8d926f12d9dea2161bef6

See more details on using hashes here.

Provenance

The following attestation bundles were made for adbc_driver_spanner-0.5.0-py3-none-win_arm64.whl:

Publisher: libraries.yml on fornwall/adbc-spanner

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

File details

Details for the file adbc_driver_spanner-0.5.0-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for adbc_driver_spanner-0.5.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 1963a80566e5a529518e2f96a6983d5772ed3455295b4f7a9f52d74aa4e30ebf
MD5 32b07533aee404c7f2154099ebacecfc
BLAKE2b-256 56bfadc719e543763b36cc25990cce28b981fd544fa421a74ae21334a5e12ee8

See more details on using hashes here.

Provenance

The following attestation bundles were made for adbc_driver_spanner-0.5.0-py3-none-win_amd64.whl:

Publisher: libraries.yml on fornwall/adbc-spanner

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

File details

Details for the file adbc_driver_spanner-0.5.0-py3-none-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for adbc_driver_spanner-0.5.0-py3-none-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 708d1151b4f71cbad9effbcb08a0383cb7d8244b7fb0ddc7ad97fb2e1b040679
MD5 a4159400bc9a057b2ec952eede2a54a0
BLAKE2b-256 873521ce5a8ab64dda94d9b7f617cbdbcdb6cf1595f59f3d0b794d87b71813bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for adbc_driver_spanner-0.5.0-py3-none-manylinux_2_35_x86_64.whl:

Publisher: libraries.yml on fornwall/adbc-spanner

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

File details

Details for the file adbc_driver_spanner-0.5.0-py3-none-manylinux_2_35_aarch64.whl.

File metadata

File hashes

Hashes for adbc_driver_spanner-0.5.0-py3-none-manylinux_2_35_aarch64.whl
Algorithm Hash digest
SHA256 501697edd504c7d1d1674ff2ff41b368b2c8ffa05743c6e81649ecc18f1e4b3a
MD5 831b4d36da55d32fcc7741e7b2b863a5
BLAKE2b-256 2fb7cc650a33e0c86255a6c64d45bf98431b5c3253630d097cf8154e167c56b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for adbc_driver_spanner-0.5.0-py3-none-manylinux_2_35_aarch64.whl:

Publisher: libraries.yml on fornwall/adbc-spanner

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

File details

Details for the file adbc_driver_spanner-0.5.0-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for adbc_driver_spanner-0.5.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3eac241f657f0cdac2dd811289c0107e6f44f2ba59ff031b80e46dd02bd3a894
MD5 6ebc866fc6656ee08cbbd9cf2fa8a7cd
BLAKE2b-256 b2afead915aafb5951ed87cca90e58c5308a9c2c7b8bdef0bd5ab852b6ede31c

See more details on using hashes here.

Provenance

The following attestation bundles were made for adbc_driver_spanner-0.5.0-py3-none-macosx_11_0_arm64.whl:

Publisher: libraries.yml on fornwall/adbc-spanner

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

File details

Details for the file adbc_driver_spanner-0.5.0-py3-none-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for adbc_driver_spanner-0.5.0-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9cbaeab85fa352fbab9b3913b027aeb9fcd59a099e4b9597140f5fadf786305a
MD5 00af6d5f828742857fcf286ef844f94a
BLAKE2b-256 d7d3bc69f0e75bec5d87c9421ac3f2ecd3647f17e35d2c311fe1d8b584592e98

See more details on using hashes here.

Provenance

The following attestation bundles were made for adbc_driver_spanner-0.5.0-py3-none-macosx_10_12_x86_64.whl:

Publisher: libraries.yml on fornwall/adbc-spanner

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