Return function that evaluates an AST of a query