Backends¶
Choosing a backend (koality.backends)¶
Backends: how koality reads source tables from, and writes results to, the database behind database_accessor.
Koality picks the backend registered for the attached database's type in duckdb_databases(), and falls back
to DuckDBBackend, which handles every database DuckDB attaches as a catalog. Third-party packages can add
backends with register_backend or through the koality.backends entry point group, whose entries load a
Backend subclass.
backend_for ¶
backend_for(duckdb_client: DuckDBPyConnection, database_accessor: str, database_provider: DatabaseProvider | None, *, owns_connection: bool = False) -> Backend
Return the backend for the attached database database_provider.
Without an attached database, or for a type without its own backend, the backend registered for "duckdb"
is used (DuckDBBackend unless replaced), since DuckDB attaches those databases as catalogs.
Source code in src/koality/backends/__init__.py
register_backend ¶
Use backend for attached databases of type backend.type; usable as a class decorator.
Source code in src/koality/backends/__init__.py
Backend interface (koality.backends.base)¶
The interface between koality and the database attached as database_accessor.
Backend ¶
Backend(duckdb_client: DuckDBPyConnection, database_accessor: str, database_provider: DatabaseProvider | None, *, owns_connection: bool = False)
Bases: SqlDialect, ABC
How koality reads source tables from, and writes results to, one kind of attached database.
Koality copies the rows its checks need into DuckDB and runs every check there, so a backend only
covers the source side: naming a source table, writing filter values, date columns and date literals
for the source's SQL (SqlDialect, DuckDB's by default), running SQL against the source, and appending
results to result_table.
A backend is chosen by the attached database's type in duckdb_databases() (e.g. "bigquery");
register one with koality.backends.register_backend or the koality.backends entry point group.
The type comes from the database attached in database_setup, so a source needs a DuckDB extension that
attaches it with its own type. A backend may still fetch the data another way (e.g. through the source's
API) as long as query returns a DuckDB relation on duckdb_client, for instance
self.duckdb_client.from_arrow(...); a relation from another connection (such as duckdb.from_arrow)
cannot be staged. Failures should be raised as duckdb.Error or koality.exceptions.DatabaseError, which
koality reports as database errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
duckdb_client
|
DuckDBPyConnection
|
The DuckDB connection koality runs on. |
required |
database_accessor
|
str
|
The name of the attached database ("" if there is none). |
required |
database_provider
|
DatabaseProvider | None
|
The attached database's entry in |
required |
owns_connection
|
bool
|
Whether koality created the connection, so that in-memory data is lost when koality exits. |
False
|
Source code in src/koality/backends/base.py
columns ¶
Return the column names of the source table, e.g. to check a config's columns against the database.
The default binds SELECT * ... LIMIT 0 through query, which DuckDB catalogs answer from their
metadata without reading rows; override it for a source that would scan the table instead.
Raises:
| Type | Description |
|---|---|
Error | DatabaseError
|
If the table doesn't exist or can't be read. |
Source code in src/koality/backends/base.py
date_column ¶
date_literal ¶
format_filter_value ¶
Format a filter value (or the parenthesized list for IN / NOT IN) as an SQL literal.
plan ¶
Return the source's plan of sql without running it (for koality explain --connect), or None.
The default is None: a backend that sends its queries to the source (e.g. BigQuery's) can't have them planned without sending them.
Raises:
| Type | Description |
|---|---|
Error | DatabaseError
|
If |
Source code in src/koality/backends/base.py
query
abstractmethod
¶
table_ref
abstractmethod
¶
write_results
abstractmethod
¶
Append result rows to result_table, creating it if needed, and return where they were written.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result_table
|
str
|
The configured |
required |
rows
|
list[dict]
|
Aggregated result dicts with the keys DATE, METRIC_NAME, TABLE, |
required |
identifier_column
|
str
|
The name of the identifier field (e.g. IDENTIFIER). |
required |
now
|
datetime
|
The insert timestamp (UTC). |
required |
Source code in src/koality/backends/base.py
result_columns ¶
Return the result_table columns in order, with their types named by type_names.
type_names maps each generic type ("date", "text", "float" and "timestamp") to the database's type name.
Source code in src/koality/backends/base.py
DuckDB catalogs (koality.backends.duckdb_catalog)¶
The default backend: any database DuckDB attaches as a catalog (DuckDB files, Postgres, MySQL, SQLite, ...).
DuckDBBackend ¶
DuckDBBackend(duckdb_client: DuckDBPyConnection, database_accessor: str, database_provider: DatabaseProvider | None, *, owns_connection: bool = False)
Bases: Backend
Reads and writes through DuckDB's catalog of the attached database, or the session's own tables.
Used for every attached database without a dedicated backend. table and result_table resolve
inside the database_accessor catalog (see koality.utils.qualify_table_name).
Source code in src/koality/backends/base.py
plan ¶
Return DuckDB's plan of sql (as EXPLAIN shows it), which binds the query without running it.
Source code in src/koality/backends/duckdb_catalog.py
query ¶
table_ref ¶
Return table qualified with the accessor catalog, or a TEMP table of that name if only that exists.
Source code in src/koality/backends/duckdb_catalog.py
write_results ¶
Append rows to result_table in one INSERT with bound parameters.
Source code in src/koality/backends/duckdb_catalog.py
BigQuery (koality.backends.bigquery)¶
Backend for BigQuery, attached through DuckDB's community bigquery extension.
BigQueryBackend ¶
BigQueryBackend(duckdb_client: DuckDBPyConnection, database_accessor: str, database_provider: DatabaseProvider | None, *, owns_connection: bool = False)
Bases: Backend
Runs SQL in BigQuery itself, through bigquery_query (reads, which supports views) and bigquery_execute.
table and result_table are passed to BigQuery as written (dataset.table or project.dataset.table),
and filter values are written as BigQuery literals. query_options are named arguments passed to every
bigquery_query call; a subclass setting it to {"use_rest_api": True} reads results through the REST API
instead of the Storage Read API (e.g. for an emulator, or without permission to create read sessions).
Source code in src/koality/backends/base.py
columns ¶
Return the column names of table from its dataset's INFORMATION_SCHEMA, without reading its rows.
A SELECT * ... LIMIT 0 would be billed by BigQuery as a scan of the whole table. For a wildcard
table (e.g. events_*), BigQuery uses the schema of the most recently created table it matches, so
that table's columns are returned, with the _TABLE_SUFFIX pseudo-column.
Source code in src/koality/backends/bigquery.py
format_filter_value ¶
Format a filter value as a BigQuery literal.
Source code in src/koality/backends/bigquery.py
query ¶
Run sql in BigQuery: bigquery_execute for statements that write, bigquery_query otherwise.
Source code in src/koality/backends/bigquery.py
table_ref ¶
write_results ¶
Append rows to result_table through BigQuery, with values as escaped BigQuery literals.
Source code in src/koality/backends/bigquery.py
bigquery_literal ¶
Return value as a BigQuery SQL literal.
Strings are single-quoted with backslash escapes (BigQuery does not accept doubled quotes), and
infinite or NaN floats are cast from their string form, as BigQuery has no literal for them.
For example, O'Brien becomes 'O\'Brien' and -inf becomes CAST('-Infinity' AS FLOAT64).