Class CountAggregate

  • All Implemented Interfaces:
    java.lang.Cloneable
    Direct Known Subclasses:
    Count, CountDistinct

    public class CountAggregate
    extends AggregateFunction
    This aggregate function can be used to compute both COUNT(...) and COUNT(DISTINCT ...) aggregate functions. In addition, the COUNT(DISTINCT ...) operation can consume either sorted or unsorted values to compute the distinct count.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int count
      Contains the current number of items
      private boolean distinct
      Boolean that is true if we are counting distinct values
      private java.lang.Object lastValueSeen
      Stores the most recently seen object
      private boolean sortedInputs
      True if the inputs are sorted.
      private java.util.HashSet<java.lang.Object> valuesSeen
      Contains all of the values seen so far, used for COUNT DISTINCT to keep track of distinct values
    • Constructor Summary

      Constructors 
      Constructor Description
      CountAggregate​(boolean distinct, boolean sortedInputs)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addValue​(java.lang.Object value)
      Adds a value to the aggregate function so that it can update its internal state.
      void clearResult()
      Clears the aggregate function's current state so that the object can be reused to compute an aggregate on another set of input values.
      java.lang.Object getResult()
      Returns the aggregated result computed for this aggregate function.
      ColumnType getReturnType​(java.util.List<Expression> args, Schema schema)
      Returns the column type of the resulting column after applying the function.
      • Methods inherited from class edu.caltech.nanodb.functions.Function

        clone
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • count

        private int count
        Contains the current number of items
      • valuesSeen

        private java.util.HashSet<java.lang.Object> valuesSeen
        Contains all of the values seen so far, used for COUNT DISTINCT to keep track of distinct values
      • lastValueSeen

        private java.lang.Object lastValueSeen
        Stores the most recently seen object
      • distinct

        private boolean distinct
        Boolean that is true if we are counting distinct values
      • sortedInputs

        private boolean sortedInputs
        True if the inputs are sorted. We use this to count distinct values faster since if the inputs are sorted, we do not need to add them to a set.
    • Constructor Detail

      • CountAggregate

        public CountAggregate​(boolean distinct,
                              boolean sortedInputs)
    • Method Detail

      • clearResult

        public void clearResult()
        Description copied from class: AggregateFunction
        Clears the aggregate function's current state so that the object can be reused to compute an aggregate on another set of input values.
        Specified by:
        clearResult in class AggregateFunction
      • addValue

        public void addValue​(java.lang.Object value)
        Description copied from class: AggregateFunction
        Adds a value to the aggregate function so that it can update its internal state. Generally, aggregate functions ignore null inputs (which represent SQL NULL values) when computing their results.
        Specified by:
        addValue in class AggregateFunction
        Parameters:
        value - the value to add to the aggregate function
      • getResult

        public java.lang.Object getResult()
        Description copied from class: AggregateFunction
        Returns the aggregated result computed for this aggregate function. Generally, if aggregate functions receive no non-null inputs then they should produce a null result. (COUNT is an exception to this rule, producing 0 in that case.)
        Specified by:
        getResult in class AggregateFunction
        Returns:
        the result of the aggregate computation.
      • getReturnType

        public ColumnType getReturnType​(java.util.List<Expression> args,
                                        Schema schema)
        Description copied from class: ScalarFunction
        Returns the column type of the resulting column after applying the function. This generally depends on the column type of the inputs.
        Specified by:
        getReturnType in class ScalarFunction
        Parameters:
        args - the arguments to the function call
        schema - the schema of the table
        Returns:
        the column type of the resulting column