This looks like the meat of the PR.
could you explain the part where self join is treated separately?
What's the constraint that forces 2 cases?