Beispiel #1
0
  void addRewrites(TokenRewriteStream stream, QB qb, String program, PrintStream out) {
    QBSubQuery sqW = qb.getWhereClauseSubQueryPredicate();
    QBSubQuery sqH = qb.getHavingClauseSubQueryPredicate();

    if (sqW != null || sqH != null) {

      ASTNode sqNode =
          sqW != null
              ? sqW.getOriginalSubQueryASTForRewrite()
              : sqH.getOriginalSubQueryASTForRewrite();
      ASTNode tokQry = getQueryASTNode(sqNode);
      ASTNode tokFrom = (ASTNode) tokQry.getChild(0);

      StringBuilder addedJoins = new StringBuilder();

      if (sqW != null) {
        addRewrites(stream, sqW, program, out, qb.getId(), true, addedJoins);
      }

      if (sqH != null) {
        addRewrites(stream, sqH, program, out, qb.getId(), false, addedJoins);
      }
      stream.insertAfter(program, tokFrom.getTokenStopIndex(), addedJoins);
    }

    Set<String> sqAliases = qb.getSubqAliases();
    for (String sqAlias : sqAliases) {
      addRewrites(stream, qb.getSubqForAlias(sqAlias).getQB(), program, out);
    }
  }
Beispiel #2
0
  void addRewrites(
      TokenRewriteStream stream,
      QBSubQuery sq,
      String program,
      PrintStream out,
      String qbAlias,
      boolean isWhere,
      StringBuilder addedJoins) {
    ASTNode sqNode = sq.getOriginalSubQueryASTForRewrite();
    ASTNode tokQry = getQueryASTNode(sqNode);
    ASTNode tokInsert = (ASTNode) tokQry.getChild(1);
    ASTNode tokWhere = null;

    for (int i = 0; i < tokInsert.getChildCount(); i++) {
      if (tokInsert.getChild(i).getType() == HiveParser.TOK_WHERE) {
        tokWhere = (ASTNode) tokInsert.getChild(i);
        break;
      }
    }

    SubQueryDiagnostic.QBSubQueryRewrite diag = sq.getDiagnostic();
    String sqStr = diag.getRewrittenQuery();
    String joinCond = diag.getJoiningCondition();

    /*
     * the SubQuery predicate has been hoisted as a Join. The SubQuery predicate is replaced
     * by a 'true' predicate in the Outer QB's where/having clause.
     */
    stream.replace(program, sqNode.getTokenStartIndex(), sqNode.getTokenStopIndex(), "1 = 1");

    String sqJoin = " " + getJoinKeyWord(sq) + " " + sqStr + " " + joinCond;
    addedJoins.append(" ").append(sqJoin);

    String postJoinCond = diag.getOuterQueryPostJoinCond();
    if (postJoinCond != null) {
      stream.insertAfter(program, tokWhere.getTokenStopIndex(), " and " + postJoinCond);
    }

    String qualifier = isWhere ? "Where Clause " : "Having Clause ";
    if (qbAlias != null) {
      qualifier = qualifier + "for Query Block '" + qbAlias + "' ";
    }
    out.println(String.format("\n%s Rewritten SubQuery:\n%s", qualifier, diag.getRewrittenQuery()));
    out.println(
        String.format(
            "\n%s SubQuery Joining Condition:\n%s", qualifier, diag.getJoiningCondition()));
  }
Beispiel #3
0
 private String getJoinKeyWord(QBSubQuery sq) {
   switch (sq.getJoinType()) {
     case LEFTOUTER:
       return "left outer join";
     case LEFTSEMI:
       return "left semi join";
     case RIGHTOUTER:
       return "right outer join";
     case FULLOUTER:
       return "full outer join";
     case INNER:
     default:
       return "inner join";
   }
 }