I would still code most of the body of this function as a recursive function:

OwnedHqlExpr folded - foldHqlExpr(instance.query);
expandResults(out, folded);

void expandResults(StringBuffer & out, IHqlExpression * expr)
{
switch (expr->getOperator())
{
case no_output:
case no_datasetfromdictionary:
case ...
   expandResults(out, expr->queryChild(0));
   break;
case no_constant:
...
case no_list:
   etc.
default:
   EClIR
   throw
}