Cleanup query rewrite support and added support for limit/offset in query.