Program: 87805
Project: 87805
Buggy source:
/** 
 * Causes this node to compute its output estimates (such as number of rows, size in bytes) based on the inputs and the compiler hints. The compiler hints are instantiated with conservative default values which are used if no other values are provided. Nodes may access the statistics to determine relevant information.
 * @param statistics The statistics object which may be accessed to get statistical information. The parameter may be null, if no statistics are available.
 */
public void computeOutputEstimates(DataStatistics statistics){
  boolean allPredsAvailable=true;
  for (  List<PactConnection> incomingConnections : getIncomingConnections()) {
    if (allPredsAvailable) {
      for (      PactConnection incomingConnection : incomingConnections) {
        if (incomingConnection.getSourcePact() == null) {
          allPredsAvailable=false;
          break;
        }
      }
    }
 else {
      break;
    }
  }
  CompilerHints hints=getPactContract().getCompilerHints();
  computeUniqueFields();
  if (!allPredsAvailable) {
    this.estimatedCardinality.putAll(hints.getDistinctCounts());
    this.estimatedNumRecords=0;
    int count=0;
    for (    Entry<FieldSet,Long> cardinality : hints.getDistinctCounts().entrySet()) {
      float avgNumValues=hints.getAvgNumRecordsPerDistinctFields(cardinality.getKey());
      if (avgNumValues != -1) {
        this.estimatedNumRecords+=cardinality.getValue() * avgNumValues;
        count++;
      }
    }
    if (count > 0) {
      this.estimatedNumRecords=(this.estimatedNumRecords / count) >= 1 ? (this.estimatedNumRecords / count) : 1;
    }
 else {
      this.estimatedNumRecords=-1;
    }
    if (this.estimatedNumRecords != -1 && hints.getAvgBytesPerRecord() != -1) {
      this.estimatedOutputSize=(this.estimatedNumRecords * hints.getAvgBytesPerRecord() >= 1) ? (long)(this.estimatedNumRecords * hints.getAvgBytesPerRecord()) : 1;
    }
  }
 else {
    boolean outputCardEstimated=true;
    this.estimatedNumRecords=0;
    int count=0;
    for (    Entry<FieldSet,Long> cardinality : hints.getDistinctCounts().entrySet()) {
      float avgNumValues=hints.getAvgNumRecordsPerDistinctFields(cardinality.getKey());
      if (avgNumValues != -1) {
        this.estimatedNumRecords+=cardinality.getValue() * avgNumValues;
        count++;
      }
    }
    if (count > 0) {
      this.estimatedNumRecords=(this.estimatedNumRecords / count) >= 1 ? (this.estimatedNumRecords / count) : 1;
    }
 else {
      this.estimatedNumRecords=this.computeNumberOfStubCalls();
      if (hints.getAvgRecordsEmittedPerStubCall() != -1.0 && this.computeNumberOfStubCalls() != -1) {
        this.estimatedNumRecords=(this.computeNumberOfStubCalls() * hints.getAvgRecordsEmittedPerStubCall() >= 1) ? (long)(this.computeNumberOfStubCalls() * hints.getAvgRecordsEmittedPerStubCall()) : 1;
      }
 else {
        outputCardEstimated=false;
      }
    }
    this.estimatedCardinality.putAll(hints.getDistinctCounts());
    if (this.getUniqueFields() != null) {
      for (      FieldSet uniqueFieldSet : this.uniqueFields) {
        if (this.estimatedCardinality.get(uniqueFieldSet) == null) {
          this.estimatedCardinality.put(uniqueFieldSet,1L);
        }
      }
    }
    for (int input=0; input < getIncomingConnections().size(); input++) {
      int[] keyColumns;
      if ((keyColumns=getConstantKeySet(input)) != null) {
        long estimatedKeyCardinality;
        if (hints.getAvgRecordsEmittedPerStubCall() < 1.0) {
          double probToKeepKey=1.0 - Math.pow((1.0 - hints.getAvgRecordsEmittedPerStubCall()),this.computeStubCallsPerProcessedKey());
          estimatedKeyCardinality=(this.computeNumberOfProcessedKeys() * probToKeepKey >= 1) ? (long)(this.computeNumberOfProcessedKeys() * probToKeepKey) : 1;
        }
 else {
          estimatedKeyCardinality=this.computeNumberOfProcessedKeys();
        }
        FieldSet fieldSet=new FieldSet(keyColumns);
        if (estimatedCardinality.get(fieldSet) != null) {
          estimatedCardinality.put(fieldSet,estimatedKeyCardinality);
        }
      }
    }
    if (this.estimatedNumRecords != -1) {
      for (      Entry<FieldSet,Float> avgNumValues : hints.getAvgNumRecordsPerDistinctFields().entrySet()) {
        if (estimatedCardinality.get(avgNumValues.getKey()) == null) {
          long estimatedCard=(this.estimatedNumRecords / avgNumValues.getValue() >= 1) ? (long)(this.estimatedNumRecords / avgNumValues.getValue()) : 1;
          estimatedCardinality.put(avgNumValues.getKey(),estimatedCard);
        }
      }
    }
    if (!outputCardEstimated) {
      long newEstimatedNumRecords=0;
      count=0;
      for (      Entry<FieldSet,Long> cardinality : estimatedCardinality.entrySet()) {
        float avgNumValues=hints.getAvgNumRecordsPerDistinctFields(cardinality.getKey());
        if (avgNumValues != -1) {
          newEstimatedNumRecords+=cardinality.getValue() * avgNumValues;
          count++;
        }
      }
      if (count > 0) {
        newEstimatedNumRecords=(newEstimatedNumRecords / count) >= 1 ? (newEstimatedNumRecords / count) : 1;
      }
    }
    double estAvgRecordWidth=this.computeAverageRecordWidth();
    if (this.estimatedNumRecords != -1 && estAvgRecordWidth != -1) {
      this.estimatedOutputSize=(this.estimatedNumRecords * estAvgRecordWidth) >= 1 ? (long)(this.estimatedNumRecords * estAvgRecordWidth) : 1;
    }
    for (    Entry<FieldSet,Long> cardinality : this.estimatedCardinality.entrySet()) {
      if (cardinality.getValue() > this.estimatedNumRecords) {
        cardinality.setValue(this.estimatedNumRecords);
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Causes this node to compute its output estimates (such as number of rows, size in bytes) based on the inputs and the compiler hints. The compiler hints are instantiated with conservative default values which are used if no other values are provided. Nodes may access the statistics to determine relevant information.
 * @param statistics The statistics object which may be accessed to get statistical information. The parameter may be null, if no statistics are available.
 */
public void computeOutputEstimates(DataStatistics statistics){
  boolean allPredsAvailable=true;
  for (  List<PactConnection> incomingConnections : getIncomingConnections()) {
    if (allPredsAvailable) {
      for (      PactConnection incomingConnection : incomingConnections) {
        if (incomingConnection.getSourcePact() == null) {
          allPredsAvailable=false;
          break;
        }
      }
    }
 else {
      break;
    }
  }
  CompilerHints hints=getPactContract().getCompilerHints();
  computeUniqueFields();
  if (!allPredsAvailable) {
    this.estimatedCardinality.putAll(hints.getDistinctCounts());
    this.estimatedNumRecords=0;
    int count=0;
    for (    Entry<FieldSet,Long> cardinality : hints.getDistinctCounts().entrySet()) {
      float avgNumValues=hints.getAvgNumRecordsPerDistinctFields(cardinality.getKey());
      if (avgNumValues != -1) {
        this.estimatedNumRecords+=cardinality.getValue() * avgNumValues;
        count++;
      }
    }
    if (count > 0) {
      this.estimatedNumRecords=(this.estimatedNumRecords / count) >= 1 ? (this.estimatedNumRecords / count) : 1;
    }
 else {
      this.estimatedNumRecords=-1;
    }
    if (this.estimatedNumRecords != -1 && hints.getAvgBytesPerRecord() != -1) {
      this.estimatedOutputSize=(this.estimatedNumRecords * hints.getAvgBytesPerRecord() >= 1) ? (long)(this.estimatedNumRecords * hints.getAvgBytesPerRecord()) : 1;
    }
  }
 else {
    boolean outputCardEstimated=true;
    this.estimatedNumRecords=0;
    int count=0;
    for (    Entry<FieldSet,Long> cardinality : hints.getDistinctCounts().entrySet()) {
      float avgNumValues=hints.getAvgNumRecordsPerDistinctFields(cardinality.getKey());
      if (avgNumValues != -1) {
        this.estimatedNumRecords+=cardinality.getValue() * avgNumValues;
        count++;
      }
    }
    if (count > 0) {
      this.estimatedNumRecords=(this.estimatedNumRecords / count) >= 1 ? (this.estimatedNumRecords / count) : 1;
    }
 else {
      this.estimatedNumRecords=this.computeNumberOfStubCalls();
      if (hints.getAvgRecordsEmittedPerStubCall() != -1.0 && this.computeNumberOfStubCalls() != -1) {
        this.estimatedNumRecords=(this.computeNumberOfStubCalls() * hints.getAvgRecordsEmittedPerStubCall() >= 1) ? (long)(this.computeNumberOfStubCalls() * hints.getAvgRecordsEmittedPerStubCall()) : 1;
      }
 else {
        outputCardEstimated=false;
      }
    }
    this.estimatedCardinality.putAll(hints.getDistinctCounts());
    if (this.getUniqueFields() != null) {
      for (      FieldSet uniqueFieldSet : this.uniqueFields) {
        if (this.estimatedCardinality.get(uniqueFieldSet) == null) {
          this.estimatedCardinality.put(uniqueFieldSet,this.estimatedNumRecords);
        }
      }
    }
    for (int input=0; input < getIncomingConnections().size(); input++) {
      int[] keyColumns;
      if ((keyColumns=getConstantKeySet(input)) != null) {
        long estimatedKeyCardinality;
        if (hints.getAvgRecordsEmittedPerStubCall() < 1.0) {
          double probToKeepKey=1.0 - Math.pow((1.0 - hints.getAvgRecordsEmittedPerStubCall()),this.computeStubCallsPerProcessedKey());
          estimatedKeyCardinality=(this.computeNumberOfProcessedKeys() * probToKeepKey >= 1) ? (long)(this.computeNumberOfProcessedKeys() * probToKeepKey) : 1;
        }
 else {
          estimatedKeyCardinality=this.computeNumberOfProcessedKeys();
        }
        FieldSet fieldSet=new FieldSet(keyColumns);
        if (estimatedCardinality.get(fieldSet) == null) {
          estimatedCardinality.put(fieldSet,estimatedKeyCardinality);
        }
      }
    }
    if (this.estimatedNumRecords != -1) {
      for (      Entry<FieldSet,Float> avgNumValues : hints.getAvgNumRecordsPerDistinctFields().entrySet()) {
        if (estimatedCardinality.get(avgNumValues.getKey()) == null) {
          long estimatedCard=(this.estimatedNumRecords / avgNumValues.getValue() >= 1) ? (long)(this.estimatedNumRecords / avgNumValues.getValue()) : 1;
          estimatedCardinality.put(avgNumValues.getKey(),estimatedCard);
        }
      }
    }
    if (!outputCardEstimated) {
      long newEstimatedNumRecords=0;
      count=0;
      for (      Entry<FieldSet,Long> cardinality : estimatedCardinality.entrySet()) {
        float avgNumValues=hints.getAvgNumRecordsPerDistinctFields(cardinality.getKey());
        if (avgNumValues != -1) {
          newEstimatedNumRecords+=cardinality.getValue() * avgNumValues;
          count++;
        }
      }
      if (count > 0) {
        newEstimatedNumRecords=(newEstimatedNumRecords / count) >= 1 ? (newEstimatedNumRecords / count) : 1;
      }
    }
    double estAvgRecordWidth=this.computeAverageRecordWidth();
    if (this.estimatedNumRecords != -1 && estAvgRecordWidth != -1) {
      this.estimatedOutputSize=(this.estimatedNumRecords * estAvgRecordWidth) >= 1 ? (long)(this.estimatedNumRecords * estAvgRecordWidth) : 1;
    }
    for (    Entry<FieldSet,Long> cardinality : this.estimatedCardinality.entrySet()) {
      if (cardinality.getValue() > this.estimatedNumRecords) {
        cardinality.setValue(this.estimatedNumRecords);
      }
    }
  }
}