apache/druid · error · IllegalStateException

Cannot work with segment of type

Error message

Cannot work with segment of type[%s]

What it means

SegmentToRowsAndColumnsOperator converts a Segment into RowsAndColumns, either directly (QuerySegment/RowsAndColumns types) or via Segment.as(RowsAndColumns.class). If the segment cannot be viewed as RowsAndColumns at all, the operator cannot process it and throws this ISE.

Solutions

  1. Ensure the query reads from a supported datasource (inline, table with standard Druid segments) for window operator queries
  2. Upgrade or replace the custom Segment implementation to expose RowsAndColumns via Segment.as(...)
  3. Log segment.getClass() and confirm which segment type is being passed; fix the upstream source accordingly

Example fix

// before
class MySegment implements Segment { /* no as(RowsAndColumns.class) */ }
// after
class MySegment implements Segment {
  @Override public <T> T as(Class<T> clazz) {
    if (clazz.equals(RowsAndColumns.class)) return clazz.cast(myRowsAndColumns);
    return null;
  }
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (segment.as(RowsAndColumns.class) == null) {
  throw new IllegalArgumentException("Segment " + segment.getId() + " cannot be viewed as RowsAndColumns");
}

Type guard

RowsAndColumns asRowsAndColumns(Segment s) {
  if (s instanceof RowsAndColumns) return (RowsAndColumns) s;
  return s.as(RowsAndColumns.class);
}

Try / catch

try {
  operator.go(receiver, cont);
} catch (ISE e) {
  if (e.getMessage().startsWith("Cannot work with segment of type")) {
    // route the segment through a supported query type or materialize it first
  } else throw e;
}

Prevention

When it happens

Trigger: Running a window operator query over a data source whose segments are neither QuerySegment/Shifty-backed with RowsAndColumns support nor expose an as(RowsAndColumns.class) adapter — e.g. a legacy or custom Segment implementation.

Common situations: Custom or third-party segment types fed into operator (window) queries, misconfigured data sources producing non-materializable segments, or version mismatches where a segment type lost its RowsAndColumns adapter.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at processing/src/main/java/org/apache/druid/query/operator/SegmentToRowsAndColumnsOperator.java:58

    this.segment = segment;
  }

  @Override
  public Closeable goOrContinue(Closeable continuation, Receiver receiver)
  {
    try (final CloseableShapeshifter shifty = segment.as(CloseableShapeshifter.class)) {
      if (shifty == null) {
        throw DruidException.defensive("Segment [%s] cannot shapeshift", segment.getDebugString());
      }
      RowsAndColumns rac;
      if (shifty instanceof RowsAndColumns) {
        rac = (RowsAndColumns) shifty;
      } else {
        rac = shifty.as(RowsAndColumns.class);
      }

      if (rac == null) {
        throw new ISE("Cannot work with segment of type[%s]", segment.getClass());
      }

      // After pushing in a single object, we are done, so ignore the signal and call completed()
      receiver.push(rac);
      receiver.completed();
    }
    catch (IOException e) {
      throw new RE(e, "Problem closing resources for segment[%s]", segment.getId());
    }
    return null;
  }
}

View on GitHub (pinned to 9b90983fd2)