Extracted sanity check, reordering of rows from process() and union part from buildQueryPlan()