Package org.apache.hadoop.hive.ql.parse

Examples of org.apache.hadoop.hive.ql.parse.ASTNode


   * @throws SemanticException
   */
  private static void doSemanticAnalysis(SemanticAnalyzer sem,
      ASTNode ast, Context ctx) throws SemanticException {
    QB qb = new QB(null, null, false);
    ASTNode child = ast;
    ParseContext subPCtx = ((SemanticAnalyzer) sem).getParseContext();
    subPCtx.setContext(ctx);
    ((SemanticAnalyzer) sem).initParseCtx(subPCtx);

    LOG.info("Starting Sub-query Semantic Analysis");
View Full Code Here


      out = new PrintStream(outS);

      QB qb = work.getQb();
      TokenRewriteStream stream = work.getCtx().getTokenRewriteStream();
      String program = "sq rewrite";
      ASTNode ast = work.getAst();
     
      try {
        addRewrites(stream, qb, program, out);
        out.println("\nRewritten Query:\n" + stream.toString(program,
            ast.getTokenStartIndex(), ast.getTokenStopIndex()));
      } finally {
        stream.deleteProgram(program);
      }
     
      out.close();
View Full Code Here

    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);
View Full Code Here

    }
  }
 
  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 + "' ";
View Full Code Here

      String[] tabColAlias = inputRR.reverseLookup(inpCInfo.getInternalName());
      if (tabColAlias != null) {
        colAlias = tabColAlias[1];
      }
      ASTNode inExpr = null;
      inExpr = PTFTranslator.getASTNode(inpCInfo, inputRR);
      if ( inExpr != null ) {
        rr.putExpression(inExpr, cInfo);
        colAlias = inExpr.toStringTree().toLowerCase();
      }
      else {
        colAlias = colAlias == null ? cInfo.getInternalName() : colAlias;
        rr.put(cInfo.getTabAlias(), colAlias, cInfo);
      }
