private void replaceViewReferenceWithDefinition(QB qb, Table tab,
                                                  String tab_name, String alias) throws SemanticException {

    ASTNode viewTree;
    final ASTNodeOrigin viewOrigin = new ASTNodeOrigin("VIEW", tab.getTableName(),
        tab.getViewExpandedText(), alias, qb.getParseInfo().getSrcForAlias(
        alias));
    try {
      // Reparse text, passing null for context to avoid clobbering
      // the top-level token stream.
      String viewFullyQualifiedName = tab.getCompleteName();
      String viewText = tab.getViewExpandedText();
      TableMask viewMask = new TableMask(this, conf, false);
      viewTree = ParseUtils.parse(viewText, ctx, tab.getCompleteName());
      cacheTableHelper.populateCacheForView(ctx.getParsedTables(), conf,
          getTxnMgr(), tab.getDbName(), tab.getTableName());
      if (viewMask.isEnabled() && analyzeRewrite == null) {
        ParseResult parseResult = rewriteASTWithMaskAndFilter(viewMask, viewTree,
            ctx.getViewTokenRewriteStream(viewFullyQualifiedName),
            ctx, db);
        viewTree = parseResult.getTree();
      }
      SemanticDispatcher nodeOriginDispatcher = new SemanticDispatcher() {
        @Override
        public Object dispatch(Node nd, java.util.Stack<Node> stack,
                               Object... nodeOutputs) {
          ((ASTNode) nd).setOrigin(viewOrigin);
          return null;
        }
      };
      SemanticGraphWalker nodeOriginTagger = new DefaultGraphWalker(
          nodeOriginDispatcher);
      nodeOriginTagger.startWalking(java.util.Collections
          .<Node> singleton(viewTree), null);
    } catch (ParseException e) {
      // A user could encounter this if a stored view definition contains
      // an old SQL construct which has been eliminated in a later Hive
      // version, so we need to provide full debugging info to help
      // with fixing the view definition.
      LOG.error("Failed to replaceViewReferenceWithDefinition", e);
      StringBuilder sb = new StringBuilder();
      sb.append(e.getMessage());
      ASTErrorUtils.renderOrigin(sb, viewOrigin);
      throw new SemanticException(sb.toString(), e);
    }
    QBExpr qbexpr = new QBExpr(alias);
    doPhase1QBExpr(viewTree, qbexpr, qb.getId(), alias, true, null);
    // if skip authorization, skip checking;
    // if it is inside a view, skip checking;
    // if HIVE_STATS_COLLECT_SCANCOLS is enabled, check.
    if ((!this.skipAuthorization() && !qb.isInsideView())
        || HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_STATS_COLLECT_SCANCOLS)) {
      qb.rewriteViewToSubq(alias, tab_name, qbexpr, tab);
    } else {
      qb.rewriteViewToSubq(alias, tab_name, qbexpr, null);
    }
  }
Gets the staging directory where MR files will be stored temporary.
It walks through the QB plan to find the correct location where save temporary files. This
temporary location (or staging directory) may be created inside encrypted tables locations for
security reasons. If the QB has read-only tables, then the older scratch directory will be used,
or a permission error will be thrown if the requested query table is encrypted and the old scratch
directory is not.

@param qb The QB object that contains a list of all table locations.
@return The path to the staging directory.
@throws HiveException If an error occurs while identifying the correct staging location.
ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java