Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:173TECH Sayn DAG Construction

From Leeroopedia
Knowledge Sources
Domains Graph_Theory, Data_Engineering
Last Updated 2026-10-17 00:00 GMT

Overview

Technique for automatically constructing a task execution graph by inferring dependencies from source-output table mappings, then validating and topologically sorting the graph for ordered execution.

Description

DAG (Directed Acyclic Graph) construction is the practice of deriving task execution order from the data dependencies between tasks. Instead of requiring users to manually specify every dependency, the DAG is inferred from the relationship between what each task reads (sources via src()) and what each task produces (outputs via out()). This solves the problem of maintaining explicit dependency lists that become stale or inconsistent as the project grows.

Key concepts:

  • Implicit dependency inference: If task B reads a table that task A produces, then A is a parent of B
  • Explicit dependencies: Users can also specify parents manually as a fallback
  • Cycle detection: The DAG is validated for cycles before execution
  • Topological sort: Tasks are ordered so that all parents of a task appear before it
  • Task query resolution: CLI -t/-x flags filter tasks with upstream/downstream expansion

Usage

Use this principle when building a data pipeline system where tasks have data dependencies. It is the right choice when you want dependencies to be inferred automatically from data flow rather than maintained manually.

Theoretical Basis

The DAG construction algorithm operates in four phases:

  1. Collection: For each task, collect declared sources and outputs as DbObject instances
  2. Inference: Build a mapping output → [task_names]. For each task's sources, find tasks that produce those outputs. If found, they become parents.
  3. Validation: Check for missing parents (warn for tables not produced by any task) and cycles (error on cyclic dependencies)
  4. Topological Sort: Order tasks so every parent appears before its children

The topological sort algorithm iterates through pending tasks, adding a task to the sorted list when all its parents have been sorted. If the graph has a cycle, the sort fails.

Pseudo-code for inference:

# Abstract algorithm (NOT real implementation)
output_to_task = {output: [tasks_producing_it]}
for task in all_tasks:
    for source in task.sources:
        if source in output_to_task:
            for parent_task in output_to_task[source]:
                task.parents.add(parent_task)

Task query resolution extends the DAG with include/exclude operations:

  • Include -t task_name: adds task and optionally its upstream/downstream
  • Exclude -x task_name: removes task and optionally its upstream/downstream
  • group:name and tag:name syntax for group/tag-based selection

Practical Guide

Dependencies are inferred automatically:

-- File: sql/model_users.sql
-- Uses src() to reference source tables
SELECT * FROM {{ src('raw.users') }}
-- File: sql/model_orders.sql
-- Reads from model_users, so model_users is a parent
SELECT * FROM {{ src('analytics.users') }}
JOIN {{ src('raw.orders') }}

The DAG is: model_users → model_orders (inferred, no manual parents needed).

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment