Metadata-Version: 2.4
Name: featuremesh-adbc
Version: 0.1.0
Summary: Run FeatureQL through ADBC: the FeatureMesh registry translates, your database's ADBC driver executes.
Keywords: featureql,featuremesh,adbc,arrow,flight-sql,duckdb,sql,feature-store
Author: FeatureMesh
Author-email: FeatureMesh <hello@featuremesh.com>
License-Expression: LicenseRef-Proprietary
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS
Classifier: Topic :: Database
Classifier: Topic :: Database :: Front-Ends
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Dist: pyarrow>=18.0.0
Requires-Dist: adbc-driver-manager>=1.0.0
Requires-Dist: httpx>=0.27.0
Maintainer: FeatureMesh
Maintainer-email: FeatureMesh <hello@featuremesh.com>
Requires-Python: >=3.12
Project-URL: Homepage, https://featuremesh.com
Project-URL: Documentation, https://docs.featuremesh.com/docs/guides/third_party_clients/adbc
Project-URL: Changelog, https://docs.featuremesh.com/docs/guides/changelog
Description-Content-Type: text/markdown

# FeatureMesh ADBC

Run FeatureQL from Python code that uses [ADBC](https://arrow.apache.org/adbc/), Arrow's database
connectivity API, and get Arrow tables back. It works with DuckDB, PostgreSQL, Trino, ClickHouse,
BigQuery, Snowflake and Databricks, and with featuremeshd, FeatureMesh's serving runtime.

Documentation: [Query FeatureQL through ADBC](https://docs.featuremesh.com/docs/guides/third_party_clients/adbc).

## How it works

The package wraps an ADBC driver and runs in one of two modes.

**Transpiled mode**: your own database runs the query. The package sends each statement to a
FeatureMesh registry, gets back SQL for your database, and runs that SQL through the database's
ADBC driver. Only the statement text goes to the registry, never your data.

```
FeatureQL ──► featuremesh-adbc ──(1) statement──────────► FeatureMesh registry
                    │          ◄─(2) SQL for your database──┘
                    │
                    └──(3) SQL──► ADBC driver ──► your database ──► Arrow table
```

**Direct mode** (the default): the statement goes to the driver unchanged. Use it with the Flight
SQL driver against featuremeshd, which reads FeatureQL itself.

```
FeatureQL ──► featuremesh-adbc ──► Flight SQL driver ──► featuremeshd ──► Arrow table
```

## Install

```bash
pip install featuremesh-adbc
```

Then install the ADBC driver of your database. The `driver` name you pass to `connect()` picks it:

| Database | `driver` | Install the driver with |
|---|---|---|
| DuckDB | `duckdb` | `pip install duckdb` (the driver ships inside it) |
| PostgreSQL | `postgresql` | `pip install adbc-driver-postgresql` |
| BigQuery | `bigquery` | `pip install adbc-driver-bigquery` |
| Snowflake | `snowflake` | `pip install adbc-driver-snowflake` |
| featuremeshd | `flightsql` | `pip install adbc-driver-flightsql` |
| Trino | `trino` | `dbc install trino` |
| ClickHouse | `clickhouse` | `dbc install clickhouse` |
| Databricks | `databricks` | `dbc install databricks` |

Trino, ClickHouse and Databricks have no pip package: install them with
[dbc](https://docs.columnar.tech/dbc/), Columnar's ADBC driver installer. The package loads the pip
driver when it is installed, and otherwise the driver dbc installed. A missing driver raises
`InterfaceError` naming both install commands.

Requirements: Python 3.12 or later. The transpiled mode needs a FeatureMesh registry 0.4.1 or later
(the managed one is `https://api.featuremesh.com/v1/featureql`) and a registry token. The direct
mode needs featuremeshd 0.3.1 or later and a project user or service account token.

## Run FeatureQL on your database (transpiled mode)

```python
from featuremesh_adbc import dbapi

FEATUREMESH = {
    "adbc.featuremesh.execution_mode": "transpiled",
    "adbc.featuremesh.registry_url": "https://api.featuremesh.com/v1/featureql",
    "adbc.featuremesh.registry_token": "<your token>",
}

with dbapi.connect(driver="duckdb", path=":memory:", **FEATUREMESH) as con, con.cursor() as cursor:
    cursor.execute("""
        DEFINE id := INPUT(BIGINT)
        RETURN id, double := id * 2
        FOR id := BIND_VALUES(SEQUENCE(1, 3))
        ORDER BY id
    """)
    print(cursor.fetch_arrow_table().to_pylist())
    # [{'ID': 1, 'DOUBLE': 2}, {'ID': 2, 'DOUBLE': 4}, {'ID': 3, 'DOUBLE': 6}]
```

`cursor.fetch_df()` returns a pandas data frame instead (it needs `pandas`).

### Connect to each database

Only the driver and its options change; the FeatureMesh options and the FeatureQL stay the same.
Every other keyword is an ADBC option of the driver, passed as is (flat, or grouped under
`db_kwargs`).

```python
# PostgreSQL
dbapi.connect(driver="postgresql", uri="postgresql://user:password@host:5432/database", **FEATUREMESH)

# Trino
dbapi.connect(driver="trino", uri="http://user@host:8080?catalog=memory&schema=default", **FEATUREMESH)

# ClickHouse (HTTP interface)
dbapi.connect(driver="clickhouse", uri="http://host:8123", username="user", password="password", **FEATUREMESH)

# BigQuery (Google application default credentials); the driver needs a default dataset
dbapi.connect(
    driver="bigquery",
    **{"adbc.bigquery.sql.project_id": "my-project", "adbc.bigquery.sql.dataset_id": "my_dataset"},
    **FEATUREMESH,
)

# Snowflake (programmatic access token)
dbapi.connect(
    driver="snowflake",
    username="user",
    **{
        "adbc.snowflake.sql.account": "myorg-myaccount",
        "adbc.snowflake.sql.auth_type": "auth_pat",
        "adbc.snowflake.sql.client_option.auth_token": "<snowflake token>",
        "adbc.snowflake.sql.warehouse": "my_warehouse",
    },
    **FEATUREMESH,
)

# Databricks (personal access token)
dbapi.connect(driver="databricks", uri="databricks://token:<pat>@<host>:443/<http path>", **FEATUREMESH)
```

### What the registry translates

- A FeatureQL statement (`RETURN ...`, `DEFINE ... RETURN ...`) is translated for your database:
  `RETURN a := ARRAY[1, 2, 3]` runs on each of them.
- A plain SQL statement (`SELECT ...`) comes back unchanged, so it must already be valid on your
  database: `SELECT ARRAY[1, 2, 3]` fails on Snowflake and Databricks.
- The registry writes SQL for the backend named after the driver (`postgresql` is sent as
  `postgres`). When they differ, for example a Flight SQL server in front of another engine, set
  `adbc.featuremesh.backend`.
- `"adbc.featuremesh.debug": True` prints the SQL the registry returned.

## Send FeatureQL to featuremeshd (direct mode)

featuremeshd answers FeatureQL on its Flight SQL port (10093 by default) and checks every caller:
send a project user or service account token in the `Authorization` header.

```python
from featuremesh_adbc import dbapi

auth = {"adbc.flight.sql.authorization_header": "Bearer <your token>"}

with dbapi.connect(driver="flightsql", uri="grpc://<featuremeshd host>:10093", **auth) as con, con.cursor() as cursor:
    cursor.execute("RETURN F1 := 1, F2 := 2, F3 := F1 + F2;")
    print(cursor.fetch_arrow_table().to_pylist())  # [{'f1': 1, 'f2': 2, 'f3': 3}]
```

In this mode the cursor is the driver's own and the registry options are not used.

## Options

| Option | Values | Default |
|---|---|---|
| `adbc.featuremesh.execution_mode` | `direct`: the statement goes to the driver unchanged. `transpiled`: the registry translates it first | `direct` |
| `adbc.featuremesh.registry_url` | The registry's FeatureQL endpoint. Required in the transpiled mode | none |
| `adbc.featuremesh.registry_token` | Your registry token. Required in the transpiled mode | none |
| `adbc.featuremesh.backend` | The backend the registry writes SQL for | the driver's name (`postgresql` → `postgres`) |
| `adbc.featuremesh.debug` | `True` prints the SQL the registry returned | `False` |

## Results differ by engine

The rows are the same; each engine names and types the columns its own way:

- FeatureQL names are case-insensitive. In the transpiled mode the databases return them in upper
  case (`ID`); featuremeshd returns them in lower case (`id`).
- Snowflake's driver returns an ARRAY as JSON text (`'[1, 2, 3]'`) and integers as `Decimal`.

## When a statement is refused

The cursor runs one SQL statement per call. A FeatureQL statement whose translation needs several
steps (temporary tables created first, for example) is refused with `PLAN-NEEDS-STEPS`: run it with
the [featuremesh Python library](https://pypi.org/project/featuremesh/), which runs every step.

A registry newer than this package can answer with a plan the package does not know. The package
refuses it with `PLAN-VERSION-UNSUPPORTED` or `PLAN-UNKNOWN-VALUE` before running anything: upgrade
the package.

## License

Proprietary: see the LICENSE file in the package. Contact hello@featuremesh.com for other uses.
