Metadata-Version: 2.4
Name: adbc-driver-kinetica
Version: 7.2.1
Summary: Arrow ADBC driver for Kinetica
License: Apache-2.0
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: adbc-driver-manager>=1.0
Requires-Dist: pyarrow>=14
Provides-Extra: dataframe
Requires-Dist: pandas>=1.5; extra == "dataframe"

# adbc-driver-kinetica

The [Arrow ADBC](https://arrow.apache.org/adbc/) driver for
[Kinetica](https://www.kinetica.com/), packaged for Python.

The package carries the driver for your platform and hands it to the ADBC
driver manager, so the connection you get back is a standard ADBC one:
anything the
[ADBC Python documentation](https://arrow.apache.org/adbc/current/python/index.html)
describes works here. Query results come back from the server as Arrow, with
no row-by-row conversion in between.

## Install

```bash
pip install adbc-driver-kinetica
pip install "adbc-driver-kinetica[dataframe]"   # adds pandas, for fetch_df
```

Wheels are published for Linux (x86-64 and ARM64), macOS (Apple silicon) and
Windows (x86-64), for Python 3.11 or later. On Windows ARM64, the x64 build of
Python installs the x86-64 wheel; there is no ARM64 wheel yet, since `pyarrow`,
which the package needs, does not publish one. The driver needs Kinetica 7.2.3.24
or later, the first version to return query results as Arrow; it refuses a
connection to an older server when it opens, and says which version it found.

The package's version follows Kinetica's: `7.2.x` releases are for Kinetica
7.2. To take every release for 7.2 and none for a later Kinetica version:

```bash
pip install "adbc-driver-kinetica~=7.2.0"
```

## Connect

```python
import adbc_driver_kinetica.dbapi as kinetica

conn = kinetica.connect(
    "http://localhost:9191",
    db_kwargs={"username": "<user>", "password": "<password>"},
)
```

The address may also be a `kinetica://` URI,
`kinetica://[user[:password]@]host[:port][,host[:port]...][/path][?name=value&...]`.
It connects over HTTPS unless it says `tls=false`, and every other parameter
is an option under its own name, such as `kinetica.query.max_rows=1000`; an
option set directly takes precedence over the URI's.

## Query

```python
cur = conn.cursor()

cur.execute("SELECT 1 AS n, 'hello' AS s")
cur.fetchall()                      # [(1, 'hello')]
```

Arrow is the native result, so take it directly rather than row by row when the
result is large:

```python
cur.execute("SELECT * FROM my_table")

table = cur.fetch_arrow_table()     # pyarrow.Table
frame = cur.fetch_df()              # pandas.DataFrame, needs [dataframe]
```

Parameters use the driver's placeholders:

```python
cur.execute("SELECT * FROM my_table WHERE id = ?", (42,))
```

## Change data

```python
cur.execute("CREATE TABLE t (id INTEGER, name VARCHAR)")
cur.execute("INSERT INTO t VALUES (1, 'one'), (2, 'two')")
cur.execute("UPDATE t SET name = 'TWO' WHERE id = 2")
cur.execute("DELETE FROM t WHERE id = 1")
```

For anything more than a few rows, hand over an Arrow table and let the driver
route it to the worker ranks instead of building a long `INSERT`:

```python
import pyarrow as pa

batch = pa.table({"id": [1, 2, 3], "name": ["one", "two", "three"]})
cur.adbc_ingest("t", batch, mode="append")      # or "create", "replace"
```

## Read the catalog

```python
conn.adbc_get_table_schema("t")                 # pyarrow.Schema
conn.adbc_get_objects(depth="tables").read_all()
```

## Transactions

Kinetica has no multi-statement transactions. Autocommit is always on, so
`commit()` and `rollback()` are refused, and the driver manager warns once:

```
Warning: Cannot disable autocommit; conn will not be DB-API 2.0 compliant
```

The connection works normally. A tool that insists on calling them can have
them accepted and ignored instead, so that both succeed and do nothing:

```python
conn = kinetica.connect(
    "http://localhost:9191",
    db_kwargs={"username": "<user>", "password": "<password>",
               "kinetica.fake_transactions": "true"},
)
```

Nothing is actually held back either way: a statement has taken effect by the
time it returns.

## Temp tables and sessions

A connection is a server session, and the SQL `#name` temp tables it creates
belong to that session alone: two connections can each have their own `#work`
without colliding. Closing the connection drops them.

```python
cur.execute("CREATE TABLE #work (a INT)")
cur.execute("INSERT INTO #work VALUES (1)")
...
conn.close()                 # #work goes with the session
```

To share them instead, name the session. Every connection given the same id
sees the same `#name` tables:

```python
conn = kinetica.connect(
    "http://localhost:9191",
    db_kwargs={"username": "<user>", "password": "<password>",
               "kinetica.session_id": "reporting-run-42"},
)
```

A session you name is yours to drop, with `/drop/session` sent under the same
`X-Kinetica-Session` header; closing the connection leaves it alone. Left
alone entirely, the server drops any session idle for longer than
`sql.session_idle_timeout` (an hour by default).

## Without this package

The same driver can be installed for every language at once with
[dbc](https://docs.columnar.tech/dbc), and loaded by name through
`adbc-driver-manager` 1.8 or later:

```bash
dbc install kinetica
```

```python
import adbc_driver_manager.dbapi as dbapi

conn = dbapi.connect(
    driver="kinetica",
    db_kwargs={"uri": "http://localhost:9191",
               "username": "<user>", "password": "<password>"},
)
```

## Environment

| variable | meaning |
|---|---|
| `KINETICA_ADBC_LIB` | path to a driver library to load instead of the one the package carries; a path that does not exist is an error |
