Skip to main content
Version: 1.33.0 (latest) View Markdown

Run transformations in jobs

A transformation is most useful when it runs by itself, right after the data that it reads arrived. On the dltHub platform you get this with a chain of jobs: one job loads the data, and one job runs each transformation. The scheduler gives every job run the interval that it must process, starts a transformation when the data before it is complete, holds it back when a check failed, and reloads the whole chain when you ask for a refresh.

This page builds such a chain for the costs of GitHub Actions. It has three jobs, and each one writes to a dataset of its own:

JobReadsWrites
load_github_actionsthe GitHub REST APIdataset github_actions: workflow runs, their jobs and steps, and a runner_pricing table
transform_actions_costsdataset github_actionsdataset github_actions_costs: the table costed_jobs with the cost of every job
transform_daily_costsdataset github_actions_costsdataset github_actions_reports: the table daily_costs with one row per day and runner system

Give each step a pipeline and a dataset​

You can keep raw data, facts and reports apart when each step of the chain has its own pipeline and its own dataset. Create the pipelines at the module level, so every job can use them:

import dlt

ingest_pipeline = dlt.pipeline(
"ingest_github_actions", destination="warehouse", dataset_name="github_actions"
)
costs_pipeline = dlt.pipeline(
"transform_actions_costs", destination="warehouse", dataset_name="github_actions_costs"
)
daily_pipeline = dlt.pipeline(
"transform_daily_costs", destination="warehouse", dataset_name="github_actions_reports"
)

Here all three pipelines use one named destination, warehouse. The three datasets are then at the same data location, and dlt runs each transformation as SQL inside the destination. A transformation reads the dataset of the pipeline before it: costs_pipeline runs a transformation on ingest_pipeline.dataset().

Make the transformations incremental​

Every job of the chain processes one scheduler interval in each job run. You can let all incrementals of all jobs take that interval with one setting in .dlt/config.toml:

[jobs]
incremental_mode = "interval"

The first transformation computes the cost of each job from the duration of its steps and the price of its runner:

from datetime import datetime
from typing import Any

import dlt


@dlt.hub.transformation(
name="costed_jobs",
write_disposition="append",
primary_key="id",
incremental=dlt.sources.incremental[datetime](
"completed_at", range_start="closed", range_end="open"
),
)
def costed_jobs(dataset: dlt.Dataset) -> Any:
yield dataset("""
WITH step_time AS (
SELECT _dlt_parent_id AS job_dlt_id, SUM(duration_sec) AS billable_sec
FROM workflow_jobs__steps
GROUP BY _dlt_parent_id
)
SELECT
j.id, j.run_id, j.name, j.conclusion, j.started_at, j.completed_at, p.os,
CAST(CEIL(st.billable_sec / 60.0) AS BIGINT) AS billed_minutes,
CAST(CEIL(st.billable_sec / 60.0) AS BIGINT) * p.rate_per_minute AS estimated_cost
FROM workflow_jobs AS j
JOIN step_time AS st ON j._dlt_id = st.job_dlt_id
JOIN workflow_jobs__labels AS l ON j._dlt_id = l._dlt_parent_id
JOIN runner_pricing AS p ON l.value = p.label
WHERE j.completed_at IS NOT NULL
""")

Here the cursor is declared on the decorator, and dlt filters the result on completed_at. The range is closed at its start and open at its end, like the interval itself. Two intervals next to each other never share a row, so append does not write a job twice.

The second transformation rolls the costs up to days. Its result has a day column, and its input has the completed_at timestamp, so it filters both:

@dlt.hub.transformation(
name="daily_costs",
write_disposition="merge",
primary_key=["day", "os"],
)
def daily_costs(
dataset: dlt.Dataset,
day: dlt.sources.incremental[datetime] = dlt.sources.incremental(
"day", on_cursor_value_missing="exclude"
),
) -> Any:
jobs = dataset.table("costed_jobs", incremental=day.with_cursor("completed_at")).to_ibis()
yield jobs.group_by(day=jobs.completed_at.date(), os=jobs.os).aggregate(
jobs=jobs.count(),
billed_minutes=jobs.billed_minutes.sum(),
estimated_cost=jobs.estimated_cost.sum(),
)

Here day.with_cursor("completed_at") filters the costed_jobs table to the interval, and dlt filters the result on day. With merge and the primary key day and os, a day that is aggregated again replaces its rows.

