Program: 166975
Project: 166975
Buggy source:
@Override public boolean hasNext(){
  try {
    while (true) {
switch (this.queryState) {
case REGULAR:
        if (this.elements != null) {
          return true;
        }
 else {
          if (this.queryResult != null) {
            iterateRegularQueries();
            this.first=false;
          }
          if (this.elements == null) {
            closePreparedStatement();
            if (this.distinctQueriesIterator.hasNext()) {
              this.currentDistinctQueryStack=this.distinctQueriesIterator.next();
              this.subQueryStacks=SchemaTableTree.splitIntoSubStacks(this.currentDistinctQueryStack);
              this.currentRootSchemaTableTree.resetColumnAliasMaps();
              executeRegularQuery();
              this.first=true;
            }
 else {
              if (this.rootSchemaTableTreeIterator.hasNext()) {
                this.currentRootSchemaTableTree=this.rootSchemaTableTreeIterator.next();
                this.distinctQueriesIterator=this.currentRootSchemaTableTree.constructDistinctQueries().iterator();
              }
 else {
                if (this.currentRootSchemaTableTree != null) {
                  this.currentRootSchemaTableTree.resetColumnAliasMaps();
                }
                this.queryState=QUERY.OPTIONAL;
                this.rootSchemaTableTreeIterator=this.rootSchemaTableTrees.iterator();
                break;
              }
            }
          }
        }
      break;
case OPTIONAL:
    if (this.elements != null) {
      return true;
    }
 else {
      if (this.queryResult != null) {
        iterateOptionalQueries();
        this.first=false;
      }
      if (this.elements == null) {
        closePreparedStatement();
        if (this.optionalLeftJoinResultsIterator.hasNext()) {
          this.optionalCurrentLeftJoinResult=this.optionalLeftJoinResultsIterator.next();
          this.subQueryStacks=SchemaTableTree.splitIntoSubStacks(this.optionalCurrentLeftJoinResult.getLeft());
          this.currentRootSchemaTableTree.resetColumnAliasMaps();
          executeOptionalQuery();
          this.first=true;
        }
 else {
          if (this.rootSchemaTableTreeIterator.hasNext()) {
            this.currentRootSchemaTableTree=this.rootSchemaTableTreeIterator.next();
            List<Pair<LinkedList<SchemaTableTree>,Set<SchemaTableTree>>> leftJoinResult=new ArrayList<>();
            SchemaTableTree.constructDistinctOptionalQueries(this.currentRootSchemaTableTree,leftJoinResult);
            this.optionalLeftJoinResultsIterator=leftJoinResult.iterator();
          }
 else {
            if (this.currentRootSchemaTableTree != null) {
              this.currentRootSchemaTableTree.resetColumnAliasMaps();
            }
            this.queryState=QUERY.EMIT;
            this.rootSchemaTableTreeIterator=this.rootSchemaTableTrees.iterator();
            break;
          }
        }
      }
    }
  break;
case EMIT:
if (this.elements != null) {
  return true;
}
 else {
  if (this.queryResult != null) {
    iterateEmitQueries();
    this.first=false;
  }
  if (this.elements == null) {
    closePreparedStatement();
    if (this.emitLeftJoinResultsIterator.hasNext()) {
      this.emitCurrentLeftJoinResult=this.emitLeftJoinResultsIterator.next();
      this.subQueryStacks=SchemaTableTree.splitIntoSubStacks(this.emitCurrentLeftJoinResult);
      this.currentRootSchemaTableTree.resetColumnAliasMaps();
      executeEmitQuery();
      this.first=true;
    }
 else {
      if (this.rootSchemaTableTreeIterator.hasNext()) {
        this.currentRootSchemaTableTree=this.rootSchemaTableTreeIterator.next();
        List<LinkedList<SchemaTableTree>> leftJoinResult=new ArrayList<>();
        SchemaTableTree.constructDistinctEmitBeforeQueries(this.currentRootSchemaTableTree,leftJoinResult);
        this.emitLeftJoinResultsIterator=leftJoinResult.iterator();
        if (currentRootSchemaTableTree.isFakeEmit()) {
          List<Emit<SqlgElement>> fake=new ArrayList<>();
          fake.add(new Emit<>());
          this.elements=fake;
          this.currentRootSchemaTableTree.setFakeEmit(false);
        }
      }
 else {
        if (this.currentRootSchemaTableTree != null) {
          this.currentRootSchemaTableTree.resetColumnAliasMaps();
        }
        return false;
      }
    }
  }
}
break;
}
}
}
 catch (SQLException e) {
throw new RuntimeException(e);
}
}

Fixed version (ground truth):
@Override public boolean hasNext(){
  if (lazy) {
    return hasNextLazy();
  }
  if (allElements == null) {
    List<List<Emit<SqlgElement>>> allList=new LinkedList<>();
    while (hasNextLazy()) {
      allList.add(this.elements);
      this.elements=null;
    }
    allElements=allList.iterator();
  }
  return allElements.hasNext();
}