Recursive query processing based on program structure