Beispiel #1
0
  @Override
  public int execute(DriverContext driverContext) {

    PrintStream out = null;
    try {
      Path resFile = new Path(work.getResFile());
      OutputStream outS = resFile.getFileSystem(conf).create(resFile);
      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();
      out = null;
      return (0);
    } catch (Exception e) {
      console.printError(
          "Failed with exception " + e.getMessage(), "\n" + StringUtils.stringifyException(e));
      return (1);
    } finally {
      IOUtils.closeStream(out);
    }
  }
Beispiel #2
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 #3
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 #4
0
  public static void compile(String filename, String parameters) throws Exception {

    String source = "";
    String s = "";

    int last = filename.lastIndexOf(".cmtjava");
    if (last == -1) { // arquivos devem ter extensão cmtjava
      System.out.println("Usage: java -jar cmtjavac.jar <options> <source file>");
      System.exit(0);
    } else source = filename.substring(0, last);

    last = source.lastIndexOf('/');
    if (last != -1) source = source.substring(last + 1, source.length());

    System.out.println("Translating " + source + ".cmtjava");

    String environmentVariable = System.getenv("CMTJAVAC");
    String pathForTemplate = environmentVariable + "/CMTJava.stg";

    // load in CMTJava.stg template group, put in templates variable
    FileReader groupFileR = null;
    try {
      groupFileR = new FileReader(pathForTemplate);
    } catch (Exception e) {
      System.out.println(
          "Could not load the template file for translating the source file.\nPlease set the CMTJAVAC environment variable with the path of the template file");
      System.exit(0);
    }
    StringTemplateGroup templates = new StringTemplateGroup(groupFileR);
    groupFileR.close();

    File f = new File(filename);
    FileInputStream fis = new FileInputStream(f);
    ANTLRInputStream input = new ANTLRInputStream(fis);

    CMTJavaLexer lexer = new CMTJavaLexer(input);
    TokenRewriteStream tokens = new TokenRewriteStream(lexer);
    CMTJavaParser parser = new CMTJavaParser(tokens);
    parser.setTemplateLib(templates);

    /*CMTJavaParser.compilationUnit_return r = */ parser
        .compilationUnit(); // parse rule compilationUnit
    // StringTemplate output = (StringTemplate)r.getTemplate();

    try {
      BufferedWriter out = new BufferedWriter(new FileWriter(source + ".java"));
      out.write(tokens.toString());
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    Process t = null;
    try {
      System.out.println("javac " + parameters + " " + source + ".java");
      t = Runtime.getRuntime().exec("javac " + parameters + " " + source + ".java");
    } catch (Exception e) {
      System.out.println("Could not load the javac compiler.");
      System.exit(0);
    }
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(t.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(t.getErrorStream()));

    // read the output from the command
    System.out.println("Compiling " + source + ".java");
    while ((s = stdInput.readLine()) != null) {
      System.out.println("éoq");
      System.out.println(s);
    }

    // read any errors from the attempted command
    while ((s = stdError.readLine()) != null) {
      System.out.println(s);
    }
  }