An Aggregation Query Processing Trick
I learned a neat trick the other day watching a talk from an engineer at Snowflake that I think is really sweet. The speaker didn’t elaborate on exactly how it worked, so I had to do some reverse engineering to think through some of the details, and here is that presented. I’m not aware of another resource on this topic, if you know of one let me know!
Background: Adaptive Query Processing
First, some background. The way query planning typically works is that you have some guesses about the sizes of various intermediate relations. This impacts your query planning decisions because of constant factors: one algorithm might be better at a smaller input size, while another is better at a larger input size. It’s approximately impossible for these guesses to be perfect all the time, since as your queries get more complex, error tends to accumulate exponentially.
The result of this is that it can be desirable to have a query plan which is adaptive. If what it sees is not what it expected, it can change course and do something that is better aligned with the reality of the situation.
This has been explored in some very good papers, such as How I Learned to Stop Worrying and Love Re-optimization, where the general approach is to accumulate results in intermediate nodes, and if estimations are sufficiently wrong, to re-plan the entire query with that intermediate node now being a base relation for which we have exact statistics.
This is cool, and as they show, seems to be effective, but it has the limitation that we are required to materialize the entire intermediate relation, which might not be desirable, especially if the problem we are accomodating is that it happens to be much larger than we anticipated. What’s more, re-planning probably involves forcibly shutting down processing of the query and starting over with this fresh relation. All workable, but slightly unelegant.
What I like about the Snowflake trick is that it has none of these problems: it’s truly adaptive in that query operators can change their behaviour mid-run, without re-planning or invalidating any work that has already been done.
Background: Aggregation
A common operation in the processing of a query is aggregation.
In SQL, this is GROUP BY.
It’s parameterized by an input , an aggregation , and a set of grouping columns to group on ,
all written
For instance, say our relation was
| 1 | 2 | 10 |
| 1 | 2 | 30 |
| 2 | 5 | 20 |
| 2 | 2 | 5 |
| 3 | 1 | 15 |
Then
| 1 | 4 |
| 2 | 7 |
| 3 | 1 |
Each row agreeing on the value for gets slotted into the same group, and their values for get aggregated by taking their sum.
Often this kind of aggregation is implemented with a hash table keyed on the grouping columns. When we see a row, we hash its values for the grouping columns and then map that to the current aggregation. From this, we can see that the amount of memory taken up by an aggregation is thus proportional to the total number of distinct values for the grouping columns.
Background: Joins
TODO
Performing Group-By before Join
There is a paper on how to rearrange joins and aggregations in the general case. At least, I assume it’s the general case, I’m not sure, I personally find the paper incomprehensible.
Here’s a simpler example of such a transformation (say has column set and has column set ):