{"record":{"id":"365d14ee2ac6b9fb","repo":"apache/druid","slug":"this-method-is-not-supported-use-withdatasources","errorCode":null,"errorMessage":"This method is not supported. Use withDataSources instead!","messagePattern":"This method is not supported\\. Use withDataSources instead!","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/union/UnionQuery.java","lineNumber":190,"sourceCode":"    return context().getString(BaseQuery.QUERY_ID);\n  }\n\n  @Override\n  public Query<Object> withSubQueryId(String subQueryId)\n  {\n    return withOverriddenContext(ImmutableMap.of(BaseQuery.SUB_QUERY_ID, subQueryId));\n  }\n\n  @Override\n  public String getSubQueryId()\n  {\n    return context().getString(BaseQuery.SUB_QUERY_ID);\n  }\n\n  @Override\n  public Query<Object> withDataSource(DataSource dataSource)\n  {\n    throw new RuntimeException(\"This method is not supported. Use withDataSources instead!\");\n  }\n\n  public Query<Object> withDataSources(List<DataSource> children)\n  {\n    Preconditions.checkArgument(queries.size() == children.size(), \"Number of children must match number of queries\");\n    List<Query<?>> newQueries = new ArrayList<>();\n    for (int i = 0; i < queries.size(); i++) {\n      newQueries.add(queries.get(i).withDataSource(children.get(i)));\n    }\n    return new UnionQuery(newQueries, context);\n  }\n\n  List<Query<?>> mapQueries(Function<Query<?>, Query<?>> mapFn)\n  {\n    List<Query<?>> newQueries = new ArrayList<>();\n    for (Query<?> query : queries) {\n      newQueries.add(mapFn.apply(query));\n    }","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/union/UnionQuery.java#L172-L208","documentation":"UnionQuery represents a native query union where each sub-query has its own DataSource. Because a union cannot be represented as a single DataSource, the generic Query.withDataSource(DataSource) contract is intentionally unimplemented and throws this RuntimeException; callers must use UnionQuery.withDataSources(List<DataSource>), which replaces each child query's data source with the corresponding entry of the list.","triggerScenarios":"Calling Query.withDataSource(...) on a Query that is actually a UnionQuery — e.g. generic query-rewriting/serialization code (BaseQuery.updateQueryDataSource, tooling, query cloning) that treats all queries uniformly and passes a single DataSource.","commonSituations":"Framework or extension code that rewrites queries (e.g. swapping tables, applying query-level datasource migration during upgrades) hits a union at runtime; unit tests invoking the generic Query API against unions.","solutions":["Replace the call with withDataSources(List<DataSource>), supplying one DataSource per sub-query in the same order.","Branch on query type before rewriting: if (query instanceof UnionQuery) call withDataSources with a list, otherwise withDataSource.","Check the number of data sources equals queries.size(); withDataSources enforces this with a Preconditions check.","If the union was produced by the SQL layer, re-generate the native query rather than mutating its data source."],"exampleFix":"// before\nQuery<?> rewritten = query.withDataSource(newDataSource);\n// after\nQuery<?> rewritten;\nif (query instanceof UnionQuery) {\n  List<DataSource> perChild = ((UnionQuery) query).getQueries().stream()\n      .map(q -> newDataSource).collect(Collectors.toList());\n  rewritten = query.withDataSources(perChild);\n} else {\n  rewritten = query.withDataSource(newDataSource);\n}","handlingStrategy":"type-guard","validationCode":"if (query instanceof UnionQuery uq) {\n  Preconditions.checkState(uq.getQueries().size() == dataSources.size(),\n      \"need one DataSource per sub-query\");\n}","typeGuard":"boolean isUnion(Query<?> q) { return q instanceof UnionQuery; }","tryCatchPattern":"try {\n  return query.withDataSource(ds);\n} catch (RuntimeException e) {\n  if (query instanceof UnionQuery) {\n    return handleUnion((UnionQuery) query, ds);\n  }\n  throw e;\n}","preventionTips":["Check query type before generic Query API mutation calls","Always supply one DataSource per sub-query for unions","Prefer withDataSources for union rewrite code paths","Add unit tests that run query-rewrite logic against UnionQuery instances"],"tags":["java","query-processing","unsupported-operation","union-query"],"backgroundTag":"unsupported-operation","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}