optimize advanced row grouping code

This discussion has been closed.