{"record":{"id":"861a007f70b16c49","repo":"prestodb/presto","slug":"window-function-orderby-literal","errorCode":"WINDOW_FUNCTION_ORDERBY_LITERAL","errorMessage":"ORDER BY literals/constants with window function: '%s' is unnecessary and expensive. If you intend to ORDER BY using ordinals, please use the actual expression instead of the ordinal","messagePattern":"ORDER BY literals/constants with window function: '(.+?)' is unnecessary and expensive\\. If you intend to ORDER BY using ordinals, please use the actual expression instead of the ordinal","errorType":"error_code","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java","lineNumber":4166,"sourceCode":"                    throw new SemanticException(NOT_SUPPORTED, node, \"FILTER is not yet supported for window functions\");\n                }\n\n                if (windowFunction.getOrderBy().isPresent()) {\n                    throw new SemanticException(NOT_SUPPORTED, windowFunction, \"Window function with ORDER BY is not supported\");\n                }\n\n                Window window = windowFunction.getWindow().get();\n                if (window.getOrderBy().filter(orderBy -> orderBy.getSortItems().stream().anyMatch(item -> item.getSortKey() instanceof Literal)).isPresent()) {\n                    if (isAllowWindowOrderByLiterals(session)) {\n                        warningCollector.add(\n                                new PrestoWarning(\n                                        PERFORMANCE_WARNING,\n                                        String.format(\n                                                \"ORDER BY literals/constants with window function: '%s' is unnecessary and expensive. If you intend to ORDER BY using ordinals, please use the actual expression instead of the ordinal\",\n                                                windowFunction)));\n                    }\n                    else {\n                        throw new SemanticException(\n                                WINDOW_FUNCTION_ORDERBY_LITERAL,\n                                node,\n                                \"ORDER BY literals/constants with window function: '%s' is unnecessary and expensive. If you intend to ORDER BY using ordinals, please use the actual expression instead of the ordinal\",\n                                windowFunction);\n                    }\n                }\n\n                ImmutableList.Builder<Node> toExtract = ImmutableList.builder();\n                toExtract.addAll(windowFunction.getArguments());\n                toExtract.addAll(window.getPartitionBy());\n                window.getOrderBy().ifPresent(orderBy -> toExtract.addAll(orderBy.getSortItems()));\n                window.getFrame().ifPresent(toExtract::add);\n\n                List<FunctionCall> nestedWindowFunctions = extractWindowFunctions(toExtract.build());\n\n                if (!nestedWindowFunctions.isEmpty()) {\n                    throw new SemanticException(NESTED_WINDOW, node, \"Cannot nest window functions inside window function '%s': %s\",\n                            windowFunction,","sourceCodeStart":4148,"sourceCodeEnd":4184,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java#L4148-L4184","documentation":"When a window function's OVER clause contains ORDER BY items that are plain literals/constants, Presto flags this as unnecessary and expensive. If the session/system property allow-window-order-by-literals is disabled (default), this WINDOW_FUNCTION_ORDERBY_LITERAL error is thrown; if enabled, it is downgraded to a performance warning.","triggerScenarios":"`SELECT row_number() OVER (ORDER BY 1) FROM t` — any windowFunction whose window's ORDER BY sort items have a Literal sort key, analyzed while isAllowWindowOrderByLiterals(session) is false.","commonSituations":"Copy-pasted SQL from engines that accept `ORDER BY 1` ordinals in OVER clauses; hand-written queries where a developer intended positional ordering; ORM/BI tools generating constant sort keys.","solutions":["Remove the literal ORDER BY items from the OVER clause — they don't define a meaningful order.","Replace the ordinal with the actual column expression you meant to sort by, e.g. `OVER (ORDER BY col1)`.","As a temporary measure, enable the allow-window-order-by-literals config/session property to downgrade it to a warning, but fix the query afterwards."],"exampleFix":"-- before\nSELECT row_number() OVER (ORDER BY 1) FROM t;\n-- after\nSELECT row_number() OVER (ORDER BY created_at) FROM t;","handlingStrategy":"validation","validationCode":"-- Detect literal sort keys in OVER(ORDER BY ...) before execution:\n-- regex: OVER\\s*\\(\\s*ORDER\\s+BY\\s+(\\d+|'[^']*'|NULL)  → remove or replace with a real column expression.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never write ORDER BY <constant> inside an OVER clause.","If you meant ordinal ordering, write the actual column expression instead.","Enable allow-window-order-by-literals only as a transitional warning setting, with a follow-up fix ticket.","Lint analytic SQL for constant sort keys in code review or CI."],"tags":["sql","window-function","order-by","performance","semantic-analysis"],"backgroundTag":"window-order-by-literal","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"}