Connect the jobs with freshness checks​

You can make a transformation wait for complete data with a freshness check on the job before it. Give every job of the chain the same schedule and the same interval, and declare freshness on each transformation job:

from dlt.hub import run
from dlt.hub.run import TJobRunContext

from ingestion import github_actions_source

# start of the range of every job: a refresh run starts here again
EPOCH = "2026-11-01T00:00:00+00:00"
# every job has the same schedule, so their intervals have the same bounds
HOURLY = "0 * * * *"


@run.pipeline(ingest_pipeline, trigger=run.trigger.schedule(HOURLY), interval={"start": EPOCH})
def load_github_actions(run_context: TJobRunContext) -> None:
source = github_actions_source()
# the pricing table lands in the ingestion dataset, where costed_jobs joins it
source.resources.add(runner_pricing())
print(ingest_pipeline.run(source))


@run.pipeline(
costs_pipeline,
trigger=run.trigger.schedule(HOURLY),
interval={"start": EPOCH},
freshness=[load_github_actions.is_fresh],
)
def transform_actions_costs() -> None:
print(costs_pipeline.run(costed_jobs(ingest_pipeline.dataset())))


@run.pipeline(
daily_pipeline,
trigger=run.trigger.schedule(HOURLY),
interval={"start": EPOCH},
freshness=[transform_actions_costs.is_fresh],
)
def transform_daily_costs(rerun_lookback_days: int = 3) -> None:
dlt.current.interval.apply_full_days().apply_lag("0 0 * * *", count=rerun_lookback_days)
print(daily_pipeline.run(daily_costs(costs_pipeline.dataset())))

Here github_actions_source is a source that loads the workflow runs of a repository and their jobs with an incremental on completed_at. It takes its range from the interval like the transformations do. Each transformation job declares freshness on the job directly before it, so the rollup waits for the cost table and the cost table waits for the ingestion.

When a job starts​

A job is fresh when its latest job run completed and covered the last interval that its schedule closed. The scheduler uses this in two places.

At every tick of the schedule, the scheduler starts the ingestion job. The transformation jobs have the same tick, but the job before them is not fresh at that moment, so their start is skipped and no job run is recorded. When the ingestion job completes its job run, it becomes fresh, and the scheduler starts transform_actions_costs at once. It does not wait for the next tick. When that job completes, the scheduler starts transform_daily_costs. A job that is paused, or that has no interval left to process, is not started.

When a job fails, it is not fresh, and the jobs after it do not run. Nothing is lost: the next job run of the failed job gets the failed interval again, together with the intervals that closed in the meantime. When it completes, each job after it gets one job run that covers the whole range that it missed. You fix the job that failed, and the rest of the chain catches up by itself.

The first job run of every job gets one interval from interval.start to the last tick, and a refresh signal.

Widen the interval of an aggregation​

A job with an hourly schedule gets an interval of one hour, but daily_costs must aggregate whole days. You can change the interval of a job run before the transformation uses it:

dlt.current.interval.apply_full_days().apply_lag("0 0 * * *", count=rerun_lookback_days)

Here apply_full_days() moves the start and the end of the interval to day boundaries. apply_lag() then moves the start back by three ticks of a daily schedule, so the last three days are aggregated again in every job run. This picks up jobs that completed late.

Stop the chain when a check fails​

You can keep bad data away from the next step when you declare data quality checks on a transformation and run them inside the job, right after the pipeline run:

from dlt.hub import data_quality as dq


@dq.with_checks(
dq.checks.is_primary_key("id"),
dq.checks.case("estimated_cost >= 0", name="cost_not_negative"),
)
@dlt.hub.transformation(
name="costed_jobs",
write_disposition="append",
primary_key="id",
incremental=dlt.sources.incremental[datetime](
"completed_at", range_start="closed", range_end="open"
),
)
def costed_jobs(dataset: dlt.Dataset) -> Any:
... # the query from above


def transform_actions_costs() -> None:
print(costs_pipeline.run(costed_jobs(ingest_pipeline.dataset())))
print(dq.run(costs_pipeline, raise_on_failed_checks=True))

Here costed_jobs gets two checks: id has no nulls and no duplicates, and no job has a negative cost. The checks are stored in the schema of costs_pipeline. dq.run finds them there, computes them on the github_actions_costs dataset and stores the results in it. When all checks pass, the job prints the report:

