{"record":{"id":"6868fd0e964cd7df","repo":"apache/druid","slug":"the-time-column-must-be-selected-if-the-results","errorCode":null,"errorMessage":"The __time column must be selected if the results are time-ordered.","messagePattern":"The __time column must be selected if the results are time-ordered\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/scan/ScanQuery.java","lineNumber":184,"sourceCode":"            columnTypes.size()\n        );\n      }\n    }\n\n    final Pair<List<OrderBy>, Order> ordering = verifyAndReconcileOrdering(orderBysFromUser, orderFromUser);\n    this.orderBys = Preconditions.checkNotNull(ordering.lhs);\n    this.timeOrder = ordering.rhs;\n\n    if (this.columns != null && this.columns.size() > 0) {\n      // Validate orderBy. (Cannot validate when signature is empty, since that means \"discover at runtime\".)\n\n      for (final OrderBy orderByColumn : this.orderBys) {\n        if (!this.columns.contains(orderByColumn.getColumnName())) {\n          // Error message depends on how the user originally specified ordering.\n          if (orderBysFromUser != null) {\n            throw new IAE(\"Column [%s] from 'orderBy' must also appear in 'columns'.\", orderByColumn.getColumnName());\n          } else {\n            throw new IllegalArgumentException(\"The __time column must be selected if the results are time-ordered.\");\n          }\n        }\n      }\n    }\n\n    this.maxRowsQueuedForOrdering = validateAndGetMaxRowsQueuedForOrdering();\n    this.maxSegmentPartitionsOrderedInMemory = validateAndGetMaxSegmentPartitionsOrderedInMemory();\n  }\n\n  /**\n   * Verifies that the ordering of a query is solely determined by {@link #getTimeOrder()}. Required to actually\n   * execute queries, because {@link #getOrderBys()} is not yet understood by the query engines.\n   *\n   * @throws IllegalStateException if the ordering is not solely determined by {@link #getTimeOrder()}\n   */\n  public static void verifyOrderByForNativeExecution(final ScanQuery query)\n  {\n    if (query.getTimeOrder() == Order.NONE && !query.getOrderBys().isEmpty()) {","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/scan/ScanQuery.java#L166-L202","documentation":"When a ScanQuery has a time-ordering ('order':'ascending'/'descending' on __time) but no user-supplied orderBy list, Druid requires __time to be among the selected columns, since results are ordered by that column. The plain IllegalArgumentException is thrown in the constructor when orderBysFromUser is null but an implicit time ordering column is missing from columns.","triggerScenarios":"Creating a ScanQuery with getTimeOrder() != NONE (order set via 'order' field) where columns does not contain __time and the user did not pass an explicit orderBy list.","commonSituations":"Queries requesting time-sorted scans with a projected column list that omits __time; older clients that assumed __time was always implicitly returned.","solutions":["Add \"__time\" to the columns list","Remove the 'order' property from the query","Set an explicit orderBy on a column that is selected"],"exampleFix":"// before\n{\"queryType\":\"scan\",\"columns\":[\"country\"],\"order\":\"ascending\"}\n// after\n{\"queryType\":\"scan\",\"columns\":[\"__time\",\"country\"],\"order\":\"ascending\"}","handlingStrategy":"validation","validationCode":"if (query.getOrder() != ScanQuery.Order.NONE && !query.getColumns().contains(ScanQuery.DRUID_TIMESTAMP_FIELD /* __time */) && query.getOrderBys().isEmpty()) {\n  throw new IllegalArgumentException(\"time-ordered scan requires __time in columns\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always include __time when using 'order':'ascending'/'descending'","Prefer explicit orderBy over the legacy order field","Add a lint rule for scan query JSON templates"],"tags":["druid","scan-query","validation","time-ordering"],"backgroundTag":"schema-validation-failed","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"}