{"record":{"id":"f882d127a5a18121","repo":"prestodb/presto","slug":"window-requires-over","errorCode":"WINDOW_REQUIRES_OVER","errorMessage":"Window function %s requires an OVER clause","messagePattern":"Window function (.+?) requires an OVER clause","errorType":"validation","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/WindowFunctionValidator.java","lineNumber":41,"sourceCode":"\nclass WindowFunctionValidator\n        extends DefaultExpressionTraversalVisitor<Void, Analysis>\n{\n    private final FunctionAndTypeResolver functionAndTypeResolver;\n\n    public WindowFunctionValidator(FunctionAndTypeResolver functionAndTypeResolver)\n    {\n        this.functionAndTypeResolver = requireNonNull(functionAndTypeResolver, \"functionManager is null\");\n    }\n\n    @Override\n    protected Void visitFunctionCall(FunctionCall functionCall, Analysis analysis)\n    {\n        requireNonNull(analysis, \"analysis is null\");\n\n        FunctionMetadata functionMetadata = functionAndTypeResolver.getFunctionMetadata(analysis.getFunctionHandle(functionCall));\n        if (functionMetadata != null && functionMetadata.getFunctionKind() == WINDOW && !functionCall.getWindow().isPresent()) {\n            throw new SemanticException(WINDOW_REQUIRES_OVER, functionCall, \"Window function %s requires an OVER clause\", functionMetadata.getName());\n        }\n        return super.visitFunctionCall(functionCall, analysis);\n    }\n}\n","sourceCodeStart":23,"sourceCodeEnd":46,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/WindowFunctionValidator.java#L23-L46","documentation":"Presto's window functions (rank, row_number, dense_rank, ntile, etc.) are only defined in the context of a window specification. When function analysis finds a WINDOW-kind function invoked without an OVER clause, it throws this semantic error.","triggerScenarios":"`SELECT row_number() FROM t` — calling a window function such as row_number(), rank(), dense_rank(), lag(), lead(), first_value() without `OVER (...)`.","commonSituations":"Queries copied from engines with different syntax; forgetting the OVER clause after refactoring; hand-written SQL where the OVER clause was accidentally deleted; generated SQL that appends window functions without window specs.","solutions":["Add an OVER clause with the appropriate partitioning/ordering, e.g. `row_number() OVER (PARTITION BY x ORDER BY y)`","Use `OVER ()` if the function should run over the entire result set","Replace the window function with an aggregate + GROUP BY if no windowing is actually needed"],"exampleFix":"// before\nSELECT name, row_number() FROM employees;\n// after\nSELECT name, row_number() OVER (ORDER BY salary) FROM employees;","handlingStrategy":"validation","validationCode":"// Check that every window function call in the SQL text carries an OVER clause\nfor (FunctionCall call : extractFunctionCalls(parsedSql)) {\n    if (isWindowFunction(call.getName()) && !call.getWindow().isPresent()) {\n        throw new IllegalArgumentException(call.getName() + \" requires OVER\");\n    }\n}","typeGuard":"boolean needsOver(String functionName) {\n    return Set.of(\"row_number\",\"rank\",\"dense_rank\",\"ntile\",\"lag\",\"lead\",\"first_value\",\"last_value\",\"nth_value\").contains(functionName.toLowerCase());\n}","tryCatchPattern":"try { executeQuery(sql); } catch (SemanticException e) { if (e.getCode().equals(WINDOW_REQUIRES_OVER)) { sql = addOverClause(sql); } else throw e; }","preventionTips":["Memorize the Presto window-function list (all require OVER)","When generating SQL, always append an OVER spec alongside window functions","Use OVER () when windowing defaults are intended","Run queries through a SQL linter that validates window syntax"],"tags":["sql","window-functions","semantic-analysis"],"backgroundTag":"window-function-missing-over","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}