Post · 16 min read· pp. 204–213

Functional Dependencies and Orders

Say you’re a query optimizer. Is the following transformation “valid?”

text
select * from t where x > 5
=>
select * from t where false

Well…I doubt it. The first version permits strictly more rows than the second.

But, what if I told you that we recently collected statistics on our data, and found that no more than 0.01% of our data has a value of x that’s larger than 5?

Well, again, I doubt you’d want to perform this transformation. I’d guess that those 0.01% of rows are precisely the ones whoever is running this query is interested in.

What if I told you that we recently collected statistics on our data, and found that none of our data has a value of x that’s larger than 5?

Now we’re getting tempting! But the imperative word there is “recently.” Perhaps, since you collected those statistics, a whole lot of data got ingested and they’re now wildly out of date!

Ok, ok. What if I told you that I recently added a constraint to the table that rejects rows, old and new, that have a value of x that’s larger than 5? It sounds like we need a framework.

These scenarios highlight an important question if you want to get philosophical about what your database is doing. What exactly are we saying when we express an optimization rule? When we teach our query optimizer about join commutativity:

lisp
(deftransformation
  (join a b p)
  (join b a p))

what does that mean?

A query planner, in some sense, has to balance two competing pressures: first, due to its tight integration with the rest of the database, a planner is expected to generate plans specialized to its user’s data, utilizing information not statically expressed in the schema like table sizes and data distribution. Pushing up against that is the fact that it must always produce plans that are correct, even if the statistical information it’s using to make decisions might be out of date.

In many database architectures, for instance, it would be incorrect to look at statistical information stating that “the largest value of x in the table t is 5” and optimize select * from t where x > 5 to select * from t where false, because said statistical information might not be guaranteed to be consistent. On the other hand, if there is a constraint on the table requiring x to be no greater than 5, such an optimization could be valid.

Thus, there is a divide between the types of information a planner can make use of:

  1. statistical information, like estimated table sizes, histograms, and distinctness counts, and
  2. static schema information, like column types and integrity constraints.

Which bucket a given piece of information falls into can vary between databases, Snowflake, for instance, keeps certain statistical information consistent, so the planner can rely on it being correct.

In the world of database theory, people are generally concerned with the second kind of information. The machinery we use to talk about database queries formally is to distinguish between a database schema, which describes what data could live in the database, and a database instance, which describes what data does live in the database. This sounds obvious, but I think it’s subtly different from the view that SQL databases push you towards, since they use the same language, SQL, to both modify schemas and to modify data.

The point for me of all this is that this gives us a simple and precise way to talk about when a given query transformation is “valid:” it’s valid for a given schema if it’s valid for every instance of that schema. So even though our queries will always be running against one particular instance, an important correctness condition is that they’re valid for the schema as a whole.

So the answer to what this means:

lisp
(deftransformation
  (join a b p)
  (join b a p))

is that we are asserting that these two expressions have the same value for every instance allowed by our schema.

Today, I want to use this machinery to talk about some particularly fun and cool notions pervasive in SQL query optimization.

Ordering Data

Let’s run some SQL queries.

First, I’m going to create a table with a computed column. This is a column whose value is derived from the value of other columns.

text
$ sqldb
sqldb> create table abc (a int, b int, c int as (b * 2));

Now let’s insert some data into this table.

text
sqldb> insert into abc (a, b) values
  (1, 2),
  (1, 3),
  (2, 2),
  (2, 3),
  (2, 4),
  (3, 1);

Now let’s read it back. We should see the value of c computed by doubling b:

text
sqldb> select * from abc;
a|b|c
-+-+-
2|2|4
1|2|4
1|3|6
2|4|8
3|1|2
2|3|6

Note that the rows aren’t arranged in any particular order. SQL doesn’t guarantee anything about the order of the results in a query, so if we want the data back in a particular order, we have to use order by:

text
sqldb> select * from abc order by a;
a|b|c
-+-+-
1|3|6
1|2|4
2|3|6
2|2|4
2|4|8
3|1|2

order by a tells the database we’d like it to retrieve the data ordered on the a column.

Note, though, that this doesn’t guarantee that the results will always be in the same order, there’s multiple ways to arrange this result set that are ordered on a. This response would also have been completely valid:

text
sqldb> select * from abc order by a;
a|b|c
-+-+-
1|2|4
1|3|6
2|3|6
2|4|8
2|2|4
3|1|2

note that the rows (1,2,4)(1, 2, 4) and (1,3,6)(1, 3, 6) could have gone in either order, since we’re only comparing by the first coordinate.

If we want to be more precise about the order we want the data, we can specify additional columns:

