Fix for #5307 - aggregation with logical columns sometimes gave wrong results