I don't particularly like this method - it seems we need to know too much internals of the query/compiler setup. If it is possible to write this so that compiler or query handles the dirty details that might be nicer. If not, well, then so be it...