Initial query rewrite support.

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

I agree that this code is going to require some careful care and refactoring in some manner as it grows to keep it sane.  I have a hunch it will change somewhat in nature as we deal with recursive stuff, so my plan is to deal with that as it happens.

In this case, exhaustivity checking isn't very useful since the purpose of the code is "see if we can find a way to rewrite it, if not then bail", and the if not case is common.