(PDB-657) Query logic refactor