Fixes problems where certain patterns and predicates caused the execution planning to loop forever
Merge pull request #1245 from systay/never-ending-query

Plan building bugfix