method that recursively fetches identifiers of a parsed query