Skip to content

view.py

DuckDBView dataclass

Bases: IO[DuckDBPyRelation]

IO to load and save a DuckDB view.

Example:

>>> import duckdb
>>> from ordeq_duckdb import DuckDBView
>>> connection = duckdb.connect(":memory:")
>>> view = DuckDBView(
...     view="fruits",
...     connection=connection
... )
>>> data = connection.values([1, "apples", "red"])
>>> view.save(data)
>>> view.load()
┌───────┬─────────┬─────────┐
│ col0  │  col1   │  col2   │
│ int32 │ varchar │ varchar │
├───────┼─────────┼─────────┤
│     1 │ apples  │ red     │
└───────┴─────────┴─────────┘
<BLANKLINE>

By default, the view will be replaced if it already exists. To change this, pass replace=False to the save method:

>>> view = view.with_save_options(replace=False)
>>> view.save(data) # doctest: +SKIP
IOException('Failed to save DuckDBView(view='fruits', ...

Example in a node:

>>> from ordeq import node
>>> from ordeq_duckdb import DuckDBTable
>>> import duckdb
>>> connection = duckdb.connect(":memory:")
>>> fruits = DuckDBTable(
...     table="fruits",
...     connection=connection,
... )
>>> fruits_filtered = DuckDBView(
...     view="fruits_filtered",
...     connection=connection,
... )
>>> @node(inputs=fruits, outputs=fruits_filtered)
... def filter_fruits(
...     fruits: duckdb.DuckDBPyRelation
... ) -> duckdb.DuckDBPyRelation:
...     return fruits.filter("color = 'red'")

load()

Loads a DuckDB view.

Returns:

Type Description
DuckDBPyRelation

The DuckDB view.

save(relation, replace=True)

Saves a DuckDB relation to a DuckDB view.

Parameters:

Name Type Description Default
relation DuckDBPyRelation

The DuckDB relation to save.

required
replace bool

Whether to replace the view if it already exists.

True