Example #1
0
  /**
   * Create a new TableStats object, that keeps track of statistics on each column of a table
   *
   * @param tableid The table over which to compute statistics
   * @param ioCostPerPage The cost per page of IO. This doesn't differentiate between
   *     sequential-scan IO and disk seeks.
   */
  public TableStats(int tableid, int ioCostPerPage) {
    // For this function, we use the DbFile for the table in question,
    // then scan through its tuples and calculate the values that you
    // to build the histograms.

    // TODO: Fill out the rest of the constructor.
    // Feel free to change anything already written, it's only a guideline

    this.ioCostPerPage = ioCostPerPage;
    DbFile file = Database.getCatalog().getDbFile(tableid);
    tupleDesc = file.getTupleDesc();
    numPages = ((HeapFile) file).numPages();
    numTuples = 0;

    int numFields = tupleDesc.numFields();

    // TODO: what goes here?
    statistics = new ArrayList<Object>();

    for (int i = 0; i < numFields; i++) {
      if (Type.INT_TYPE.equals(tupleDesc.getFieldType(i))) {
        statistics.add(new IntStatistics(NUM_HIST_BINS));
      } else {
        statistics.add(new StringHistogram(NUM_HIST_BINS));
      }
    }

    final DbFileIterator iter = file.iterator(null);
    try {
      iter.open();

      while (iter.hasNext()) {
        Tuple t = iter.next();
        numTuples++;

        // TODO: and here?
        for (int i = 0; i < numFields; i++) {
          if (Type.INT_TYPE.equals(tupleDesc.getFieldType(i))) {
            ((IntStatistics) statistics.get(i)).addValue(((IntField) t.getField(i)).getValue());
          } else {
            ((StringHistogram) statistics.get(i))
                .addValue(((StringField) t.getField(i)).getValue());
          }
        }
      }
      iter.close();
    } catch (DbException e) {
      e.printStackTrace();
    } catch (TransactionAbortedException e) {
      e.printStackTrace();
    }
  }
 /**
  * Merge a new tuple into the aggregate, grouping as indicated in the constructor
  *
  * @param tup the Tuple containing an aggregate field and a group-by field
  */
 public void mergeTupleIntoGroup(Tuple tup) {
   if (isGrouping()) {
     Field gbValue = tup.getField(this.gbField);
     if (this.tupleStorage.containsKey(gbValue)) {
       this.tupleStorage.get(gbValue).add((IntField) tup.getField(this.aField));
     } else {
       this.tupleStorage.put(gbValue, new ArrayList<IntField>());
       this.tupleStorage.get(gbValue).add((IntField) tup.getField(this.aField));
     }
   } else {
     this.tupleStorageNoGrouping.add((IntField) tup.getField(this.aField));
   }
 }
  /** Unit test for Tuple.getField() and Tuple.setField() */
  @Test
  public void modifyFields() {
    TupleDesc td = Utility.getTupleDesc(2);

    Tuple tup = new Tuple(td);
    tup.setField(0, new IntField(-1));
    tup.setField(1, new IntField(0));

    assertEquals(new IntField(-1), tup.getField(0));
    assertEquals(new IntField(0), tup.getField(1));

    tup.setField(0, new IntField(1));
    tup.setField(1, new IntField(37));

    assertEquals(new IntField(1), tup.getField(0));
    assertEquals(new IntField(37), tup.getField(1));
  }
 /**
  * Merge a new tuple into the aggregate, grouping as indicated in the constructor
  *
  * @param tup the Tuple containing an aggregate field and a group-by field
  */
 public void merge(Tuple tup) {
   if (_gb == NO_GROUPING) {
     _count++;
   } else {
     Field f = tup.getField(_gb);
     Integer c = _counts.get(f);
     if (c == null) c = 1;
     else c++;
     _counts.put(f, c);
   }
 }