add global distinct support and fixed aggregation handling