Factor out complex Compiler::compileQuery() to separate functions