Program: 22588
Project: 22588
Buggy source:
public static Plan getPlan(HDTQueryEngine engine,Query query,DatasetGraph dataset,Binding input,Context context){
  if (query.getAggregators().size() != 1)   return null;
  Aggregator ag=query.getAggregators().get(0).getAggregator();
  if (ag == null || !query.getHavingExprs().isEmpty() || !query.getGroupBy().isEmpty() || query.hasLimit() || query.hasOffset() || !((ag instanceof AggCount) || (ag instanceof AggCountVar) || (ag instanceof AggCountDistinct)|| (ag instanceof AggCountVarDistinct))) {
    return null;
  }
  ElementGroup el=(ElementGroup)query.getQueryPattern();
  List<Element> list=el.getElements();
  if (list.size() != 1) {
    return null;
  }
  Element ele=list.get(0);
  Node graphName=null;
  if (ele instanceof ElementNamedGraph) {
    graphName=((ElementNamedGraph)ele).getGraphNameNode();
    if (graphName.isVariable()) {
      return null;
    }
    ele=((ElementNamedGraph)ele).getElement();
    if (ele instanceof ElementGroup) {
      ElementGroup elGroup=(ElementGroup)ele;
      if (elGroup.getElements().size() != 1) {
        return null;
      }
      ele=elGroup.getElements().get(0);
    }
  }
  if (!(ele instanceof ElementPathBlock)) {
    return null;
  }
  PathBlock pb=((ElementPathBlock)ele).getPattern();
  if (pb.size() != 1) {
    return null;
  }
  TriplePath tp=pb.get(0);
  Triple triple=tp.asTriple();
  if (triple.getSubject().equals(triple.getPredicate()) || triple.getPredicate().equals(triple.getObject()) || triple.getSubject().equals(triple.getObject())) {
    return null;
  }
  if (query.getProjectVars().size() != 1) {
    return null;
  }
  Var varOutput=query.getProjectVars().get(0);
  Graph g=null;
  if (dataset instanceof DatasetGraphOne) {
    g=dataset.getDefaultGraph();
  }
 else   if (dataset instanceof DatasetGraphMapLink) {
    if (graphName != null) {
      g=dataset.getGraph(graphName);
    }
 else {
      g=dataset.getDefaultGraph();
    }
  }
  if ((g == null) || !(g instanceof HDTGraph)) {
    return null;
  }
  HDTGraph hdtg=(HDTGraph)g;
  long count;
  if (ag instanceof AggCountVarDistinct) {
    ExprList exprList=ag.getExprList();
    if (exprList.size() != 1) {
      return null;
    }
    Expr expr=exprList.get(0);
    if (!(expr instanceof ExprVar)) {
      return null;
    }
    Var countVar=expr.asVar();
    if (!triple.getSubject().isVariable() || !triple.getPredicate().isVariable() || !triple.getObject().isVariable()) {
      return null;
    }
    Dictionary dictionary=hdtg.getHDT().getDictionary();
    if (countVar.equals(triple.getSubject())) {
      count=dictionary.getNsubjects();
    }
 else     if (countVar.equals(triple.getPredicate())) {
      count=dictionary.getNpredicates();
    }
 else     if (countVar.equals(triple.getObject())) {
      count=dictionary.getNobjects();
    }
 else {
      return null;
    }
  }
 else {
    if (ag instanceof AggCountVar) {
      ExprList exprList=ag.getExprList();
      if (exprList.size() != 1) {
        return null;
      }
      Expr expr=exprList.get(0);
      if (!(expr instanceof ExprVar)) {
        return null;
      }
      Var countVar=expr.asVar();
      if (!(triple.getSubject().equals(countVar) || triple.getPredicate().equals(countVar) || triple.getObject().equals(countVar))) {
        return null;
      }
    }
    TripleID patternID=hdtg.getNodeDictionary().getTriplePatID(triple);
    if (patternID.isEmpty()) {
      count=hdtg.getHDT().getTriples().getNumberOfElements();
    }
 else {
      IteratorTripleID it=hdtg.getHDT().getTriples().search(patternID);
      if (it.numResultEstimation() == ResultEstimationType.EXACT) {
        count=it.estimatedNumResults();
      }
 else {
        count=0;
        while (it.hasNext()) {
          it.next();
          count++;
        }
      }
    }
  }
  Binding bindingResult=new BindingOne(varOutput,NodeFactory.createLiteral(Long.toString(count)));
  return new PlanOp(new HDTOptimizeddOp(),engine,new QueryIterYieldN(1,bindingResult));
}

