Fix bugs in query utils