Getting Started¶
This guide will help you get started with Koality for data quality monitoring.
Installation¶
Using pip¶
Using uv¶
Supported Databases¶
Koality uses DuckDB as its query engine. Currently supported:
- DuckDB (in-memory) - Fully supported
- Google Cloud BigQuery - Fully supported via DuckDB's BigQuery extension
- PostgreSQL - Fully supported via DuckDB's
postgresextension (setup)
Other databases that DuckDB attaches as a catalog (e.g. DuckDB files, MySQL, SQLite) use the default backend; databases that need special handling get their own backend. Contributions are welcome!
Basic Usage¶
1. Create a Configuration File¶
Create a YAML configuration file (checks.yaml) that defines your data quality checks:
name: my_dqm_checks
database_setup: |
ATTACH 'my_database.duckdb' AS my_db;
database_accessor: my_db
defaults:
result_table: dqm_results
filters:
partition_date:
column: date
value: "2024-01-01"
type: date
check_bundles:
- name: orders_checks
defaults:
table: orders
checks:
- check_type: NullRatioCheck
check_column: order_id
lower_threshold: 0.0
upper_threshold: 0.0
- check_type: CountCheck
check_column: "*"
lower_threshold: 1000
upper_threshold: 1000000
Understanding database_setup and database_accessor¶
extensions: DuckDB extensions to install (if missing) and load beforedatabase_setupruns, e.g.bigqueryfrom thecommunityrepository. See DuckDB extensions.database_setup: SQL commands executed when the CheckExecutor initializes. Use this to attach databases, create secrets, and configure connections.database_accessor: The alias/name of your attached database. Koality uses this to identify the database type and route queries appropriately.
Example for Google Cloud BigQuery:
name: bigquery_checks
extensions:
- name: bigquery
repository: community
database_setup: |
ATTACH 'project=my-gcp-project' AS bq (TYPE bigquery, READ_ONLY);
database_accessor: bq
When using BigQuery, Koality automatically uses the BigQuery Jobs API for reads (which supports views) and bigquery_execute() for writes.
2. Run Checks Programmatically¶
from pathlib import Path
from koality import CheckExecutor
from koality.models import Config
# Load configuration (as `koality run` reads it, e.g. an unquoted NO stays text)
config = Config.from_yaml(Path("checks.yaml").read_text())
# Create executor and run checks
executor = CheckExecutor(config)
results = executor()
# Handle results
if executor.check_failed:
print("Some checks failed:")
print(executor.get_failed_checks_msg())
else:
print("All checks passed!")
3. Run Checks via CLI¶
Exit Codes¶
koality run reports the outcome through its exit code, so it can gate CI jobs and orchestrator tasks:
| Code | Meaning |
|---|---|
0 |
All checks passed. |
1 |
At least one check failed: its value is outside its thresholds, or its data is missing. |
2 |
At least one check errored (e.g. its query failed), the configuration or database setup is invalid, a table is missing, or data could not be loaded from the database_accessor database. |
130 |
The run was interrupted (e.g. Ctrl-C or SIGINT). |
Checks with monitor_only: true never fail a run because of their thresholds, but missing data still fails it
and errors still count. This matches CheckExecutor.check_failed and CheckExecutor.check_errored in Python.
Use --exit-zero to exit with 0 even if checks fail or error. Configuration, database setup, data loading
(e.g. a missing table) and result persistence (result_table) errors still exit with 2.
Override Configuration at Runtime¶
Use the --overwrites (-o) option to override configuration values without modifying the YAML file:
# Override a filter value (e.g., run for a specific date instead of "yesterday")
koality run --config_path checks.yaml -o defaults.filters.partition_date=2023-06-15
# Override multiple values
koality run --config_path checks.yaml -o defaults.filters.partition_date=2023-06-15 -o defaults.filters.shop_id=SHOP02
# Override other defaults
koality run --config_path checks.yaml -o defaults.identifier_format=column_name -o defaults.monitor_only=true
# Override filter fields (column, operator, etc.)
koality run --config_path checks.yaml -o defaults.filters.partition_date.column=OTHER_DATE_COL
# Override at bundle level (only affects that bundle)
koality run --config_path checks.yaml -o check_bundles.orders_checks.filters.partition_date=2023-06-15
# Override at check level (only affects specific check by index)
koality run --config_path checks.yaml -o check_bundles.orders_checks.0.table=orders_archive
Overwrites applied to global defaults propagate to all checks automatically.
4. Validate Configuration¶
Validate your configuration file without executing checks:
The configuration is loaded as koality run loads it, so pass the same -o overwrites and -dsv variables (or set
DATABASE_SETUP_VARIABLES); an undefined ${VAR_NAME} in database_setup is reported as an error.
With --connect, koality also runs database_setup and looks up every table the checks read, with each check,
filter, join and date column, without reading any data. All missing tables and columns are reported at once:
Error: Configuration 'checks.yaml' reads tables or columns that don't exist or can't be read:
- bundle 'orders', check 0 (NullRatioCheck): column 'amout' not found in table 'orders'
- bundle 'orders', check 2 (CountCheck): table 'returns' can't be read: Catalog Error: Table with name returns does not exist!
koality validate exits with 0 if the configuration is valid and 2 otherwise, like koality run for an invalid
configuration. For BigQuery, columns are looked up in the dataset's INFORMATION_SCHEMA.COLUMNS, so no table is
scanned.
5. Print Resolved Configuration¶
View the fully resolved configuration (after default propagation) in different formats:
# YAML format (default)
koality print --config_path checks.yaml
# JSON format
koality print --config_path checks.yaml --format json
# Pydantic model representation
koality print --config_path checks.yaml --format model
# Custom indentation
koality print --config_path checks.yaml --format yaml --indent 4
# Preview configuration with overwrites applied
koality print --config_path checks.yaml -o partition_date=2023-06-15
Using print with overwrites is useful for verifying your overrides are applied correctly before running checks.
6. Explain Checks and Their SQL¶
See what koality run would do, without reading any data: for each check its bundle, type, tables, thresholds,
filters (after defaults and -o overwrites), the query that checks its data exists, and the query that computes
its result. With a database_accessor, a run first fetches each source table's rows into an in-memory staging
catalog, once for all checks; those fetch queries follow the checks.
koality explain --config_path checks.yaml
# Only some checks, as JSON (e.g. for tooling)
koality explain --config_path checks.yaml --bundle orders --check-type NullRatioCheck --format json
# With the overwrites and variables of a run
koality explain --config_path checks.yaml -o partition_date=2024-01-01 -dsv PROJECT_ID=my-gcp-project
Backend: duckdb (not connected; --connect uses the attached database's)
Data: fetched from 'src' into the staging catalog koality_stage (a run adds a unique suffix), where the checks read it
Bundle 'orders', check 0: NullRatioCheck (amount_null_ratio)
Tables: orders
Column: amount
Passes if the result is <= 0.1
Filters:
partition_date: d = '2024-01-01' (type date)
...
Fetch queries (one per source table, for all checks of the run):
orders:
SELECT amount, d
FROM "src"."orders"
WHERE (CAST(d AS DATE) = DATE '2024-01-01')
Without --connect, no database is contacted and the SQL is written for DuckDB. With --connect, database_setup
runs as for koality run, the SQL is written for the attached database (e.g. BigQuery's own SQL and literals),
and each fetch query (or, without a database_accessor, each check's query) gets DuckDB's plan (EXPLAIN, which
doesn't run the query). There is no
plan for a source such as BigQuery, which would receive the query. koality explain exits with 0, or 2 for an
invalid configuration, an unknown --bundle, or (with --connect) a failing database_setup or a query that
can't be planned, e.g. because of a missing column; the explanation is still printed, with the error at that query.
Understanding Check Results¶
Each check returns a result dictionary with the following fields:
| Field | Description |
|---|---|
DATE |
The date the check was run for |
METRIC_NAME |
Name of the metric/check |
IDENTIFIER |
Identifier value (format depends on identifier_format setting) |
TABLE |
Table being checked |
COLUMN |
Column being checked |
VALUE |
Actual value measured |
LOWER_THRESHOLD |
Lower threshold for passing |
UPPER_THRESHOLD |
Upper threshold for passing |
RESULT |
SUCCESS, FAIL, MONITOR_ONLY, or ERROR |
Persisting Results¶
Set defaults.result_table to append each run's results, plus an INSERT_TIMESTAMP, to a table. The table is
created if it does not exist and is resolved like a check's table: inside the database_accessor database, or the
session's current database if there is no accessor.
database_setup: |
ATTACH 'warehouse.duckdb' AS warehouse;
database_accessor: warehouse
defaults:
result_table: dqm_results # -> warehouse.main.dqm_results
Outside BigQuery, VALUE, LOWER_THRESHOLD and UPPER_THRESHOLD are DOUBLE (open thresholds are ±inf, so
the database must support infinite doubles, as DuckDB and PostgreSQL do), DATE is a DATE, INSERT_TIMESTAMP is a
UTC TIMESTAMP, and the other fields are VARCHAR. The identifier column is named as in the result dictionary
(IDENTIFIER, or per identifier_format).
For BigQuery, result_table is passed to BigQuery as written (dataset.table or project.dataset.table) and the
attached database must not be READ_ONLY. The table uses the corresponding BigQuery types (FLOAT64, DATE,
TIMESTAMP, STRING).
koality run uses an in-memory DuckDB session. If result_table resolves to an in-memory database (e.g. no
database_accessor and no attached file), the results are lost when koality exits, and koality logs a warning.
Next Steps¶
- Learn about Configuration options
- Explore available Checks
- See the API Reference for detailed documentation