apache/druid · error · IllegalStateException

Union datasource with non-leaf inputs is not supported

Error message

Union datasource with non-leaf inputs is not supported [%s]!

What it means

UnionDataSource.createSegmentMapFunction only supports unions of leaf datasources (typically tables); if any input still has children (e.g. a nested query or another wrapped datasource), Druid throws ISE because segment mapping for nested inputs is not implemented. Call Identity mapping only when every input is a leaf.

Solutions

  1. Flatten the query so union inputs are plain TableDataSources
  2. Rewrite the query to avoid unions of subqueries (e.g. use UNION ALL at the SQL level with table inputs)
  3. If building programmatically, extract base tables from nested datasources before wrapping in a union

Example fix

// before
new UnionDataSource(List.of(query1.getDataSource(), tableDs)); // query datasource has children
// after
List<DataSource> leaves = List.of(((QueryDataSource) query1.getDataSource()).getQuery().getDataSource(), tableDs);
new UnionDataSource(leaves); // all leaf
Defensive patterns

Strategy: validation

Validate before calling

boolean allLeaf = unionDs.getChildren().stream().allMatch(d -> d.getChildren().isEmpty());
if (!allLeaf) {
  throw new UnsupportedOperationException("Union inputs must be leaf datasources");
}

Type guard

boolean isLeaf(DataSource ds) {
  return ds.getChildren().isEmpty();
}

Try / catch

try {
  unionDs.createSegmentMapFunction(query);
} catch (IllegalStateException e) {
  // flatten the datasource tree before retrying
}

Prevention

When it happens

Trigger: Building a union whose members are themselves non-leaf datasources (queries, joins, nested unions) and then executing a query against it, hitting createSegmentMapFunction.

Common situations: SQL features generating unions of subqueries that were not flattened to tables; programmatic query building nesting datasources inside a union; older planner paths producing non-flattened unions.

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/7e42272f4d35ad2c. Report an issue: GitHub.

Appendix: source

Thrown at processing/src/main/java/org/apache/druid/query/UnionDataSource.java:132

  @Override
  public boolean isGlobal()
  {
    return dataSources.stream().allMatch(DataSource::isGlobal);
  }

  @Override
  public boolean isProcessable()
  {
    return dataSources.stream().allMatch(DataSource::isProcessable);
  }

  @Override
  public SegmentMapFunction createSegmentMapFunction(Query query)
  {
    for (DataSource dataSource : dataSources) {
      if (!dataSource.getChildren().isEmpty()) {
        throw new ISE("Union datasource with non-leaf inputs is not supported [%s]!", dataSources);
      }
    }
    return SegmentMapFunction.IDENTITY;
  }

  @Override
  public byte[] getCacheKey()
  {
    return null;
  }

  @Override
  public boolean equals(Object o)
  {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {

View on GitHub (pinned to 9b90983fd2)