Data quality results of 2 check(s) in dataset github_actions_costs, 0 failed
[pass] costed_jobs.cost_not_negative: 0 of 48 rows failed
[pass] costed_jobs.is_primary_key__id: 0 of 48 rows failed

With raise_on_failed_checks=True, dq.run raises DataQualityChecksFailed when a check fails, and this fails the job run of transform_actions_costs. The job is then not fresh, and transform_daily_costs does not start until a later job run of transform_actions_costs completes. The results of the checks are stored before the exception is raised. The rows that the failed job run loaded stay in the dataset as well: the check stops the chain, it does not roll the load back.

You can also pass a check at run time, for example one that belongs to this job only:

dq.run(
costs_pipeline,
checks={
"costed_jobs": [dq.checks.case("billed_minutes <= 360", name="job_within_time_limit")],
},
raise_on_failed_checks=True,
)

Here the check runs together with the two declared ones, and any of the three fails the job run. A check that you pass at run time is not stored in the schema.

Reload the pipelines on a refresh​

A refresh asks a job to load its data again from the start. By itself, the refresh signal only sets run_context["refresh"]. You can let it reload every pipeline that a job runs with one more setting in .dlt/config.toml:

[jobs]
incremental_mode = "interval"
auto_refresh_pipeline_mode = "drop_sources"

Here every pipeline of every job gets the refresh mode drop_sources, but only in a job run that carries the refresh signal. The mode drops the tables and the state of the sources that the job run loads. You can use drop_resources or drop_data in the same way, and you can set the mode for a single job in the section [jobs.<module>.<job>].

Backfill the chain​

You can load the whole history again with a refresh of the first job:

dlthub run load_github_actions --refresh

The refresh cascade follows the freshness checks. The job run of load_github_actions gets the refresh signal and one interval from EPOCH to the last tick. When it completes, the scheduler starts transform_actions_costs with the refresh signal and the same full interval, and after that transform_daily_costs. Every job drops its tables and loads its full range once. To reload only a part of the chain, refresh the job where the reload must start.

A full interval from EPOCH can be large. On your machine, you can make it shorter in the ingestion job:

def load_github_actions(run_context: TJobRunContext, epoch: str = None) -> None:
if run_context["refresh"] and epoch:
dlt.current.interval.update(start=epoch)
source = github_actions_source()
source.resources.add(runner_pricing())
print(ingest_pipeline.run(source))

Here epoch is a job argument that you set in the configuration of your dev profile, or on the command line with -c epoch=2026-11-28T00:00:00Z. Without it the job uses the interval that it got.

Test the chain on your machine​

You can run every job of the chain on your machine before you deploy it. A local run starts one job with the interval that you pass. It does not check freshness, and it does not start the next job, so you run the jobs in the order of the chain:

dlthub local run load_github_actions --start 2026-11-01T00:00:00Z --end 2026-11-02T00:00:00Z
dlthub local run transform_actions_costs --start 2026-11-01T00:00:00Z --end 2026-11-02T00:00:00Z
dlthub local run transform_daily_costs --start 2026-11-01T00:00:00Z --end 2026-11-02T00:00:00Z

Run the next interval in the same way, and compare the row counts of the three datasets after each step. A transformation with append appends the same rows again when you run the same interval twice. In this example the primary key check on costed_jobs then fails the local run:

dlthub.data_quality._info.DataQualityChecksFailed: 1 data quality check(s) failed on dataset `github_actions_costs`:
[fail] costed_jobs.is_primary_key__id: 48 of 96 rows failed

To start again, run each job with --refresh. Each job then drops its tables and loads its full range.

This demo works on codespaces. Codespaces is a development environment available for free to anyone with a Github account. You'll be asked to fork the demo repository and from there the README guides you with further steps.
The demo uses the Continue VSCode extension.

Off to codespaces!

DHelp

Ask a question

Welcome to "Codex Central", your next-gen help center, driven by OpenAI's GPT-4 model. It's more than just a forum or a FAQ hub – it's a dynamic knowledge base where coders can find AI-assisted solutions to their pressing problems. With GPT-4's powerful comprehension and predictive abilities, Codex Central provides instantaneous issue resolution, insightful debugging, and personalized guidance. Get your code running smoothly with the unparalleled support at Codex Central - coding help reimagined with AI prowess.