Here's my first patch.
This is very incomplete, but it does work with basic querys. An
example program is attached.
The patch also includes significant, but not comprehensive,
refactoring of query paths. I should stress that the way it's done
reflects only my own views and there are many other ways of achieving
the same ends.