Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Ibis supports more than 20 backends:
- [SQLite](https://ibis-project.org/backends/sqlite/)
- [Snowflake](https://ibis-project.org/backends/snowflake)
- [Trino](https://ibis-project.org/backends/trino/)

- [Db2](https://ibis-project.org/backends/db2/)
## How it works

Most Python dataframes are tightly coupled to their execution engine. And many databases only support SQL, with no Python API. Ibis solves this problem by providing a common API for data manipulation in Python, and compiling that API into the backend’s native language. This means you can learn a single API and use it across any supported backend (execution engine).
Expand Down
173 changes: 173 additions & 0 deletions docs/backends/db2.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# IBM DB2

[https://www.ibm.com/products/db2](https://www.ibm.com/products/db2)

![](https://img.shields.io/badge/memtables-fallback-yellow?style=flat-square) ![](https://img.shields.io/badge/inputs-DB2 tables-blue?style=flat-square) ![](https://img.shields.io/badge/outputs-DB2 tables | CSV | pandas | Parquet | PyArrow-orange?style=flat-square)

## Install

Install Ibis and dependencies for the DB2 backend:

::: {.panel-tabset}

## `pip`

Install with the `db2` extra:

```{.bash}
pip install 'ibis-framework[db2]'
```

And connect:

```{.python}
import ibis

con = ibis.db2.connect() # <1>
```

1. Adjust connection parameters as needed.

## `conda`

Install for DB2:

```{.bash}
conda install -c conda-forge ibis-db2
```

And connect:

```{.python}
import ibis

con = ibis.db2.connect() # <1>
```

1. Adjust connection parameters as needed.

## `mamba`

Install for DB2:

```{.bash}
mamba install -c conda-forge ibis-db2
```

And connect:

```{.python}
import ibis

con = ibis.db2.connect() # <1>
```

1. Adjust connection parameters as needed.

:::

## Connect

### `ibis.db2.connect`

```python
con = ibis.db2.connect(
database="SAMPLE",
hostname="localhost",
port=50000,
username="db2inst1",
password="password",
schema="MYSCHEMA",
)
```

::: {.callout-note}
`ibis.db2.connect` is a thin wrapper around [`ibis.backends.db2.Backend.do_connect`](#ibis.backends.db2.Backend.do_connect).
:::

### Connection Parameters

```{python}
#| echo: false
#| output: asis
from _utils import render_do_connect

render_do_connect("db2")
```

### `ibis.connect` URL format

In addition to `ibis.db2.connect`, you can also connect to DB2 by
passing a properly-formatted DB2 connection URL to `ibis.connect`:

```python
con = ibis.connect(f"db2://{username}:{password}@{hostname}:{port}/{database}")
```

## DB2 Client Requirements

The DB2 backend requires the IBM DB2 client libraries to be installed. You can install them using:

```bash
pip install ibm_db
```

::: {.callout-note}
The `ibm_db` package includes `ibm_db_dbi` (the DB-API 2.0 compliant interface) and requires the DB2 client libraries to be installed on your system.
For installation instructions, see the [IBM DB2 documentation](https://www.ibm.com/docs/en/db2).
:::

## Features

The DB2 backend supports:

- **Temporary tables**: Create temporary tables for intermediate results
- **Schema operations**: List databases (schemas), tables, and get table schemas
- **Data manipulation**: Insert, create, and drop tables
- **Query execution**: Execute SQL queries and return results as pandas DataFrames or PyArrow tables
- **Type mapping**: Automatic conversion between Ibis types and DB2 types

## Limitations

- **Python UDFs**: Not currently supported
- **Streaming**: Not supported

## Example Usage

```python
import ibis

# Connect to DB2
con = ibis.db2.connect(
database="SAMPLE",
hostname="localhost",
port=50000,
username="db2inst1",
password="password"
)

# List available tables
tables = con.list_tables()
print(tables)

# Load a table
employee = con.table("EMPLOYEE")

# Query the table
result = employee.filter(employee.SALARY > 50000).execute()
print(result)

# Create a new table
con.create_table(
"new_table",
obj=result,
overwrite=True
)
```

```{python}
#| echo: false
BACKEND = "DB2"
```

{{< include ./_templates/api.qmd >}}
Loading
Loading