backported group by fix from 3.1