{"record":{"id":"df37fe04fa9a9975","repo":"apache/druid","slug":"could-not-find-the-dimension-spec-for-ordering-col","errorCode":null,"errorMessage":"Could not find the dimension spec for ordering column %s","messagePattern":"Could not find the dimension spec for ordering column (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/groupby/orderby/DefaultLimitSpec.java","lineNumber":392,"sourceCode":"    for (OrderByColumnSpec columnSpec : columns) {\n      String columnName = columnSpec.getDimension();\n      Ordering<ResultRow> nextOrdering = null;\n\n      final int columnIndex = rowSignature.indexOf(columnName);\n\n      if (columnIndex >= 0) {\n        if (postAggregatorsMap.containsKey(columnName)) {\n          //noinspection unchecked\n          nextOrdering = metricOrdering(columnIndex, postAggregatorsMap.get(columnName).getComparator());\n        } else if (aggregatorsMap.containsKey(columnName)) {\n          //noinspection unchecked\n          nextOrdering = metricOrdering(columnIndex, aggregatorsMap.get(columnName).getComparator());\n        } else if (dimensionsMap.containsKey(columnName)) {\n          Optional<DimensionSpec> dimensionSpec = dimensions.stream()\n                                                            .filter(ds -> ds.getOutputName().equals(columnName))\n                                                            .findFirst();\n          if (!dimensionSpec.isPresent()) {\n            throw new ISE(\"Could not find the dimension spec for ordering column %s\", columnName);\n          }\n          nextOrdering = dimensionOrdering(\n              columnIndex,\n              dimensionSpec.get().getOutputType(),\n              columnSpec.getDimensionComparator()\n          );\n        }\n      }\n\n      if (nextOrdering == null) {\n        throw new ISE(\"Unknown column in order clause[%s]\", columnSpec);\n      }\n\n      if (columnSpec.getDirection() == OrderByColumnSpec.Direction.DESCENDING) {\n        nextOrdering = nextOrdering.reverse();\n      }\n\n      ordering = ordering == null ? nextOrdering : ordering.compound(nextOrdering);","sourceCodeStart":374,"sourceCodeEnd":410,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/groupby/orderby/DefaultLimitSpec.java#L374-L410","documentation":"DefaultLimitSpec.makeComparator builds a comparator chain for the limit spec. When an ordering column is a dimension (found in dimensionsMap), it filters the dimensions list for the spec with the matching outputName; if dimensionsMap contains the name but the dimensions stream yields no spec (inconsistent map/list state), an IllegalStateException is thrown. This is an internal consistency failure between the derived map and the dimension spec list.","triggerScenarios":"makeComparator encounters columnName present in dimensionsMap but dimensions.stream().filter(ds -> ds.getOutputName().equals(columnName)).findFirst() is empty — e.g. duplicate dimension outputNames colliding on map keys while the filter unexpectedly fails, or API misuse producing divergent map/list inputs.","commonSituations":"Programmatic construction of DefaultLimitSpec.sortingComparatorWithAggregators with mismatched dimensions/aggregators collections; duplicate output names; engine-internal state divergence after query rewrite.","solutions":["Ensure each ordering column appears in exactly one dimension spec with a unique outputName.","Rebuild the query spec so dimensionsMap and dimensions are derived from the same list (use the provided builder helpers rather than hand-rolled maps).","If this arises during normal Druid usage (not custom code), it indicates a bug — collect the query spec and file an issue."],"exampleFix":"// before: two dimensions with same outputName \"user\"\n// after: deduplicate\nList<DimensionSpec> distinct = dims.stream().distinct().collect(Collectors.toList());\nreturn DefaultLimitSpec.sortingComparatorWithAggregators(orderings, distinct, aggregators);","handlingStrategy":"validation","validationCode":"long distinctNames = dimensions.stream().map(DimensionSpec::getOutputName).distinct().count();\nif (distinctNames != dimensions.size()) {\n  throw new IllegalArgumentException(\"Duplicate dimension outputName detected\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep dimension outputNames unique across the query spec","Always derive dimension/aggregator maps from the same list used for the limit spec","Avoid hand-building the map/list inputs to sortingComparatorWithAggregators"],"tags":["druid","limit-spec","ordering","internal-state"],"backgroundTag":"internal-invariant-violation","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}