private void rewrite(RowResolver parentQueryRR,
      boolean forHavingClause,
      String outerQueryAlias, ASTNode insertClause, ASTNode selectClause) throws SemanticException {
    ASTNode whereClause = SubQueryUtils.subQueryWhere(insertClause);

    if ( whereClause == null ) {
      return;
    }

    ASTNode searchCond = (ASTNode) whereClause.getChild(0);
    List<ASTNode> conjuncts = new ArrayList<ASTNode>();
    SubQueryUtils.extractConjuncts(searchCond, conjuncts);

    ConjunctAnalyzer conjunctAnalyzer = new ConjunctAnalyzer(parentQueryRR,
        forHavingClause, outerQueryAlias);
    ASTNode sqNewSearchCond = null;

    for(ASTNode conjunctAST : conjuncts) {
      Conjunct conjunct = conjunctAnalyzer.analyzeConjunct(conjunctAST);

      /*
       * Check.12.h :: SubQuery predicates cannot only refer to Outer Query columns.
       */
      if ( conjunct.refersOuterOnly() ) {
        throw new SemanticException(ASTErrorUtils.getMsg(
            ErrorMsg.UNSUPPORTED_SUBQUERY_EXPRESSION.getMsg(),
            conjunctAST,
            "SubQuery expression refers to Outer query expressions only."));
      }

      if ( conjunct.isCorrelated() ) {
        hasCorrelation = true;
        subQueryJoinAliasExprs = new ArrayList<ASTNode>();
        String exprAlias = getNextCorrExprAlias();
        ASTNode sqExprAlias = SubQueryUtils.createAliasAST(exprAlias);
        ASTNode sqExprForCorr = SubQueryUtils.createColRefAST(alias, exprAlias);
        boolean corrCondLeftIsRewritten = false;
        boolean corrCondRightIsRewritten = false;

        if ( conjunct.getLeftExprType().refersSubQuery() ) {
          corrCondLeftIsRewritten = true;
          if ( forHavingClause && conjunct.getRightOuterColInfo() != null ) {
            corrCondRightIsRewritten = true;
            rewriteCorrConjunctForHaving(conjunctAST, false, outerQueryAlias,
                parentQueryRR, conjunct.getRightOuterColInfo());
          }
          ASTNode joinPredicate = SubQueryUtils.alterCorrelatedPredicate(
              conjunctAST, sqExprForCorr, true);
          joinConditionAST = SubQueryUtils.andAST(joinConditionAST, joinPredicate);
          subQueryJoinAliasExprs.add(sqExprForCorr);
          ASTNode selExpr = SubQueryUtils.createSelectItem(conjunct.getLeftExpr(), sqExprAlias);
          selectClause.addChild(selExpr);
          subQueryDiagnostic.addSelectClauseRewrite(conjunct.getLeftExpr(), exprAlias);
          numOfCorrelationExprsAddedToSQSelect++;
          if ( containsAggregationExprs ) {
            ASTNode gBy = getSubQueryGroupByAST();
            SubQueryUtils.addGroupExpressionToFront(gBy, conjunct.getLeftExpr());
            subQueryDiagnostic.addGByClauseRewrite(conjunct.getLeftExpr());
          }
          if ( notInCheck != null ) {
            notInCheck.addCorrExpr((ASTNode)conjunctAST.getChild(0));
          }
          subQueryDiagnostic.addJoinCondition(conjunctAST, corrCondLeftIsRewritten, corrCondRightIsRewritten);
        } else {
          corrCondRightIsRewritten = true;
          if ( forHavingClause && conjunct.getLeftOuterColInfo() != null ) {
            corrCondLeftIsRewritten = true;
            rewriteCorrConjunctForHaving(conjunctAST, true, outerQueryAlias,
                parentQueryRR, conjunct.getLeftOuterColInfo());
          }
          ASTNode joinPredicate = SubQueryUtils.alterCorrelatedPredicate(
              conjunctAST, sqExprForCorr, false);
          joinConditionAST = SubQueryUtils.andAST(joinConditionAST, joinPredicate);
          subQueryJoinAliasExprs.add(sqExprForCorr);
          ASTNode selExpr = SubQueryUtils.createSelectItem(conjunct.getRightExpr(), sqExprAlias);
          selectClause.addChild(selExpr);
          subQueryDiagnostic.addSelectClauseRewrite(conjunct.getRightExpr(), exprAlias);
          numOfCorrelationExprsAddedToSQSelect++;
          if ( containsAggregationExprs ) {
            ASTNode gBy = getSubQueryGroupByAST();
            SubQueryUtils.addGroupExpressionToFront(gBy, conjunct.getRightExpr());
            subQueryDiagnostic.addGByClauseRewrite(conjunct.getRightExpr());
          }
          if ( notInCheck != null ) {
            notInCheck.addCorrExpr((ASTNode)conjunctAST.getChild(1));
          }
          subQueryDiagnostic.addJoinCondition(conjunctAST, corrCondLeftIsRewritten, corrCondRightIsRewritten);
        }
      } else {
        sqNewSearchCond = SubQueryUtils.andAST(sqNewSearchCond, conjunctAST);
        subQueryDiagnostic.addWhereClauseRewrite(conjunctAST);
      }
    }

    if ( sqNewSearchCond != searchCond ) {
      if ( sqNewSearchCond == null ) {
        /*
         * for now just adding a true condition(1=1) to where clause.
         * Can remove the where clause from the AST; requires moving all subsequent children
         * left.
         */
        sqNewSearchCond = SubQueryUtils.constructTrueCond();
        subQueryDiagnostic.addWhereClauseRewrite("1 = 1");
      }
      whereClause.setChild(0, sqNewSearchCond);
    }

  }
@param parentQueryRR
@param forHavingClause
@param outerQueryAlias
@return true if it is correlated scalar subquery with an aggregate
@throws SemanticException
ql/src/java/org/apache/hadoop/hive/ql/parse/QBSubQuery.java