Metadata-Version: 2.4
Name: sqlalchemy-materialize
Version: 0.1.6
Summary: A SQLAlchemy dialect for Materialize
License: Apache-2.0
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: SQLAlchemy<2.1,>=2.0
Requires-Dist: psycopg2-binary>=2.9.13
Dynamic: description
Dynamic: description-content-type
Dynamic: license
Dynamic: license-file
Dynamic: requires-dist
Dynamic: summary

# 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

```bash
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

```python
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:

```python
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:

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

Run the tests:

```bash
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:

```bash
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](LICENSE)
for the full license text.
