Taking action and asking ourselves what we can do if dbt goes paywalled, gets discontinued or the open source alternatives aren't as good.

By Alejandro Aboy

Published: 2025-10-27

URL: https://thepipeandtheline.substack.com/p/behind-dbts-magic-building-a-orchestrator


Hi there! Alejandro here 😊

Suscribe if you like to read about technical data & AI learnings, deep dives!

Enjoy the reading and let me know in the comments what you think about it 👨🏻‍💻

Subscribe now

📝 TL;DR

In simpler words, you run dbt run, you’re just triggering this process: parse → build graph → sort → execute.

The entire core experiment fits in around 500 lines of Python, its not production ready but you can get away with it running portable setups for quick DuckDB analysis that later you can wire up to Streamlit or even more complex BI tools.

-

Regex extraction finds all {{ ref() }} calls using a simple pattern match

-

Dependency graphs map which models depend on which using dictionaries and in_degree counters

-

Topological sort (Kahn’s algorithm) is like planning a dinner party—process tasks with no blockers first, then work through dependencies

-

Execution runs CREATE TABLE/VIEW statements in the calculated order

-

The entire core fits in around 500 lines of readable Python—no magic, just smart engineering


🔥 The Data Drama Is On Fire

AI is hype, we know it, but what I love about Data world is the level of gossip and niche drama we have, it’s just amazing and so tailored to our ecosystem.

Fivetran and dbt “married” into a one and only after other moves that got attention in the market.

The timeline:

-

January 2025: dbt Labs acquires SDF Labs

-

September 2025: Fivetran acquires SQLMesh

-

October 2025: Fivetran merges with dbt Labs

When one company controls your entire transformation layer, knowing how to rebuild these tools yourself starts sounding more realistic than paranoid.

Understanding how these tools work means you’re not locked into anyone’s ecosystem.

Let’s go behind the scenes and see how tools like dbt work, and how to do a basic one from scratch.


🤔 The Core Problem: SQL Files Don’t Know About Each Other

Let’s start with some questions:

-

How does dbt figure out that campaignperformance needs intcampaigns which needs stg_campaigns?

-

How does it know to run them in that exact order?

SQL files sitting in folders don’t magically understand dependencies. You need to:

-

Extract which models reference which other models

-

Build a graph of those relationships

-

Determine the correct execution order

-

Run the SQL in that order

I created this Claude Artifact based on the article’s content and my research to showcase it visually:


🀄domino-sql: Simple SQL Orchestrator with DuckDB

I created this Github repo with a working project for you to try.

Its using generated marketing data from Synthetic Datagen and supports TABLEs and VIEWs with a simple config.

🔗 Step 1: Finding Dependencies

When you write {{ ref(’stgcampaigns’) }} in your SQL, dbt needs to extract “stgcampaigns” as a dependency. This is pure pattern matching.

dbt uses Jinja templating, I tried to see what it looks like to use pattern matching with the so hated regex.

Here’s the actual regex from parser.py:

Three lines of Python to find every ref() call in your SQL file.

Once extracted, these refs get stored on the model object.

Now dbt knows: “This model depends on these other models.”

The compilation step is equally find & replace {{ ref(’stg_campaigns’) }} with the actual table name:

Thanks for reading The Pipe & The Line! This post is public so feel free to share it.

Share


📊 Step 2: Building the Dependency Graph

Once you’ve parsed all the models and extracted their refs, you need to organize this information.

A dependency graph is just a dictionary that maps each model to the list of models that depend on it.

Here’s how graph.py builds it:

Think of this as building a map of “who needs to run before whom.”

If campaignperformance references intcampaigns, then int_campaigns must run first.

The graph stores this as:

int_campaigns -> campaign_performance.

The in_degree counter tracks how many dependencies each model has:

-

Models with zero dependencies can run immediately.

-

Models with dependencies must wait.

This structure is called a Directed Acyclic Graph (DAG).

-

Directed because relationships flow one way.

-

Acyclic because there are no circular loops, you can’t have Model A depending on Model B depending on Model A.

Anything triggering a circular dependency will cause the process to fail.


🔄 Step 3: Decide Execution Order (Kahn’s Algorithm)

Now for the smart part: determining execution order.

Some tasks have dependencies, others don’t. You need to figure out a valid order to do everything.

That’s exactly what topological sorting does for your SQL models.

It finds any valid ordering where all dependencies are satisfied.

These are the steps:

-

Start with all models that have zero dependencies.

-

Run one of those models and remove it from the graph.

-

For every model that depended on it, reduce their dependency count by one.

-

If any model now has zero dependencies, add it to the queue.

-

Repeat until everything is processed.

Here’s the actual implementation from graph.py:

This produces something like:

[’stg_campaigns’, ‘stg_ads’, ‘int_campaigns’, ‘int_ads’, ‘campaign_performance’].

If you finish and there are still models left, you either got a circular dependency going on or some other magic.


The Pipe & The Line is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.


⚡ Step 4: Execute in Order

Once you have the execution order, the rest is straightforward.

The usual SQL commands you are used to see, but in the right sequence.

Loop through the list and run each model:

That’s it! You can build a u$600M company (joking - or not)

🏁 Quick Demo

This is how it looks when you run the example project of domino-sql

Then you can open the duckdb database that received these materializations and run some queries:

duckdb marketing.duckdb


If you enjoyed the content, hit the like ❤️ button, share, comment, repost, and all those nice things people do when like stuff these days. Glad to know you made it to this part!


Hi, I am Alejandro Aboy. I am currently working as a Data Engineer. I started in digital marketing at 19. I gained experience in website tracking, advertising, and analytics. I also founded my agency. In 2021, I found my passion for data engineering. So, I shifted my career focus, despite lacking a CS degree. I’m now pursuing this path, leveraging my diverse experience and willingness to learn.

Follow me on Linkedin