Skip to content

Extensions

DuckDB extensions declared in a config's extensions: installed once, loaded for every run.

An extension is only downloaded if it isn't installed in the extension directory yet, so a run needs no network access once koality setup installed the extensions, e.g. while building a Docker image.

ExtensionError

Bases: KoalityError

A declared DuckDB extension could not be installed or loaded.

ExtensionStatus dataclass

ExtensionStatus(extension: ExtensionConfig, installed: bool)

What load_extensions did with an extension: whether it had to install it first.

connect

connect(extension_directory: str | None = None) -> DuckDBPyConnection

Return an in-memory DuckDB connection that installs and loads extensions from extension_directory.

Source code in src/koality/extensions.py
def connect(extension_directory: str | None = None) -> duckdb.DuckDBPyConnection:
    """Return an in-memory DuckDB connection that installs and loads extensions from `extension_directory`."""
    return duckdb.connect(
        ":memory:",
        config={"extension_directory": extension_directory} if extension_directory else {},
    )

load_extensions

load_extensions(duckdb_client: DuckDBPyConnection, extensions: Iterable[ExtensionConfig], *, force_install: bool = False, install_hint: str = _RUN_INSTALL_HINT) -> list[ExtensionStatus]

Install each extension that isn't installed yet (or each one, with force_install), then load it.

install_hint is appended to the error when an extension can't be installed.

Raises:

Type Description
ExtensionError

If an extension can't be downloaded (e.g. offline) or doesn't load with this DuckDB version.

Source code in src/koality/extensions.py
def load_extensions(
    duckdb_client: duckdb.DuckDBPyConnection,
    extensions: Iterable[ExtensionConfig],
    *,
    force_install: bool = False,
    install_hint: str = _RUN_INSTALL_HINT,
) -> list[ExtensionStatus]:
    """Install each extension that isn't installed yet (or each one, with `force_install`), then load it.

    `install_hint` is appended to the error when an extension can't be installed.

    Raises:
        ExtensionError: If an extension can't be downloaded (e.g. offline) or doesn't load with this DuckDB version.

    """
    statuses = []
    for extension in extensions:
        install_mode = _install_mode(duckdb_client, extension.name)
        # Extensions built into DuckDB (e.g. parquet) can't be reinstalled
        install = install_mode is None or (force_install and install_mode != "STATICALLY_LINKED")
        if install:
            _install(duckdb_client, extension, force_install=force_install, install_hint=install_hint)
        try:
            duckdb_client.load_extension(extension.name)
        except duckdb.Error as e:
            msg = (
                f"Could not load DuckDB extension '{extension.name}' with DuckDB {duckdb.__version__}: {e}\n"
                "If it was installed for another DuckDB version or is damaged, reinstall it with "
                "`koality setup --force`; an extension from a custom repository must also be signed."
            )
            raise ExtensionError(msg) from e
        statuses.append(ExtensionStatus(extension, installed=install))
    return statuses