Fix bug where nested condition generated wrong query.