Skip to main content

sqlalchemy-materialize

A SQLAlchemy dialect for Materialize, using the psycopg2 driver. Connect with a materialize+psycopg2:// URL and inspect columns and indexes through Materialize's system catalogs.


Why this package exists

Materialize provides a PostgreSQL-compatible connection interface, but its system catalogs differ from PostgreSQL's. This package extends SQLAlchemy's PostgreSQL dialect with reflection methods that query Materialize's catalogs.

The connection and SQL compilation behavior come from SQLAlchemy's PostgreSQL and psycopg2 dialects. Materialize-specific column and index reflection is implemented in this package.


Installation

pip install sqlalchemy-materialize

This also installs SQLAlchemy 2.0 (>=2.0,<2.1) and psycopg2 (>=2.9). Installing psycopg2 from source may require PostgreSQL development libraries and a C compiler.


Usage

from sqlalchemy import create_engine, text
from sqlalchemy.engine import URL

url = URL.create(
    "materialize+psycopg2",
    username="YOUR_USER",
    password="YOUR_PASSWORD",
    host="YOUR_HOST",
    port=6875,
    database="materialize",
    query={"sslmode": "require"},
)

engine = create_engine(url)

with engine.connect() as connection:
    print(connection.execute(text("SELECT 1")).scalar_one())

engine.dispose()

Replace the placeholders with your connection details. Adjust the port, database, and SSL settings for your deployment. URL.create() handles special characters in passwords without manual URL encoding.


What it adds

Feature Description
materialize+psycopg2:// connection URL Registers the Materialize dialect with SQLAlchemy using the psycopg2 driver.
Column reflection Reads column names, types, nullability, and defaults from Materialize's catalogs.
Index reflection Reflects column indexes and expression indexes, including the order of indexed columns.
Bulk reflection Implements get_multi_columns and get_multi_indexes for inspecting multiple relations.
Relation filtering Supports schema, relation-name, object-kind, and temporary-object scope filters for column and index reflection.
Tables and views Reflection lookup supports tables, views, and materialized views.
Constraint reflection Returns empty check-constraint and foreign-key lists for matching Materialize relations.
Statement caching Enables SQLAlchemy's compiled SQL cache. Query results are not cached.

For example, inspect an existing relation using a configured engine:

from sqlalchemy import inspect

inspector = inspect(engine)
columns = inspector.get_columns("my_table", schema="public")
indexes = inspector.get_indexes("my_table", schema="public")

Replace my_table and public with your relation and schema. Run inspection before disposing of the engine.


Known limits

  • Types not recognized by the inherited PostgreSQL type parser produce a warning and are reflected as SQLAlchemy NullType.
  • Check-constraint and foreign-key reflection returns empty lists for matching relations. To inspect constraints in an upstream database, connect directly to it using its corresponding dialect.
  • This dialect raises NotImplementedError for two-phase commit recovery.

Development

From the repository root, install the package in editable mode:

python -m pip install -e .
python -m pip install pytest

Run the tests:

python -m pytest

Statement-cache tests

tests/test_statement_cache.py checks compilation reuse with changing pagination values and expanding IN lists. These tests use SQLAlchemy's internal compilation API to compare cached parameters with freshly compiled statements.

Optional read-only tests check actual cache misses and hits through a real Materialize connection and compare results with caching disabled. They cover changing scalar parameters, pagination, expanding IN lists (including empty lists), integer/string/boolean/numeric/date values, and column/index reflection. Set a connection URL for a test instance to run them:

export MATERIALIZE_TEST_URL='materialize+psycopg2://USER:PASSWORD@HOST:6875/materialize?sslmode=require'
python -m pytest tests/test_statement_cache.py -v

URL-encode special characters in credentials when using this URL string. Without MATERIALIZE_TEST_URL, the live tests are skipped. The reflection test needs at least one existing table or view visible to the connected user, and skips if none is available. The tests do not create or modify database objects.


License

This project is licensed under the Apache License 2.0. See LICENSE for the full license text.

Metadata

Release files for sqlalchemy-materialize 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for sqlalchemy-materialize 0.1.6
File Interpreter ABI Platform Actions
sqlalchemy_materialize-0.1.6-py3-none-any.whl Python 3 none any Details for sqlalchemy_materialize-0.1.6-py3-none-any.whl

Release files / sqlalchemy_materialize-0.1.6-py3-none-any.whl

Download URL sqlalchemy_materialize-0.1.6-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
33c66ffbaede3d58fad378c463b87fc8dee2be9e9c1061f5845f85488afbc83c
BLAKE2b-256 checksum
How to use checksums
5aa63c69ce1392c00de0fecb57a949b62ba650b92efa9fd81f58cc5ee739fe9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for sqlalchemy-materialize 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page