text
sqldb> select * from abc order by a, b;
a|b|c
-+-+-
1|2|4
1|3|6
2|2|4
2|3|6
2|4|8
3|1|2

order by a, b is a lexicographic ordering. It means “order first by the a column, and if two rows have the same value for a, order them by the b column.”

If we wanted to be even more specific, we could order by a, then b, then c:

text
sqldb> select * from abc order by a, b, c;
a|b|c
-+-+-
1|2|4
1|3|6
2|2|4
2|3|6
2|4|8
3|1|2

But… We might notice that it seems like this doesn’t actually provide any additional power over just ordering by a, b… why is that? We might think this might just be a quirk of the particular data we inserted, this instance, but it turns out any data we put into this table will have this property.

Thus, it seems as though a clever optimizer might be able to look at a, b, c and determine that there’s no need for c and eliminate it. This post will talk a bit about how such determinations can be made.

Let’s get a bit more mathematical and precise.

Orders

A result set is a relation, which is a set of rows, where each column has a name:

idpetowner
1PeteeLauren
2CarlSpencer
3SmudgeJustin
4CarlSpencer
5SisselJustin

Typically we consider relations to be unordered, since they’re sets. In many contexts, however, we do have an order on them, like if we are streaming them between operators, materializing them in memory, or serializing them to disk. Such an ordering defines a total order <R<_R over the rows: a<Rba <_R b if aa comes before bb in RR.

If a relation has an ordering upon its rows we call it an ordered relation.

An order is an ordered, distinct list of column names, such as ⟨id⟩\langle \texttt{id} \rangle, ⟨name⟩\langle \texttt{name} \rangle, or ⟨name,id⟩\langle \texttt{name}, \texttt{id} \rangle.

An order OO defines a preorder ≤O\le_O on the set of rows of a relation: we can compare any two rows lexicographically by comparing their values for the first column in OO, then the second, and so on, until we find a place they disagree. If they never disagree, we say they’re peers. Two rows can be peers and not be equal, if they have columns not appearing in the order. This is what makes this a preorder and not a total order: we don’t have antisymmetry unless every column appears in the order.

An ordered relation RR adheres to an order OO if ≤R⊆≤O\le_R \subseteq \le_O, or, rather, if aa precedes bb in RR, then a≤Oba \le_O b.

More concretely, if you typed order by c1, ..., cn in a SQL terminal, the order of the result you get should always adhere to ⟨c1,…,cn⟩\langle c_1, \ldots, c_n \rangle.

Let’s come up for air and look at some examples. We need the formalism because I want to show some fun stuff about these things, but the ideas are not hard. Let’s consider our relation RR from above:

idpetowner
1PeteeLauren
2CarlSpencer
3SmudgeJustin
4CarlSpencer
5SisselJustin

As written, this relation adheres to ⟨id⟩\langle \texttt{id} \rangle since the rows are in the order of the id column.

It does not adhere to ⟨pet,owner⟩\langle \texttt{pet}, \texttt{owner} \rangle. This is because

(1,Petee,Lauren)<R(2,Carl,Spencer)(\text{1}, \text{Petee}, \text{Lauren}) <_R (\text{2}, \text{Carl}, \text{Spencer})
(because the first row physically comes before the second), but
(2,Carl,Spencer)<⟨pet,owner⟩(1,Petee,Lauren)(\text{2}, \text{Carl}, \text{Spencer}) <_{\langle \texttt{pet}, \texttt{owner} \rangle} (\text{1}, \text{Petee}, \text{Lauren})
(because “Carl” is alphabetically before “Petee,” and under ⟨pet,owner⟩\langle \texttt{pet}, \texttt{owner} \rangle, the pet column is how we compare rows first). Since the two disagree somewhere, RR doesn’t adhere to ⟨pet,owner⟩\langle \texttt{pet}, \texttt{owner} \rangle.

Ordering Orders

Orderings can have interesting relationships to each other. Note that any ordered relation that adheres to ⟨owner,pet⟩\langle \texttt{owner}, \texttt{pet} \rangle also adheres to ⟨owner⟩\langle \texttt{owner} \rangle, since if something is ordered on owner and then pet, it must also be ordered on owner. Intuitively, ⟨owner,pet⟩\langle \texttt{owner}, \texttt{pet} \rangle is “stronger” than ⟨owner⟩\langle \texttt{owner} \rangle.

This is where we will use our schema/instance distinction to figure out exactly what we mean.

If every instance of a schema adhering to PP also adheres to OO, we say PP is as strong as OO and write

O≤P.O \le P.

Claim: O≤PO \le P if and only if the columns in OO are a prefix of the columns in PP.

Proof:

