Split up ShallowCompactor's expand; move building of queries into own method

Change-Id: I694f7f5443e36482b3e39f3b3660a60173bbb387
Merge "Split up ShallowCompactor's expand; move building of queries into own method"