Program: 25359
Project: 25359
Buggy source:
public void buildQuery(){
  try {
    analyzeInternal();
  }
 catch (  SemanticException e) {
    e.printStackTrace();
  }
  try {
    CubeMetastoreClient client=CubeMetastoreClient.getInstance(new HiveConf(conf,ColumnarSQLRewriter.class));
    replaceWithUnderlyingStorage(fromAST,client);
    fromTree=HQLParser.getString(fromAST);
  }
 catch (  HiveException exc) {
    LOG.error("String_Node_Str",exc);
  }
  getFilterInJoinCond(fromAST);
  getAggregateColumns(selectAST);
  getJoinCond(fromAST);
  getAllFilters(whereAST);
  buildSubqueries(fromAST);
  String limit=getLimitClause(ast);
  if (whereTree == null || joinTree == null || allSubQueries.length() == 0) {
    LOG.info("String_Node_Str");
    constructQuery(selectTree,fromTree,whereTree,groupByTree,havingTree,orderByTree,limit);
    return;
  }
 else {
    factInLineQuery.append("String_Node_Str").append(factKeys);
    if (!aggColumn.isEmpty())     factInLineQuery.append(aggColumn.toString().replace("String_Node_Str","String_Node_Str").replace("String_Node_Str","String_Node_Str"));
    if (factInLineQuery.toString().substring(factInLineQuery.toString().length() - 1).equals("String_Node_Str"))     factInLineQuery.setLength(factInLineQuery.length() - 1);
    factInLineQuery.append("String_Node_Str").append(factTable).append("String_Node_Str").append(factAlias);
    if (allSubQueries != null) {
      factInLineQuery.append("String_Node_Str");
      factInLineQuery.append(allSubQueries.toString().substring(0,allSubQueries.lastIndexOf("String_Node_Str")));
    }
    if (!aggColumn.isEmpty()) {
      factInLineQuery.append("String_Node_Str");
      factInLineQuery.append(factKeys.toString().substring(0,factKeys.toString().lastIndexOf("String_Node_Str")));
    }
    factInLineQuery.append("String_Node_Str");
  }
  for (  Map.Entry<String,String> entry : mapAggTabAlias.entrySet()) {
    selectTree=selectTree.replace(entry.getKey(),entry.getValue());
    if (orderByTree != null) {
      orderByTree=orderByTree.replace(entry.getKey(),entry.getValue());
    }
    if (havingTree != null) {
      havingTree=havingTree.replace(entry.getKey(),entry.getValue());
    }
  }
  constructQuery(selectTree,fromTree,whereTree,groupByTree,havingTree,orderByTree,limit);
}

Fixed version (ground truth):
public void buildQuery(){
  try {
    analyzeInternal();
  }
 catch (  SemanticException e) {
    e.printStackTrace();
  }
  try {
    CubeMetastoreClient client=CubeMetastoreClient.getInstance(new HiveConf(conf,ColumnarSQLRewriter.class));
    replaceWithUnderlyingStorage(fromAST,client);
    fromTree=HQLParser.getString(fromAST);
  }
 catch (  HiveException exc) {
    LOG.error("String_Node_Str",exc);
  }
  getFilterInJoinCond(fromAST);
  getAggregateColumns(selectAST);
  getJoinCond(fromAST);
  getAllFilters(whereAST);
  buildSubqueries(fromAST);
  String limit=getLimitClause(ast);
  if (whereTree == null || joinTree == null || allSubQueries.length() == 0) {
    LOG.info("String_Node_Str");
    constructQuery(selectTree,fromTree,whereTree,groupByTree,havingTree,orderByTree,limit,joinTree);
    return;
  }
 else {
    String factNameAndAlias=getFactNameAlias(fromAST).trim();
    factInLineQuery.append("String_Node_Str").append(factKeys);
    if (!aggColumn.isEmpty())     factInLineQuery.append(aggColumn.toString().replace("String_Node_Str","String_Node_Str").replace("String_Node_Str","String_Node_Str"));
    if (factInLineQuery.toString().substring(factInLineQuery.toString().length() - 1).equals("String_Node_Str"))     factInLineQuery.setLength(factInLineQuery.length() - 1);
    factInLineQuery.append("String_Node_Str").append(factNameAndAlias);
    if (allSubQueries != null) {
      factInLineQuery.append("String_Node_Str");
      factInLineQuery.append(allSubQueries.toString().substring(0,allSubQueries.lastIndexOf("String_Node_Str")));
    }
    if (!aggColumn.isEmpty()) {
      factInLineQuery.append("String_Node_Str");
      factInLineQuery.append(factKeys.toString().substring(0,factKeys.toString().lastIndexOf("String_Node_Str")));
    }
    factInLineQuery.append("String_Node_Str");
  }
  for (  Map.Entry<String,String> entry : mapAggTabAlias.entrySet()) {
    selectTree=selectTree.replace(entry.getKey(),entry.getValue());
    if (orderByTree != null) {
      orderByTree=orderByTree.replace(entry.getKey(),entry.getValue());
    }
    if (havingTree != null) {
      havingTree=havingTree.replace(entry.getKey(),entry.getValue());
    }
  }
  constructQuery(selectTree,fromTree,whereTree,groupByTree,havingTree,orderByTree,limit,joinTree);
}