The case where OO is a prefix of PP is easy: any pair of rows which are ≤P\le_P must clearly also be ≤O\le_O since there is a subset of the comparisons being made, so O≤PO \le P.

For the other direction (where OO is not a prefix of PP), we want to show there is some ordered relation RR that adheres to PP but not OO, and for that it suffices to find two rows r,sr, s such that r>Osr >_O s and r≤Psr \le_P s.

If PP is a prefix of OO, there’s some column oo that appears in OO immediately following their shared prefix. Construct rr and ss so that they agree on the shared prefix, and not on that column:

…oo…
…1…
…0…

Now assume PP is not prefix of OO. Then there is some pair of columns pp and oo immediately following the common prefix of the two orders.

Again, construct rr and ss:

…oopp…
…10…
…01…

For both cases, the constructed rows satisfy r>Osr >_O s and r≤Psr \le_P s □\square.

Functional Dependencies

A functional dependency is a pair X→YX \rightarrow Y where XX and YY are sets of column names.

A result set SS adheres to a set of functional dependencies FF if for every X→Y∈FX \rightarrow Y \in F, whenever two rows in SS agree on XX, they also agree on YY. Another way to state this is that the projection of SS to (X,Y)(X, Y) is functional (hence the name “functional dependency”).

The closure of a set of functional dependencies F∗F^* is the set of functional dependencies that can be inferred from FF. It can be generated by repeated application of Armstrong’s Axioms:

  • Reflexivity: If Y⊆XY \subseteq X then X→YX \rightarrow Y.
  • Augmentation: If X→YX \rightarrow Y then X∪Z→Y∪ZX\cup Z \rightarrow Y \cup Z.
  • Transitivity: If X→YX \rightarrow Y and Y→ZY \rightarrow Z then X→ZX \rightarrow Z.

See this earlier post for more details.

Functional dependencies are how we will capture the situation we opened with, where we had a column derived from other columns. Note that in any instance of this schema:

text
sqldb> create table abc (a int, b int, c int as (b * 2));

we must have

{b}→{c}.\{b\} \rightarrow \{c\}.

This is somewhat contrived, but functional dependencies arise very naturally in many SQL queries, both simple and complex.

Ordering Orders Pt. 2

Oftentimes when planning a query, we know that a given relation adheres to some set of functional dependencies. Functional dependencies can impact the way that orderings relate to each other. Consider our relation from before:

idpetowner
1PeteeLauren
2CarlSpencer
3SmudgeJustin
4CarlSpencer
5SisselJustin

While this is just one instance, let’s assume that the following will be true of any instance:

  1. Every row has a unique id. This means we have the functional dependency {id}→{pet,owner}\{ \texttt{id} \} \rightarrow \{ \texttt{pet}, \texttt{owner} \}.
  2. Every pet has a unique owner, this means we have the functional dependency {pet}→{owner}\{ \texttt{pet} \} \rightarrow \{ \texttt{owner} \}.

(remember that if we were automating this process in a database, we’d need to know statically that these two assumptions would always be true, likely via some kind of integrity constraint.)

If we restrict ourselves to instances that obey those functional dependencies, we can reach some interesting conclusions. Not only do we have

⟨pet⟩≤⟨pet,owner⟩,\langle \texttt{pet} \rangle \le \langle \texttt{pet}, \texttt{owner} \rangle,
as before, but, perhaps surprisingly, we have the reverse:
⟨pet,owner⟩≤⟨pet⟩.\langle \texttt{pet}, \texttt{owner} \rangle \le \langle \texttt{pet} \rangle.
as well.

You might view this as, “if I can only see how they actually behave in a valid instance, I can’t distinguish between ⟨pet,owner⟩\langle \texttt{pet}, \texttt{owner} \rangle and ⟨pet⟩\langle \texttt{pet} \rangle.” We care a lot about this kind of indistinguishability in the world of query planning. If a user asks us for a particular order, but we think we can cheaply provide some different order, if it turns out those two orders are actually the same in every instance, we might be able to service their query more efficiently.

Remember what we’re saying here: I’m claiming that any instance that adheres to ⟨pet⟩\langle \texttt{pet} \rangle also adheres to ⟨pet,owner⟩\langle \texttt{pet}, \texttt{owner} \rangle. Why might that be true? It’s because we have {pet}→{owner}\{ \texttt{pet} \} \rightarrow \{ \texttt{owner} \}, which means any time two rows agree on pet (precisely when we’d then compare their owners), they also agree on owner, so no matter what order they’re in, they can’t be out-of-order.

