private String unparseTree(Tree child) throws SemanticException {
    ASTNode input = (ASTNode) child;
    ((HiveConf) (ctx.getConf())).setBoolVar(HiveConf.ConfVars.HIVE_CBO_ENABLED, false);
    BaseSemanticAnalyzer sem = SemanticAnalyzerFactory.get(queryState, input);

    sem.analyze(input, ctx);
    sem.validate();
    sem.executeUnParseTranslations();

    String expandedText = ctx.getTokenRewriteStream().toString(input.getTokenStartIndex(), input.getTokenStopIndex());

    return expandedText;
  }
Unparses the input AST node into correctly quoted sql string.
ql/src/java/org/apache/hadoop/hive/ql/parse/ScheduledQueryAnalyzer.java