Test for a self join only when needed.
Merge pull request #2459 from bar/2.5-patch

Test for self join only when needed.