The rough idea is that as we read an order from left-to-right, for any given row we’re accumulating a set of fixed values. If we ever see a column that’s determined by the columns we’ve already seen, that column only has one possible value, and thus is irrelevant to the order.

For a set of functional dependencies FF, define a new partial order ≤F\le_F on the set of orderings in more-or-less the same way as before: let OO and PP be orderings. For every instance RR adhering to FF, if RR adhering to PP implies RR adheres to OO, O≤FPO \le_F P (you could call this “strength given adherence to FF").

It would be really nice if we could automate this determination. If a user were to write select * from t order by pet, owner, and we have an index on pet, there’s no need to do any sorting, since this is indistinguishable from them writing select * from t order by pet, but to actually avoid the sort, we need to be able to detect this.

You might say, “well, why would they write an obviously redundant query?” This is a reasonable objection to have, but a crucial perspective on query optimization is that most queries are not written by humans. It’s important that queries generated by dashboards, ORMs, or other query optimization rules need not be concerned with eliminating such redundancy. That’s the query optimizer’s job.

Ordering Orders with Functional Dependencies

We’d like an easy way to compute ≤F\le_F. We can do this via a representative.

Define repF(O)\text{rep}_F(O) to be OO, but removing any columns which are determined by the columns preceding them in OO according to FF.

Algorithmically this would look something like:

python
def rep(O, F):
  F = F*
  out = []
  fixed_cols = {}
  for col in O:
    if not F.contains(fixed_cols -> col):
      out.push(col)
    fixed_cols.add(col)
  return out

Claim: O≤FPO \le_F P if and only if repF(O)≤repF(P)\text{rep}_F(O) \le \text{rep}_F(P).

Note that if this claim were true it would give us an easy way to evaluate ≤F\le_F: simply take the representatives of the two orders and check if the first is a prefix of the second.

Lemma: for all OO and FF, O≤FrepF(O)O \le_F \text{rep}_F(O) and repF(O)≤FO\text{rep}_F(O) \le_F O (meaning they agree on all pairs of rows).

Proof of lemma: if the two disagree on some pair of rows, the disagreement must be in a column present in OO and not repF(O)\text{rep}_F(O) or any earlier column in OO. Thus, that column was determined by the columns preceding it in OO, so two rows agreeing on the preceding columns can never differ on it, a contradiction.

Proof of Claim (forwards direction: O≤FP→repF(O)≤repF(P)O \le_F P \rightarrow \text{rep}_F(O) \le \text{rep}_F(P)):

By the lemma, and the fact that ≤F\le_F is transitive,

O≤FP→repF(O)≤FrepF(P)O \le_F P \rightarrow \text{rep}_F(O) \le_F \text{rep}_F(P)

Assume repF(O)≤FrepF(P)\text{rep}_F(O) \le_F \text{rep}_F(P) and repF(O)≰repF(P)\text{rep}_F(O) \not\le \text{rep}_F(P).

As we saw earlier, this means that repF(O)\text{rep}_F(O) is not a prefix of repF(P)\text{rep}_F(P).

We can now repeat the same proof as before:

If repF(P)\text{rep}_F(P) is a prefix of repF(O)\text{rep}_F(O), them construct these rows, where oo is the first column not appearing in repF(P)\text{rep}_F(P):

…oo…
…1…
…0…

and if not, let oo and pp be the first columns that differ in the two, and construct these rows:

…oopp…
…10…
…01…

These rows can be made to be valid even in relations adhering to FF, since by the construction of the representatives, neither oo nor pp are determined by the columns in the shared prefix, contradicting repF(O)≤FrepF(P)\text{rep}_F(O) \le_F \text{rep}_F(P).

Proof of Claim (reverse direction: repF(O)≤repF(P)→O≤FP\text{rep}_F(O) \le \text{rep}_F(P) \rightarrow O \le_F P):

By the lemma, and the fact that ≤\le is transitive,

repF(O)≤repF(P)→O≤P\begin{aligned} \text{rep}_F(O) &\le \text{rep}_F(P) \rightarrow O \le P \end{aligned}
since ≤F\le_F considers a subset of relations (namely, it only considers those adhering to FF), it’s weaker than ≤\le, giving
O≤FP\begin{aligned} O &\le_F P \end{aligned}
as needed □\square.

There’s additional dependencies that can give rise to even more equivalencies: whether two columns are always equal (which can be common following a join), whether one column to another is a monotonic function (which can be common in cases like extracting the month from a date), etc.

This is of course a small piece of what makes up a query optimizer. Things like orders, partitions, and functional dependencies make up the vocabulary of how optimizers think about relations, and I think it’s important to have a solid foundation to build more complex optimizations on top of.

Thanks to my friend Ilia for help sorting out this post.