Initial query rewrite support.

Currently supports matching columns, literals and count(*) in the selection
and columns in grouping.


Maybe for really simple cases, but this is pretty hard to reason about.