Bug fix in union refactoring and self join elimination