replace reduce with each to clarify intent

The way we used reduce resulted in a change to a data structure defined
outside of the scope of the reduce statement.  Better to use each in
this case.
cache grouping of query parts