bug fixing in query rewritng