apache/druid · error · IllegalStateException

Cannot return null value as long

Error message

Cannot return null value as long

What it means

NilColumnValueSelector.getLong() throws IllegalStateException since all values in the column are null and long primitives cannot express null. The selector intentionally fails fast instead of silently returning 0. Check null status before primitive access.

Solutions

  1. Use isNull()/getObject() before calling getLong()
  2. Confirm the column exists (SegmentMetadataQuery or datasource schema)
  3. Apply SQL COALESCE or query defaults for missing columns
  4. Set default values for long fields in the ingestion spec

Example fix

// before
long l = selector.getLong();
// after
long l = selector.isNull() ? 0L : ((Number) selector.getObject()).longValue();
Defensive patterns

Strategy: type-guard

Validate before calling

if (selector.isNull()) { /* handle missing */ }

Type guard

Long safeGetLong(ColumnValueSelector<?> s) { return s.isNull() ? null : s.getLong(); }

Try / catch

try {
  long l = selector.getLong();
} catch (IllegalStateException e) {
  long l = 0L;
}

Prevention

When it happens

Trigger: Calling getLong() on a selector for a fully-null long column — e.g. a missing metric column in a segment or rows omitting the field.

Common situations: Queries against datasources where the long metric is missing in some segments; row-based ingestion with absent fields; typo'd column names in queries.

Related errors


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

Appendix: source

Thrown at processing/src/main/java/org/apache/druid/segment/NilColumnValueSelector.java:68

    throw new IllegalStateException("Cannot return null value as double");
  }

  /**
   * always throws an exception, all values in this column are null
   */
  @Override
  public float getFloat()
  {
    throw new IllegalStateException("Cannot return null value as float");
  }

  /**
   * always throws an exception, all values in this column are null
   */
  @Override
  public long getLong()
  {
    throw new IllegalStateException("Cannot return null value as long");
  }

  /**
   * Always returns null.
   */
  @Nullable
  @Override
  public Object getObject()
  {
    return null;
  }

  /**
   * Returns Object.class.
   */
  @Override
  public Class classOfObject()
  {

View on GitHub (pinned to 9b90983fd2)