{"record":{"id":"05fbacf990d15775","repo":"prestodb/presto","slug":"errorstring","errorCode":null,"errorMessage":"errorString","messagePattern":"errorString","errorType":"error_code","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/AggregationAnalyzer.java","lineNumber":739,"sourceCode":"                    && !hasFreeReferencesToLambdaArgument(node, analysis)) {\n                return true;\n            }\n\n            return super.process(node, context);\n        }\n    }\n\n    private boolean hasOrderByReferencesToOutputColumns(Node node)\n    {\n        return hasReferencesToScope(node, analysis, orderByScope.get());\n    }\n\n    private void verifyNoOrderByReferencesToOutputColumns(Node node, SemanticErrorCode errorCode, String errorString)\n    {\n        getReferencesToScope(node, analysis, orderByScope.get())\n                .findFirst()\n                .ifPresent(expression -> {\n                    throw new SemanticException(errorCode, expression, errorString);\n                });\n    }\n}\n","sourceCodeStart":721,"sourceCodeEnd":743,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/AggregationAnalyzer.java#L721-L743","documentation":"verifyNoOrderByReferencesToOutputColumns throws a SemanticException with the caller-supplied errorCode and errorString when an ORDER BY clause references an output-column alias/scope that it is not allowed to reference. It finds any expression in the ORDER BY that resolves against the orderByScope and throws using the caller's message template, so the exact text depends on the call site (e.g. 'ORDER BY clause cannot contain aggregations...' style errors).","triggerScenarios":"An ORDER BY (of a grouped query, aggregation query, or similar context validated via this helper) references expressions resolved from the output-column scope where that is prohibited — e.g. ORDER BY on output aliases containing aggregates when the context disallows it.","commonSituations":"Ordering by output column aliases after adding aggregates/window functions; refactoring a query so an alias becomes an aggregation while ORDER BY still resolves it.","solutions":["Rewrite the ORDER BY to reference the underlying column expressions instead of the disallowed output reference.","Remove the aggregate/window/grouping construct from the ORDER BY expression.","Add the ordered expression to the GROUP BY clause and order by the base column.","Use ordinal or a subquery to control ordering without referencing the forbidden scope."],"exampleFix":"// before\nSELECT count(*) AS c FROM t GROUP BY g ORDER BY c + 1;\n// after\nSELECT count(*) AS c FROM t GROUP BY g ORDER BY count(*) + 1;","handlingStrategy":"validation","validationCode":"// ensure ORDER BY references base columns, not forbidden output-scope aliases\norderByExpressions.forEach(e -> {\n    if (isOutputColumnAlias(e) && disallowedContext) {\n        throw new IllegalArgumentException(\"ORDER BY references forbidden output column: \" + e);\n    }\n});","typeGuard":null,"tryCatchPattern":"try { runQuery(sql); } catch (SemanticException e) { if (e.getMessage().contains(\"ORDER BY\")) { /* rewrite ORDER BY to base columns */ } throw e; }","preventionTips":["Order by underlying column expressions rather than aliases in restricted contexts.","Avoid aggregates/window functions creeping into ordered aliases.","Keep ORDER BY expressions aligned with the current GROUP BY."],"tags":["sql","order-by","scope-resolution"],"backgroundTag":"order-by-references-output-column","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"}