Refactor query projection

(Replaces #2584)