You Could Have Come Up With Dremel and Parquet's Repetition and Definition Levels
Earlier in the year (August or so), I gave some 101 level talks on columnar storage, query engines, Parquet, Iceberg etc to colleagues within my team at work. The audience consisted of relatively new and junior engineers that we wanted to help onboard quickly into some of the more complex parts of the technology stack. I threw together some notes on short notice and informally shared the mental models and intuition I’ve developed over the years working on the data warehouse / lakehouse stack.
I’m highly inspired by the teaching of Prof. Erik Demaine of MIT and Prof. Michael Bender of Stony Brook University. They not only understand their subjects really well, but are also passionate about teaching, and from years of teaching the same topics, they’ve found new ways of thinking about old problems, and in doing so often found entirely new solutions. When you attend their lectures (which I did almost 15 years ago at this point), you feel the infectious energy of discovering the elegance of ideas, and whenever I deliver a technical talk, I try to create that feeling in the audience. If you haven’t watched Prof. Erik Demaine’s lectures on advanced algorithms (available on YouTube), I highly recommend them.
One of my favorite examples is The LCA Problem Revisited by Bender and Farach-Colton. I was thinking about lowest common ancestors in connection with a work project, while also working on some query optimization problems related to columnar storage, and my brain somehow made this connection. There is nothing really new here, and this post only borrows the idea of an LCA, not the clever parts of that paper. It’s just an intuitive way of thinking about repetition and definition levels, and I hope it helps anyone who wants to understand this topic quickly. With that introduction, let’s proceed!
Suppose we wanted to store a collection of nested records in columns, so that a query could read only the columns/fields it needs without having to read every record in its entirety. This is the basic problem that Google’s Dremel system addressed, and the encoding scheme that flattens arbitrarily nested records into columns found its way into Apache Parquet.
But once we separate the values into columns, we lose the structure that tells us which values belong together. Dremel solved this by associating two small integers with each entry in a column, called the repetition level and definition level.
When you read the paper, these names can make the scheme appear like some sort of magic, and more complicated than it really is. We can arrive at the same two integers by thinking about an ordinary tree, its lowest common ancestors, and the information that a stack would need to reconstruct that tree.
Candy Box Sales
Imagine we’re recording sales of candy boxes. Each sale contains several boxes, each box contains several candies, and each candy may or may not have a name.
Here are two sales:
[
{
"SaleId": "A",
"Boxes": [
{
"Candies": [
{ "Name": "Twix" },
{ "Name": "Mars" }
]
},
{
"Candies": [
{}
]
},
{
"Candies": []
}
]
},
{
"SaleId": "B",
"Boxes": []
}
]
In Sale A, the first box contains two candies, Twix and Mars. The second contains a candy whose name is missing, while the third box is empty. Sale B has no boxes at all.
An empty box is different from a box containing an unnamed candy, and a sale without any boxes is different from both.
Suppose a query asks which candy names are the most popular. In a columnar storage format, we’d like to read just the Name column, whose actual values are:
Twix, Mars
We’ve reduced the amount of data to read, but we’ve also lost most of the information needed to reconstruct the original records. We can no longer tell that Twix and Mars were in the same box, that another box contained a candy without a name, or that Sale B contained no boxes.
So what else do we need to store next to the values, so we can get the structure back?
The Path
Let’s think of the original records themselves as trees! Each record is a tree and an instance of a pre-defined schema (in Dremel it’s the protobuf schema, and in Parquet, it’s a schema in Thrift).
Each sale is a root, its boxes are children, the candies are children of the boxes, and each candy has an optional Name field.
For the Name column, every value follows the same schema path:
Sale Depth 0
|
+-- Box Depth 1 repeated
|
+-- Candy Depth 2 repeated
|
+-- Name Depth 3 optional
In this diagram, depth is measured from the sale record. The sale itself has depth 0, and a fully populated Name reaches depth 3.
There are two things we need to know about each entry as we walk through the records.
Depth: How far along this path did the record actually go?
If a candy has a name, we reach depth 3. If the candy exists but its name is absent, we reach depth 2. If a box exists but contains no candies, we stop at depth 1. And if the sale contains no boxes, we stop at depth 0.
Ancestor: How much of the path does the current entry share with the previous entry?
This is where the lowest common ancestor (LCA) idea comes in. When going through a sequence of leaves in a tree, we don’t need to rebuild the entire path from the root for every leaf. We can just retain the part that the new leaf shares with the previous one, discard the part that is no longer relevant, and build the remaining path. So, in a single forward scan, each entry can be completely rebuilt from the previous one, because we encode the “delta” of how the current entry differs from the previous entry while being aware of the overall schema.
Ok, so now let’s work this out with our candy example: the ancestor is the number of nodes on the path that the current entry shares with the previous one (it is one more than the depth of their LCA, and 0 when we start a new sale). For example, Twix and Mars share the sale and the box, so the ancestor for Mars is 2, even though their LCA (the box) is at depth 1.
So, the possible ancestor levels are:
- 0: Start a new sale.
- 1: Keep the sale and start a new box.
- 2: Keep the sale and box, and start a new candy.
There’s no ancestor level of 3, since Name is optional and can’t have several children. Only repeated fields can branch, so only they need an ancestor level.
Flattening Nested Records
Let’s walk through the sales in order and record both numbers for each entry.
| Name value | Depth | Ancestor | Explanation |
|---|---|---|---|
| Twix | 3 | 0 | First candy in a new sale |
| Mars | 3 | 2 | Same box, another candy |
| NULL | 2 | 1 | New box, candy exists without a name |
| NULL | 1 | 1 | Another box, but its candy list is empty |
| NULL | 0 | 0 | New sale, no boxes |
If we stored only the values, the three NULLs would look identical. Their depths are what separate an unnamed candy from an empty box from a sale with no boxes.
We also need the ancestor number. Without it, Twix and Mars could just as easily belong to different boxes, or even different sales, since both have depth 3. The ancestor tells us whether to continue within the current box or begin a different branch.
So is this enough to get the original records back? YES IT IS!
Reconstructing the Records Using a Stack
I think this was the most interesting part of the talk, where at least one person in the audience told me afterwards that they read the original paper and my explanation made it easy for them to understand what was going on.
Imagine a reader that goes through these entries one at a time and keeps a stack of the current sale, box, and candy. The reader only needs two operations on this stack:
- Keep(ancestor): Keep the bottom ancestor levels of the stack and pop everything deeper. This is the part of the path the current entry shares with the previous one.
- Build(depth): Create new nodes from that point down to depth, pushing each one onto the stack, and set the value if we reach
Name. This is the part of the path that is new.
In other words, Keep stuff, then Build new stuff:
for each (value, depth, ancestor):
Keep(ancestor)
Build(depth, value)
1. Twix (depth 3, ancestor 0)
- Keep(0): There is no previous record / structure, so stack stays empty.
- Build(3): Create a new sale, a box inside it, and a candy inside the box. We’ve reached depth 3, so assign the name “Twix”.
Stack: [Sale A, Box 1, Candy "Twix"]
2. Mars (depth 3, ancestor 2)
- Keep(2): We’re still in the same sale and the same box. Keep those two and pop the Twix candy.
- Build(3): Create another candy in Box 1 and assign it the name “Mars”.
Stack: [Sale A, Box 1, Candy "Mars"]
We now have our first box with two candies.
3. NULL (depth 2, ancestor 1)
- Keep(1): Keep the sale, pop Box 1 and its candy.
- Build(2): Create a new box and a candy inside it. Since the depth stops at 2, we don’t assign a name to this candy.
Stack: [Sale A, Box 2, Candy (no name)]
This recovers the second box, which contains an unnamed candy.
4. NULL (depth 1, ancestor 1)
- Keep(1): Keep the sale, pop Box 2 and its candy.
- Build(1): Create another box and stop at the box itself, without creating any candies.
Stack: [Sale A, Box 3]
So the third box has an empty Candies list.
5. NULL (depth 0, ancestor 0)
- Keep(0): Pop everything. Sale A is complete.
- Build(0): Create a new sale, and stop there. There are no boxes to create.
Stack: [Sale B]
And that’s both sales rebuilt, empty lists and missing names included. We don’t get SaleId back, since it lives in a different column, but we’ve got the whole nested structure along the Name path.
These Are Dremel’s Levels!
What we’ve called depth and ancestor are our two numbers!
The depth is the input argument for Build(), and is the definition level.
It tells Build how far down the path to go, and counts the optional and repeated nodes along the schema path that have been reached. Required nodes don’t have to be counted, since they can’t be missing. In our example, Box and Candy are repeated, while Name is optional, so our depths 0 through 3 already correspond to definition levels.
The ancestor is the input argument for Keep(), and the repetition level. It tells Keep how much of the previous path to retain. It is recording where the current entry branches relative to the previous one, counting only repeated nodes. In our example, Box and Candy are the two repeated nodes, which gives repetition levels from 0 through 2.
So the Parquet levels for our example, with Box and Candy as plain repeated fields, are:
| Name | Repetition level | Definition level |
|---|---|---|
| Twix | 0 | 3 |
| Mars | 2 | 3 |
| NULL, unnamed candy | 1 | 2 |
| NULL, empty box | 1 | 1 |
| NULL, no boxes | 0 | 0 |
That’s it! That’s our columnar representation of the two records that gets placed in Parquet.
Here’s the same table as a picture. Each row is one entry, and each cell is one node on its path. The dark part is the repetition level (what Keep holds on to), and the dark and blue parts together are the definition level (how far Build goes).
Final Observation
In summary, what initially looks like a specialized encoding scheme comes down to a familiar operation on trees :) We just walk through the leaves in order, Keep the common part of consecutive paths, and Build the rest down to where each path ends. With those two pieces of information, a column can preserve the nesting structure of its records without storing the full tree with every value!
Further reading:
- Michael A. Bender and Martín Farach-Colton, The LCA Problem Revisited, LATIN 2000.
- Sergey Melnik et al., Dremel: Interactive Analysis of Web-Scale Datasets, PVLDB, 2010.
- Apache Parquet: Nested Encoding.