this is very close; need to throw an appropriate error when invalid aggregate used in WHERE clause, and do a lot of cleanup