apache/druid · error · java.lang.UnsupportedOperationException

CardinalityAggregator does not support getLong()

Error message

CardinalityAggregator does not support getLong()

What it means

CardinalityAggregator computes approximate distinct counts as a HyperLogLog sketch; it exposes get() (the sketch object) but its getLong() always throws UnsupportedOperationException. This fires when the query framework attempts numeric finalization of a cardinality aggregate — e.g. coercing a COUNT(DISTINCT x) result to BIGINT inside the aggregator instead of post-finalization.

Source

Thrown at processing/src/main/java/org/apache/druid/query/aggregation/cardinality/CardinalityAggregator.java:112

  @Override
  public synchronized Object get()
  {
    // Must make a new collector duplicating the underlying buffer to ensure the object from "get" is usable
    // in a thread-safe manner.
    return HyperLogLogCollector.makeCollectorSharingStorage(collector);
  }

  @Override
  public float getFloat()
  {
    throw new UnsupportedOperationException("CardinalityAggregator does not support getFloat()");
  }

  @Override
  public long getLong()
  {
    throw new UnsupportedOperationException("CardinalityAggregator does not support getLong()");
  }

  @Override
  public double getDouble()
  {
    throw new UnsupportedOperationException("CardinalityAggregator does not support getDouble()");
  }

  @Override
  public void close()
  {
    // no resources to cleanup
  }
}

View on GitHub (pinned to 9b90983fd2)

Solutions

  1. Let the sketch be finalized via get() and convert to a count afterwards (the normal post-aggregation path); don't read it as a long in-flight.
  2. Use APPROX_COUNT_DISTINCT_DS or numeric-typed distinct approximations that support direct values if a number is required.
  3. Check for custom post-aggregators that call getLong() on the raw cardinality aggregate.

Example fix

// before
long v = aggregator.getLong();
// after
Object v = aggregator.get(); // HyperLogLogCollector / estimate
Defensive patterns

Strategy: validation

Validate before calling

if (!(aggFactory instanceof CardinalityAggregatorFactory)) { long v = aggregator.getLong(); }

Type guard

if (aggregator instanceof CardinalityAggregator) {
    Object v = aggregator.get();
} else {
    long v = aggregator.getLong();
}

Try / catch

try {
    v = aggregator.getLong();
} catch (UnsupportedOperationException e) {
    v = 0L; // or extract estimate from get()
}

Prevention

When it happens

Trigger: Calling aggregator.getLong() on a CardinalityAggregator when the caller assumes a long-typed aggregation result.

Common situations: Numeric extraction of cardinality results in custom code or rollup numeric columns; using cardinality where a long aggregator was expected.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07). Data as JSON: /api/errors/2afec2b59b1293b4. Report an issue: GitHub.