Fixed version (ground truth):
public static Plan getPlan(HDTQueryEngine engine,Query query,DatasetGraph dataset,Binding input,Context context){
  if (query.getAggregators().size() != 1)   return null;
  Aggregator ag=query.getAggregators().get(0).getAggregator();
  if (ag == null || !query.getHavingExprs().isEmpty() || !query.getGroupBy().isEmpty() || query.hasLimit() || query.hasOffset() || !((ag instanceof AggCount) || (ag instanceof AggCountVar) || (ag instanceof AggCountDistinct)|| (ag instanceof AggCountVarDistinct))) {
    return null;
  }
  ElementGroup el=(ElementGroup)query.getQueryPattern();
  List<Element> list=el.getElements();
  if (list.size() != 1) {
    return null;
  }
  Element ele=list.get(0);
  Node graphName=null;
  if (ele instanceof ElementNamedGraph) {
    graphName=((ElementNamedGraph)ele).getGraphNameNode();
    if (graphName.isVariable()) {
      return null;
    }
    ele=((ElementNamedGraph)ele).getElement();
    if (ele instanceof ElementGroup) {
      ElementGroup elGroup=(ElementGroup)ele;
      if (elGroup.getElements().size() != 1) {
        return null;
      }
      ele=elGroup.getElements().get(0);
    }
  }
  if (!(ele instanceof ElementPathBlock)) {
    return null;
  }
  PathBlock pb=((ElementPathBlock)ele).getPattern();
  if (pb.size() != 1) {
    return null;
  }
  TriplePath tp=pb.get(0);
  Triple triple=tp.asTriple();
  if (triple == null) {
    return null;
  }
  if (triple.getSubject().equals(triple.getPredicate()) || triple.getPredicate().equals(triple.getObject()) || triple.getSubject().equals(triple.getObject())) {
    return null;
  }
  if (query.getProjectVars().size() != 1) {
    return null;
  }
  Var varOutput=query.getProjectVars().get(0);
  Graph g=null;
  if (dataset instanceof DatasetGraphOne) {
    g=dataset.getDefaultGraph();
  }
 else   if (dataset instanceof DatasetGraphMapLink) {
    if (graphName != null) {
      g=dataset.getGraph(graphName);
    }
 else {
      g=dataset.getDefaultGraph();
    }
  }
  if ((g == null) || !(g instanceof HDTGraph)) {
    return null;
  }
  HDTGraph hdtg=(HDTGraph)g;
  long count;
  if (ag instanceof AggCountVarDistinct) {
    ExprList exprList=ag.getExprList();
    if (exprList.size() != 1) {
      return null;
    }
    Expr expr=exprList.get(0);
    if (!(expr instanceof ExprVar)) {
      return null;
    }
    Var countVar=expr.asVar();
    if (!triple.getSubject().isVariable() || !triple.getPredicate().isVariable() || !triple.getObject().isVariable()) {
      return null;
    }
    Dictionary dictionary=hdtg.getHDT().getDictionary();
    if (countVar.equals(triple.getSubject())) {
      count=dictionary.getNsubjects();
    }
 else     if (countVar.equals(triple.getPredicate())) {
      count=dictionary.getNpredicates();
    }
 else     if (countVar.equals(triple.getObject())) {
      count=dictionary.getNobjects();
    }
 else {
      return null;
    }
  }
 else {
    if (ag instanceof AggCountVar) {
      ExprList exprList=ag.getExprList();
      if (exprList.size() != 1) {
        return null;
      }
      Expr expr=exprList.get(0);
      if (!(expr instanceof ExprVar)) {
        return null;
      }
      Var countVar=expr.asVar();
      if (!(triple.getSubject().equals(countVar) || triple.getPredicate().equals(countVar) || triple.getObject().equals(countVar))) {
        return null;
      }
    }
    TripleID patternID=hdtg.getNodeDictionary().getTriplePatID(triple);
    if (patternID.isEmpty()) {
      count=hdtg.getHDT().getTriples().getNumberOfElements();
    }
 else {
      IteratorTripleID it=hdtg.getHDT().getTriples().search(patternID);
      if (it.numResultEstimation() == ResultEstimationType.EXACT) {
        count=it.estimatedNumResults();
      }
 else {
        count=0;
        while (it.hasNext()) {
          it.next();
          count++;
        }
      }
    }
  }
  Binding bindingResult=new BindingOne(varOutput,NodeFactory.createLiteral(Long.toString(count),XSDDatatype.XSDinteger));
  return new PlanOp(new HDTOptimizeddOp(),engine,new QueryIterYieldN(1,bindingResult));
}