Thinking in Pipelines: Why Great Work Comes From Rewriting the Same Rule at Different Scales
Hatched by Kai Nguyen
May 16, 2026
10 min read
3 views
74%
The hidden question behind both tools
What if the real skill in data work is not writing more code, but deciding where the transformation belongs?
That question sits underneath both a simple functional pattern and a powerful SQL pattern. One lets you apply a function across items in an iterable. The other lets you define a dataset, then compute over grouped or ordered slices of it. At first glance, they seem like separate techniques from different worlds. In practice, they are both answers to the same problem: how do you reshape information without losing your grip on it?
Most people think the challenge is execution. It is not. The deeper challenge is placement. Should the change happen item by item, or after the data has been partitioned? Should you transform first and then compare, or compare first and then transform? Every serious analysis, every pipeline, every clean abstraction is a choice about the order in which meaning is made.
Good code is often not about what computation happens, but about when and at what granularity it happens.
That is why these ideas belong together. They both teach the same mental move: stop thinking of data as a static thing, and start seeing it as a sequence of transformations operating at different scales.
The three scales of thinking: item, group, rank
A useful way to understand this connection is through three scales of computation.
Item scale is the level of a single element. A transformation here says, “Take each thing and change it in the same way.” In Python, that is the logic of applying a function across an iterable. If you have numbers and want their squares, or strings and want them uppercased, you do not manually inspect each element. You define the rule once, then apply it repeatedly.
Group scale appears when the question is no longer “What is each item?” but “What does each item mean relative to others like it?” In SQL, a partition creates these bounded neighborhoods. Once the data is grouped, aggregate logic can describe the whole group instead of the individual rows. That is the difference between asking for each employee and asking for the average salary in each department.
Relative scale is what window functions make possible. They do not collapse the group into one answer. Instead, they let each row keep its identity while gaining context. A ranking function, for example, can tell you where a row stands inside its partition. This is subtle and important. The row is not destroyed by aggregation, but it is also not treated as isolated.
These three scales map to three different kinds of understanding:
- Item transformation: change each object in the same way.
- Group summarization: describe the collection as a whole.
- Relative positioning: interpret each object through its context.
The power of both Python iteration and SQL analytics is that they let you move fluidly between these scales. That movement is where insight lives.
Why “just loop over it” is the wrong mental model
A loop sounds harmless. It even sounds transparent. But a loop can hide a very important design decision: are you manipulating the data, or are you describing a transformation?
When you write an explicit loop, your attention gets pulled toward procedure. First this item, then that item, then a change, then another change. This can be useful, but it often makes the code harder to reason about because the mechanics of iteration become entangled with the business logic. A transformation function does the opposite. It says the rule matters more than the path.
That same distinction exists in SQL. You can think row by row, or you can think in terms of datasets, partitions, and windows. The more you rely on row-by-row reasoning, the more your logic becomes brittle and opaque. The more you elevate your thinking to groups and windows, the more the database can do the heavy lifting while you preserve the shape of the problem.
This is not just an efficiency issue. It is an epistemic issue, meaning it affects how you know what you know. A loop encourages local reasoning, one item at a time. A transformation pipeline encourages structural reasoning, where you can see the pattern before you touch the details.
Consider a practical example. Suppose you want to normalize scores within each department, then identify the top performers. If you think only in loops, you might sort, store temporary state, compare manually, and keep track of context yourself. If you think in transformations and windows, the logic becomes clearer: first partition by department, then compute rank, then filter. The code becomes a map of intent rather than a transcript of actions.
The same principle applies in Python. If you need to convert a list of temperatures from Celsius to Fahrenheit, a transformation function is the right abstraction. But if you need to annotate each temperature with its deviation from the average of its city, now you are in group and context territory. The question changed, and so should the scale of your thinking.
The real divide is not Python versus SQL, it is local versus relational thinking
The surprising connection between these tools is that they train a person to think relationally. Not relational in the narrow database sense, but relational in the broader sense of asking: what is this thing relative to?
A value in isolation is often not very informative. Ten, for example, means little by itself. Ten compared with a mean, or ten ranked among peers, or ten after a transformation, becomes meaningful. Likewise, a single element in a Python iterable can be trivial until you define the function that gives it shape.
This is why the most useful analytic habit is to stop seeing transformation as decoration. Transformation is interpretation. When you apply a function to data, you are asserting a theory about what matters.
Here is a simple mental model:
- Map answers: What should every item become?
- Partition answers: What belongs together?
- Window answers: What does each item look like inside its group?
These are not just technical operations. They are three ways of making meaning.
Imagine a music platform analyzing listening data. At the item level, each song has a duration, a genre, and a play count. At the group level, you might ask about the average skip rate per genre. At the window level, you might rank songs within each genre by growth in plays over the last week. Each question requires a different granularity of thought. If you use the wrong one, you get answers that are either too vague or too granular to matter.
The mistake is not choosing the wrong function. The mistake is choosing the wrong level of explanation.
That is why experienced practitioners often look “simpler” than beginners. They are not doing less. They are operating at the right scale earlier.
Composition is the superpower
The deepest common lesson here is that transformations are most powerful when they are composable.
A transformation becomes composable when it does one thing cleanly and hands off to the next stage. In Python, a function that transforms items in an iterable can be chained with filters, reducers, or later enrichment steps. In SQL, a CTE creates a named intermediate result that can be reused downstream, and a window function can attach contextual information without destroying the original rows.
This composability matters because real work rarely ends after one pass. You often need to clean data, enrich it, group it, compare it, rank it, and then perhaps transform it again. A good pipeline avoids rethinking the whole problem every time. It creates intermediate truths that are local, explicit, and reusable.
Think about editing a paragraph. You might first correct grammar, then improve sentence rhythm, then assess structure, then tighten repetition. You do not ask the same question at each stage. You build on the output of the previous stage. Good analytical work works the same way.
There is a reason CTEs are so valuable in SQL. They are not just a convenience. They are a way of making a complex argument legible. Each named step says, “This is the state of the data after one meaningful transformation.” That is intellectually powerful because it externalizes reasoning. You are no longer asking your reader, or your future self, to reconstruct the whole logic in one breath.
The same principle is at work when a function is passed into a mapping operation. The function becomes a portable rule. The iterable becomes the material on which that rule acts. The two fit together because they separate what should happen from where it should happen.
A practical framework for choosing the right transformation
Whenever you are working with data, ask four questions in order.
1. What is the unit of meaning?
Is the important object a single value, a row, a user, a department, or a time period? The unit of meaning tells you whether you should think item by item or in groups.
2. Do I need to preserve individuality?
If you need each item to remain visible after computation, you are likely in window function territory. If not, aggregation may be enough. If you only need to reshape each item independently, mapping is the right mental model.
3. Does context matter more than content?
A score of 90 is only interesting if you know the baseline. A purchase is only meaningful if you know the cohort. When context matters more than raw content, partitioning or ranking becomes essential.
4. Can I express the rule once and reuse it?
If yes, you probably want a function, a CTE, or a composable step rather than a hand-coded one-off process. Reusability is not only about elegance. It reduces cognitive load and makes errors easier to detect.
This framework helps you decide whether you are solving an item problem, a group problem, or a relationship problem. That decision often matters more than the syntax you use to implement it.
The habit that separates tidy scripts from thoughtful systems
The most effective analysts and developers are not merely efficient. They are disciplined about abstraction. They avoid turning every problem into a procedural maze because they know that procedure is cheap while clarity is expensive.
A tidy script says, “Here is the rule, here is the context, here is the result.” A thoughtful system says, “Each stage earns its place, and each stage makes the next one easier.” This is true in Python functions that transform iterables, and it is true in SQL queries that layer CTEs, partitions, and window functions.
This habit scales beyond code. In writing, it helps you decide whether to explain a concept through examples, categories, or comparisons. In management, it helps you know whether to inspect individual performance, team-level output, or relative contribution within a group. In decision-making, it helps you distinguish between absolute value and contextual value.
The deeper benefit is that it trains you to respect structure. You stop asking only “What is the answer?” and start asking “What is the right structure for the answer?” That shift produces cleaner code, sharper analysis, and better judgment.
Key Takeaways
- Think in scales, not just steps. Ask whether your problem is about items, groups, or relative position.
- Use transformation to clarify intent. A function or CTE should make the rule visible, not bury it inside procedure.
- Preserve context when context matters. Window logic is powerful because it keeps each row visible while adding meaning from the surrounding data.
- Choose the right level of abstraction early. The earlier you align your method with the granularity of the question, the cleaner the result will be.
- Treat composability as a design goal. Build pipelines where each stage has a clear purpose and creates a reusable intermediate state.
Conclusion: the best analysis changes shape without losing sight of the thing
The real lesson connecting these ideas is not about syntax, and not even about efficiency. It is about intellectual control. Great work happens when you can change the form of data without losing the meaning of the data.
That is what transformation functions do at the item level. That is what CTEs and window functions do at the group level and beyond. They help you move from raw material to structured insight without flattening everything into a single undifferentiated mess.
If you remember only one thing, remember this: clarity comes from knowing the scale at which a truth becomes visible. Some truths appear only when you look at each item. Some emerge only when you see the group. Some exist only in comparison.
Once you start thinking this way, coding and querying stop feeling like separate crafts. They become versions of the same discipline: deciding how to make meaning from data, one transformation at a time.
Sources
Hatch New Ideas with Glasp AI 🐣
Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)
Start Hatching 🐣