Skip to content

table.py

DuckDBTable dataclass

Bases: IO[DuckDBPyRelation]

IO to load from and save to a DuckDB table.

Example:

>>> import duckdb
>>> from ordeq_duckdb import DuckDBTable
>>> connection = duckdb.connect(":memory:")
>>> table = DuckDBTable(
...     table="my_table",
...     connection=connection
... )
>>> table.save(
...     connection.values([123, "abc"])
... )
>>> connection.sql("SELECT * FROM my_table").show()
┌───────┬─────────┐
│ col0  │  col1   │
│ int32 │ varchar │
├───────┼─────────┤
│   123 │ abc     │
└───────┴─────────┘
<BLANKLINE>

Example in a node:

>>> from ordeq import node, run
>>> from pathlib import Path
>>> connection = duckdb.connect(":memory:")
>>> table = DuckDBTable(
...     table="my_data",
...     connection=connection,
... )
>>> @node(outputs=table)
... def convert_to_duckdb_relation() -> duckdb.DuckDBPyRelation:
...     return connection.values([2, "b"])
>>> result = run(convert_to_duckdb_relation)
>>> connection.table("my_data").show()
┌───────┬─────────┐
│ col0  │  col1   │
│ int32 │ varchar │
├───────┼─────────┤
│     2 │ b       │
└───────┴─────────┘
<BLANKLINE>

load()

Load the DuckDB table into a DuckDB relation.

Returns:

Type Description
DuckDBPyRelation

A relation representing the loaded table.

save(relation, mode='create')

Save a relation to the DuckDB table.

Parameters:

Name Type Description Default
relation DuckDBPyRelation

The relation to save.

required
mode Literal['create', 'insert']

The save mode. "create" will create the table, "insert" will insert into the table if it exists, or create it if it doesn't.

'create'

Raises:

Type Description
CatalogException

If the table already exists and mode is "create".