rewrite query