Improve count distinct serialization #587