Class ApproxCountDistinct


public class ApproxCountDistinct extends AggregateFunction
APPROX_COUNT_DISTINCT(expr) — an approximate count of the distinct non-NULL values in the group, estimated with the HllAccumulator HyperLogLog sketch (~0.8% standard error, near-exact at low cardinality). NULLs are ignored, and a group with no non-NULL value counts 0. HLL names the same registered object.
  • Constructor Details

    • ApproxCountDistinct

      public ApproxCountDistinct()
      Registers the aggregate as APPROX_COUNT_DISTINCT returning BIGINT.
  • Method Details

    • createAccumulator

      public AggregateFunction.Accumulator createAccumulator()
      Description copied from class: AggregateFunction
      Creates the fresh, empty per-group state for one evaluation of this aggregate; the executor makes one per group (or per window partition) and never shares them.
      Specified by:
      createAccumulator in class AggregateFunction
      Returns:
      a new accumulator starting from this aggregate's empty-group state
    • geoRejection

      public SemiStructuredRejection geoRejection(int position)
      The sketch does not accumulate a GEOSPATIAL value, where it accumulates an OBJECT. Live APPROX_COUNT_DISTINCT(o) returns 2 while APPROX_COUNT_DISTINCT(g) and its HLL(g) synonym — the same registered object here — are argument-type errors (SQLSTATE 42P13). COUNT(DISTINCT g) is NOT affected and returned 2 live: only the approximate counter refuses.

      Live names 'HLL_ACCUMULATE' rather than the function written, an internal desugaring Frostlake does not reproduce for the same reason it reports 'AVG' where live reports 'SUM' — the engine has no such plan to name.

      Overrides:
      geoRejection in class BuiltInFunction
      Parameters:
      position - the zero-based argument position being asked about
      Returns:
      the refusal this position gives a GEOGRAPHY or GEOMETRY value; by default, the union of the two semi-structured answers
    • evaluate

      public Object evaluate(List<Object> args)
      Description copied from class: BuiltInFunction
      Computes the function's result over its already-evaluated argument values. A SQL NULL argument arrives as Java null, and returning null makes the SQL result NULL.
      Specified by:
      evaluate in class BuiltInFunction
      Parameters:
      args - the argument values, in call order
      Returns:
      the function's value for these arguments, or null for SQL NULL
    • getMinArgCount

      public int getMinArgCount()
      Description copied from class: BuiltInFunction
      The fewest arguments a call to this function may pass; fewer is an argument-count error.
      Specified by:
      getMinArgCount in class BuiltInFunction
      Returns:
      the minimum accepted argument count
    • getMaxArgCount

      public int getMaxArgCount()
      Documented `APPROX_COUNT_DISTINCT([DISTINCT] expr [, expr ...])`: the tuples are what is counted.
      Specified by:
      getMaxArgCount in class BuiltInFunction
      Returns:
      the maximum accepted argument count