WIP: Initial work in progress query rewrite support.

NOT READY FOR MERGE YET.

Currently supports matching columns, literals and count(*) in the selection
and columns in grouping.