Query engine: fixed issues with count query