View Full Code Here

      ArrayList<ObjectInspector> selectListExprOIs = new ArrayList<ObjectInspector>();
      int i = 0;
      for(WindowExpressionSpec expr : selectSpec)
      {
        String selectColName = expr.getAlias();
        ASTNode selectColumnNode = expr.getExpression();
        ExprNodeDesc selectColumnExprNode =
            ResultExpressionParser.buildExprNode(selectColumnNode,
            selectListInputTypeCheckCtx);
        ExprNodeEvaluator selectColumnExprEval =
            ExprNodeEvaluatorFactory.get(selectColumnExprNode);
View Full Code Here

    /*
     * 3. convert filterNode
     */
    if (where != null) {
      ASTNode cond = where.getCondition().accept(new RexVisitor(schema));
      hiveAST.where = ASTBuilder.where(cond);
    }

    /*
     * 4. GBy
     */
    if (groupBy != null) {
      ASTBuilder b = ASTBuilder.construct(HiveParser.TOK_GROUPBY, "TOK_GROUPBY");
      for (int i : BitSets.toIter(groupBy.getGroupSet())) {
        RexInputRef iRef = new RexInputRef(i, groupBy.getCluster().getTypeFactory()
            .createSqlType(SqlTypeName.ANY));
        b.add(iRef.accept(new RexVisitor(schema)));
      }

      if (!groupBy.getGroupSet().isEmpty())
        hiveAST.groupBy = b.node();
      schema = new Schema(schema, groupBy);
    }

    /*
     * 5. Having
     */
    if (having != null) {
      ASTNode cond = having.getCondition().accept(new RexVisitor(schema));
      hiveAST.having = ASTBuilder.having(cond);
    }

    /*
     * 6. Project
     */
    ASTBuilder b = ASTBuilder.construct(HiveParser.TOK_SELECT, "TOK_SELECT");

    if (select.getChildExps().isEmpty()) {
      RexLiteral r = select.getCluster().getRexBuilder().makeExactLiteral(new BigDecimal(1));
      ASTNode selectExpr = ASTBuilder.selectExpr(ASTBuilder.literal(r), "1");
      b.add(selectExpr);
    } else {
      int i = 0;

      for (RexNode r : select.getChildExps()) {
        ASTNode selectExpr = ASTBuilder.selectExpr(r.accept(
             new RexVisitor(schema, r instanceof RexLiteral)),
                  select.getRowType().getFieldNames().get(i++));
        b.add(selectExpr);
      }
    }
View Full Code Here

  private void convertOBToASTNode(HiveSortRel order) {
    if (order != null) {
      HiveSortRel hiveSort = (HiveSortRel) order;
      if (!hiveSort.getCollation().getFieldCollations().isEmpty()) {
        // 1 Add order by token
        ASTNode orderAst = ASTBuilder.createAST(HiveParser.TOK_ORDERBY, "TOK_ORDERBY");

        schema = new Schema((HiveSortRel) hiveSort);
        Map<Integer, RexNode> obRefToCallMap = hiveSort.getInputRefToCallMap();
        RexNode obExpr;
        ASTNode astCol;
        for (RelFieldCollation c : hiveSort.getCollation().getFieldCollations()) {

          // 2 Add Direction token
          ASTNode directionAST = c.getDirection() == RelFieldCollation.Direction.ASCENDING ? ASTBuilder
              .createAST(HiveParser.TOK_TABSORTCOLNAMEASC, "TOK_TABSORTCOLNAMEASC") : ASTBuilder
              .createAST(HiveParser.TOK_TABSORTCOLNAMEDESC, "TOK_TABSORTCOLNAMEDESC");

          // 3 Convert OB expr (OB Expr is usually an input ref except for top
          // level OB; top level OB will have RexCall kept in a map.)
          obExpr = null;
          if (obRefToCallMap != null)
            obExpr = obRefToCallMap.get(c.getFieldIndex());

          if (obExpr != null) {
            astCol = obExpr.accept(new RexVisitor(schema));
          } else {
            ColumnInfo cI = schema.get(c.getFieldIndex());
            /*
             * The RowResolver setup for Select drops Table associations. So
             * setup ASTNode on unqualified name.
             */
            astCol = ASTBuilder.unqualifiedName(cI.column);
          }

          // 4 buildup the ob expr AST
          directionAST.addChild(astCol);
          orderAst.addChild(directionAST);
        }
        hiveAST.order = orderAst;
      }
    }
View Full Code Here

    return new Schema(select, tblAlias);
  }

  private QueryBlockInfo convertSource(RelNode r) {
    Schema s;
    ASTNode ast;

    if (r instanceof TableAccessRelBase) {
      TableAccessRelBase f = (TableAccessRelBase) r;
      s = new Schema(f);
      ast = ASTBuilder.table(f);
    } else if (r instanceof JoinRelBase) {
      JoinRelBase join = (JoinRelBase) r;
      QueryBlockInfo left = convertSource(join.getLeft());
      QueryBlockInfo right = convertSource(join.getRight());
      s = new Schema(left.schema, right.schema);
      ASTNode cond = join.getCondition().accept(new RexVisitor(s));
      boolean semiJoin = join instanceof SemiJoinRel;
      ast = ASTBuilder.join(left.ast, right.ast, join.getJoinType(), cond, semiJoin);
      if (semiJoin)
        s = left.schema;
    } else if (r instanceof UnionRelBase) {
      RelNode leftInput = ((UnionRelBase) r).getInput(0);
      RelNode rightInput = ((UnionRelBase) r).getInput(1);

      ASTConverter leftConv = new ASTConverter(leftInput, this.derivedTableCount);
      ASTConverter rightConv = new ASTConverter(rightInput, this.derivedTableCount);
      ASTNode leftAST = leftConv.convert();
      ASTNode rightAST = rightConv.convert();

      ASTNode unionAST = getUnionAllAST(leftAST, rightAST);

      String sqAlias = nextAlias();
      ast = ASTBuilder.subQuery(unionAST, sqAlias);
      s = new Schema((UnionRelBase) r, sqAlias);
    } else {
      ASTConverter src = new ASTConverter(r, this.derivedTableCount);
      ASTNode srcAST = src.convert();
      String sqAlias = nextAlias();
      s = src.getRowSchema(sqAlias);
      ast = ASTBuilder.subQuery(srcAST, sqAlias);
    }
    return new QueryBlockInfo(s, ast);
View Full Code Here

    }
  }

  public ASTNode getUnionAllAST(ASTNode leftAST, ASTNode rightAST) {

    ASTNode unionTokAST = ASTBuilder.construct(HiveParser.TOK_UNION, "TOK_UNION").add(leftAST)
        .add(rightAST).node();

    return unionTokAST;
  }
View Full Code Here

TOP

Related Classes of org.apache.hadoop.hive.ql.parse.ASTNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.