Skip to main content

A SQLAlchemy PostgreSQL dialect for ADBC (Arrow Database Connectivity)

Project description

pgarrow PyPI package Test suite Code coverage

A SQLAlchemy PostgreSQL dialect for ADBC (Arrow Database Connectivity)


Contents


Installation

pgarrow can be installed from PyPI using pip:

pip install pgarrow

Usage

pgarrow can be used using the postgresql+pgarrow dialect when creating a SQLAlchemy engine. For example, to create an engine for a PostgreSQL database at 127.0.0.1 (localhost) on port 5432 with user postgres and password password:

engine = sa.create_engine('postgresql+pgarrow://postgres:password@127.0.0.1:5432/')

Query returning built-in Python types

To run a query that returns built-in Python types, as is typical with SQLAlchemy:

import sqlalchemy as sa

engine = sa.create_engine('postgresql+pgarrow://postgres:password@127.0.0.1:5432/')

with engine.connect() as conn:
    results = conn.execute(sa.text("SELECT 1")).fetchall()

Query returning an Arrow table

To run a query that returns an Arrow table, which should be the most performant for large datasets, you must use SQLAlchemy's driver_connection to access the ADBC-level connection, create a cursor from it to run the query and fetch the table using fetch_arrow_table::

import sqlalchemy as sa

engine = sa.create_engine('postgresql+pgarrow://postgres:password@127.0.0.1:5432/')

with \
        engine.connect() as conn, \
        conn.connection.driver_connection.cursor() as cursor:

    cursor.execute("SELECT 1 AS a, 2.0::double precision AS b, 'Hello, world!' AS c")
    table = cursor.fetch_arrow_table()

Replace PostgreSQL table with an Arrow table

To insert data into the database from an Arrow table, a similar pattern must be used to use adbc_ingest:

import sqlalchemy as sa

engine = sa.create_engine('postgresql+pgarrow://postgres:password@127.0.0.1:5432/')
table = pa.Table.from_arrays([[1,], [2,], ['Hello, world!',]], schema=pa.schema([
    ('a', pa.int32()),
    ('b', pa.float64()),
    ('c', pa.string()),
]))

with \
        engine.connect() as conn, \
        conn.connection.driver_connection.cursor() as cursor:

    cursor.adbc_ingest("my_table", table, mode="create")
    conn.commit()

Create a table with SQLAlchemy and append an Arrow table

To create a table using SQLAlchemy, and then append an Arrow table to it:

import sqlalchemy as sa

metadata = sa.MetaData()
sa.Table(
    "my_table",
    metadata,
    sa.Column("a", sa.INTEGER),
    sa.Column("b", sa.DOUBLE_PRECISION),
    sa.Column("c", sa.TEXT),
    schema="public",
)
table = pa.Table.from_arrays([[1,], [2,], ['Hello, world!',]], schema=pa.schema([
    ('a', pa.int32()),
    ('b', pa.float64()),
    ('c', pa.string()),
]))

with \
        engine.connect() as conn, \
        conn.connection.driver_connection.cursor() as cursor:

    metadata.create_all(conn)
    cursor.adbc_ingest("my_table", table, mode="append")
    conn.commit()

Compatibility

  • Python >= 3.9 (tested on 3.9.0, 3.10.0, 3.11.1, 3.12.0, and 3.13.0)
  • PostgreSQL >= 13.0 (tested on 13.0, 14.0, 15.0, and 16.0)
  • SQLAlchemy >= 2.0.7 on Python < 3.13, and SQLAlchemy >= 2.0.31 on Python >= 3.13 (tested on 2.0.7 on Python before 3.13.0; and SQLAlchemy 2.0.31 on Python 3.13.0)
  • PyArrow >= 15.0.0 on Python < 3.13, and PyArrow >= 18.0.0 on Python >= 3.13.0 (tested on 15.0.0, 16.0.0, 17.0.0, 18.0.0, 19.0.0, 20.0.0 on Python before 3.13.0; and 18.0.0, 19.0.0, and 20.0.0 on Python 3.13.0)
  • adbc-driver-postgresql >= 1.6.0 (tested on 1.6.0)

Project details


Download files

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

Source Distribution

pgarrow-0.0.3.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

pgarrow-0.0.3-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file pgarrow-0.0.3.tar.gz.

File metadata

  • Download URL: pgarrow-0.0.3.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pgarrow-0.0.3.tar.gz
Algorithm Hash digest
SHA256 5cf6cee18fe18f981ecb9f19741f1738ebf7224dc08e7f55833e5c5944ca4bf1
MD5 e611a25e9aa9f856669f5855b8ec2154
BLAKE2b-256 336eff65f2645a9a6202bc3e3bd8002a8af3341529b8a0921760651ef8a09c8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgarrow-0.0.3.tar.gz:

Publisher: deploy-package-to-pypi.yaml on michalc/pgarrow

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

File details

Details for the file pgarrow-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pgarrow-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pgarrow-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f793592c07e01f18794edbd7058862ec5dcb7a3c70f5ee78c4040b236f157aec
MD5 4300d4272ac6b5148adcd076995c5cbf
BLAKE2b-256 18ed8af3e8ba038f1c36f023b9f6e2edfe0433d53a2edd18ec04722976d722e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgarrow-0.0.3-py3-none-any.whl:

Publisher: deploy-package-to-pypi.yaml on michalc/pgarrow

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