fix Join needDistinct and add unit test

I found, and this fixes, a use case where needDistinct would not be set properly in Join.