{"record":{"id":"d9cac610cbe36a42","repo":"apache/druid","slug":"no-statistics-needed-for-stage-d","errorCode":null,"errorMessage":"No statistics needed for stage[%d]","messagePattern":"No statistics needed for stage\\[(.+?)\\]","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"multi-stage-query/src/main/java/org/apache/druid/msq/kernel/StageDefinition.java","lineNumber":423,"sourceCode":"          shuffleSpec.kind(),\n          getStageNumber()\n      );\n    } else if (mustGatherResultKeyStatistics() && collector == null) {\n      throw new ISE(\"Statistics required, but not gathered for stage[%d]\", getStageNumber());\n    } else if (!mustGatherResultKeyStatistics() && collector != null) {\n      throw new ISE(\"Statistics gathered, but not required for stage[%d]\", getStageNumber());\n    } else {\n      return ((GlobalSortShuffleSpec) shuffleSpec).generatePartitionsForGlobalSort(collector, maxPartitions);\n    }\n  }\n\n  public ClusterByStatisticsCollector createResultKeyStatisticsCollector(\n      final FrameType frameType,\n      final int maxRetainedBytes\n  )\n  {\n    if (!mustGatherResultKeyStatistics()) {\n      throw new ISE(\"No statistics needed for stage[%d]\", getStageNumber());\n    }\n\n    return ClusterByStatisticsCollectorImpl.create(\n        shuffleSpec.clusterBy(),\n        signature,\n        frameType,\n        maxRetainedBytes,\n        Limits.MAX_PARTITION_BUCKETS,\n        ((GlobalSortShuffleSpec) shuffleSpec).doesAggregate(),\n        shuffleCheckHasMultipleValues\n    );\n  }\n\n  /**\n   * Create the {@link FrameWriterFactory} that must be used by {@link #getProcessor()}.\n   *\n   * Calls {@link MemoryAllocatorFactory#newAllocator()} for each frame.\n   */","sourceCodeStart":405,"sourceCodeEnd":441,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/multi-stage-query/src/main/java/org/apache/druid/msq/kernel/StageDefinition.java#L405-L441","documentation":"createResultKeyStatisticsCollector() builds the ClusterByStatisticsCollector used to track shuffle key distributions. Calling it on a stage whose mustGatherResultKeyStatistics() is false is a programming error — no statistics exist to collect — so ISE(\"No statistics needed for stage[%d]\") is thrown.","triggerScenarios":"Calling createResultKeyStatisticsCollector(frameType, maxRetainedBytes) on a non-global-sort stage (shuffleSpec null or kind != GLOBAL_SORT), e.g. from gatherResultKeyStatistics or worker code iterating all stages.","commonSituations":"Worker code that unconditionally creates a statistics collector for every stage, or tooling that assumes all shuffle stages gather statistics.","solutions":["Check stageDefinition.mustGatherResultKeyStatistics() before calling createResultKeyStatisticsCollector","Skip statistics collection for stages whose shuffle spec is not GLOBAL_SORT","If statistics are expected, correct the shuffle spec assigned to the stage"],"exampleFix":"// before\nClusterByStatisticsCollector c = stageDef.createResultKeyStatisticsCollector(frameType, maxBytes);\n// after\nif (stageDef.mustGatherResultKeyStatistics()) {\n  ClusterByStatisticsCollector c = stageDef.createResultKeyStatisticsCollector(frameType, maxBytes);\n}","handlingStrategy":"type-guard","validationCode":"if (!stageDef.mustGatherResultKeyStatistics()) {\n  return; // skip collector creation\n}","typeGuard":"boolean needsStats(StageDefinition d) { return d.mustGatherResultKeyStatistics(); }","tryCatchPattern":"try { c = d.createResultKeyStatisticsCollector(ft, bytes); } catch (IllegalStateException e) { c = null; }","preventionTips":["Check mustGatherResultKeyStatistics() before creating collectors","Only gather statistics for GLOBAL_SORT stages","Unify stats setup in one helper that performs the check"],"tags":["msq","statistics","stage-definition"],"backgroundTag":"unsupported-operation","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}