{"record":{"id":"bc27b433a246b799","repo":"apache/druid","slug":"column-s-doesn-t-exist-but-window-function-firs","errorCode":null,"errorMessage":"column[%s] doesn't exist, but window function FIRST wants it to","messagePattern":"column\\[(.+?)\\] doesn't exist, but window function FIRST wants it to","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/query/operator/window/value/WindowValueProcessorBase.java","lineNumber":78,"sourceCode":"    return outputColumn;\n  }\n\n  /**\n   * This implements the common logic between the various value processors.  It looks like it could be static, but if\n   * it is static then the lambda becomes polymorphic.  We keep it as a member method of the base class so taht the\n   * JVM can inline it and specialize the lambda\n   *\n   * @param input incoming RowsAndColumns, as in Processor.process\n   * @param fn    function that converts the input column into the output column\n   * @return RowsAndColumns, as in Processor.process\n   */\n  public RowsAndColumns processInternal(RowsAndColumns input, Function<Column, Column> fn)\n  {\n    final AppendableRowsAndColumns retVal = RowsAndColumns.expectAppendable(input);\n\n    final Column column = input.findColumn(inputColumn);\n    if (column == null) {\n      throw new ISE(\"column[%s] doesn't exist, but window function FIRST wants it to\", inputColumn);\n    }\n\n    retVal.addColumn(outputColumn, fn.apply(column));\n    return retVal;\n  }\n\n  @Override\n  public boolean validateEquivalent(Processor otherProcessor)\n  {\n    return getClass() == otherProcessor.getClass()\n           && intervalValidation((WindowValueProcessorBase) otherProcessor);\n  }\n\n  protected boolean intervalValidation(WindowValueProcessorBase other)\n  {\n    // Only input needs to be the same for the processors to produce equivalent results\n    return inputColumn.equals(other.inputColumn);\n  }","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/query/operator/window/value/WindowValueProcessorBase.java#L60-L96","documentation":"Window value processors (FIRST/LAST/etc.) project a new output column from an existing input column via processInternal. If the configured input column is absent from the incoming RowsAndColumns, Druid throws an illegal-state error because the window function cannot be evaluated without its input. This almost always means the column referenced in the window spec doesn't exist at this stage of the query.","triggerScenarios":"WindowOperatorFrame with a FIRST (or subclass) processor whose inputColumn is not present in the rows flowing through the window stage; typically after a typo in column name or a projection that dropped the column.","commonSituations":"SQL query references a column alias that doesn't exist in the underlying scan; aggregation renamed the column before window processing; case-sensitivity mismatch between column names; schema drift in the data source.","solutions":["Verify the column name in the window function matches the actual column in the input (check with EXPLAIN PLAN)","Add the missing column to the projection/scan before the window stage","Fix aliasing so the source column survives to the window operator"],"exampleFix":"// before\nFIRST_VALUE(nonexistent_col) OVER (ORDER BY ts)\n// after\nFIRST_VALUE(existing_col) OVER (ORDER BY ts)","handlingStrategy":"validation","validationCode":"// verify the column exists in the input signature before the window stage\nif (!inputSignature.getColumnNames().contains(\"my_col\")) {\n  throw new IllegalStateException(\"my_col missing before window function\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  runQuery(sql);\n} catch (IllegalStateException e) {\n  if (e.getMessage().contains(\"doesn't exist, but window function\")) {\n    throw new QueryValidationException(\"check window function column name\", e);\n  }\n  throw e;\n}","preventionTips":["EXPLAIN PLAN the query to see column names at each stage","Remember aggregations rename columns — reference the output alias","Watch for case sensitivity in column names"],"tags":["window-function","missing-column","query-processing"],"backgroundTag":"resource-not-found","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"}