Declarative vs Imperative Pipeline
Concepts covered: DAG Orchestration
Pipeline-as-code comes in 2 models. Imperative code lists the steps: fetch a page, loop until the cursor is empty, delete a partition, insert the rows. Declarative code describes the end state: this table equals this query, depends on those tables, and is fresh by 06:00. An engine compares the description with what exists and works out the steps.
2 workloads, 2 models
asset: analytics.daily_revenue
owner: finance-data
depends_on:
- curated.orders
- curated.refunds
materialization: incremental
partition_by: order_date
lookback_days: 3
freshness_sla: "06:00 UTC"
query: sql/daily_revenue.sqldef ingest_invoices(client, warehouse, run_date):
rows = []
cursor = None
while True:
page = client.get_invoices(updated_on=run_date, cursor=cursor)
rows.extend(page["items"])
cursor = page.get("next_cursor")
if cursor is None:
break
warehouse.delete_partition("raw.invoices", run_date)
warehouse.insert("raw.invoices", rows)The YAML never says what order to build in or which partitions to rewrite. The engine reads the order from depends_on, rewrites the last 3 partitions because of lookback_days, and can print a plan before it runs. The Python owns each of those decisions: the page loop, the stop condition, and delete_partition before insert so a rerun replaces the partition instead of doubling it.
Trade-offs of each model
- Declarative
- Plan before apply: the engine lists what will rebuild
- Drift detection when prod stops matching the code
- Build order inferred from references
- Reruns replace by default
- Control flow the format cannot express needs an escape hatch
- Imperative
- Any control flow: loops, retries, branches on runtime data
- Fits APIs, pagination, per-tenant fan-out, model training
- Debuggable line by line in a general-purpose language
- Idempotency, ordering and cleanup are the author's job
- No plan: the only preview is a run
| Workload | Fits | Why |
|---|---|---|
| Set-based SQL transforms in a curated layer | Declarative | Output is a function of inputs, so the engine can plan and order it |
| Ingesting a paginated API | Imperative | The stop condition depends on each response |
| Per-tenant jobs over a tenant list that changes daily | Either, with dynamic partitions on the declarative side | Fan-out depends on runtime data |
| Buckets, permissions, schedules | Declarative | Desired state is stable and drift matters |
| Training a model with early stopping | Imperative | Branches on intermediate results |
| A shared layer where 200 analysts add tables | Declarative | Reviewers read a diff of end states |
An ingestion node pulls from a partner API that returns 500 records per page with a next-page cursor, and it must stop when the cursor is empty. Which model fits the node?
- Declarative, because the engine infers page order from depends_on.
- Imperative, because the stop condition depends on each response at run time.
- Declarative, because drift detection catches pages missing after the run.
- Imperative, because declarative engines cannot write to the raw layer.
Many teams settle on a hybrid: a declarative graph of assets in which a few nodes are imperative functions. The graph keeps planning, lineage and ordering for every node, and the function handles the API loop. The failure on either side is forcing the wrong model: 400 lines of templated YAML conditionals to express a loop, or a script that builds 60 tables in a hand-kept order and breaks when someone adds the 61st.
A team builds 140 curated tables with a Python script that runs CREATE TABLE statements in a hand-kept order. Last month a new table was placed before its parent and read yesterday's data. Which change prevents this class of bug?
- Add a CI step that diffs the script against the previous release.
- Retry the new table once after the rest of the script finishes.
- Split the script into 1 script per table and run them in parallel.
- Declare each table with its query and let the engine infer order.
About this lesson
This section is part of the Pipeline Operations: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.