bug fixes in query rewriter