Improved optimizer SQL, error handling and test cases
Merge pull request #86 from andig/aggregation-fixes

Improved optimizer SQL, error handling and test cases