made sure optimizer passes tests for manually checked queries

TODO:
- not sure if the modified code covers all cases previously covered.