{"record":{"id":"8ab168e64df3570a","repo":"prestodb/presto","slug":"invalid-procedure-arguments-8ab168","errorCode":"INVALID_PROCEDURE_ARGUMENTS","errorMessage":"The arguments to GROUPING() must be expressions referenced by the GROUP BY at the associated query level. Mismatch due to %s.","messagePattern":"The arguments to GROUPING\\(\\) must be expressions referenced by the GROUP BY at the associated query level\\. Mismatch due to (.+?)\\.","errorType":"error_code","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/AggregationAnalyzer.java","lineNumber":707,"sourceCode":"            checkArgument(node.getPosition() < parameters.size(), \"Invalid parameter number %s, max values is %s\", node.getPosition(), parameters.size() - 1);\n            return process(parameters.get(NodeRef.of(node)), context);\n        }\n\n        public Boolean visitGroupingOperation(GroupingOperation node, Void context)\n        {\n            // ensure that no output fields are referenced from ORDER BY clause\n            if (orderByScope.isPresent()) {\n                node.getGroupingColumns().forEach(groupingColumn -> verifyNoOrderByReferencesToOutputColumns(\n                        groupingColumn,\n                        REFERENCE_TO_OUTPUT_ATTRIBUTE_WITHIN_ORDER_BY_GROUPING,\n                        \"Invalid reference to output of SELECT clause from grouping() expression in ORDER BY\"));\n            }\n\n            Optional<Expression> argumentNotInGroupBy = node.getGroupingColumns().stream()\n                    .filter(argument -> !columnReferences.containsKey(NodeRef.of(argument)) || !isGroupingKey(argument))\n                    .findAny();\n            if (argumentNotInGroupBy.isPresent()) {\n                throw new SemanticException(\n                        INVALID_PROCEDURE_ARGUMENTS,\n                        node,\n                        \"The arguments to GROUPING() must be expressions referenced by the GROUP BY at the associated query level. Mismatch due to %s.\",\n                        argumentNotInGroupBy.get());\n            }\n            return true;\n        }\n\n        @Override\n        public Boolean process(Node node, @Nullable Void context)\n        {\n            if (expressions.stream().anyMatch(node::equals)\n                    && (!orderByScope.isPresent() || !hasOrderByReferencesToOutputColumns(node))\n                    && !hasFreeReferencesToLambdaArgument(node, analysis)) {\n                return true;\n            }\n\n            return super.process(node, context);","sourceCodeStart":689,"sourceCodeEnd":725,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/AggregationAnalyzer.java#L689-L725","documentation":"GROUPING() only accepts column expressions that are actually referenced by the GROUP BY clause at the same query level. visitGroupingOperation verifies each grouping column exists in the analysis's columnReferences and is a grouping key; otherwise it throws INVALID_PROCEDURE_ARGUMENTS naming the mismatched argument.","triggerScenarios":"GROUPING(col) where col is not in GROUP BY; GROUPING over an expression (e.g. GROUPING(a+b)) that is not literally a grouping key; GROUPING referencing columns from an outer/inner query level.","commonSituations":"Typo in the GROUPING argument; passing a computed expression instead of the exact grouped column; using GROUPING in queries whose GROUP BY was edited/renamed.","solutions":["Make the argument exactly match a column listed in GROUP BY.","Add the argument column to GROUP BY if it should be a grouping key.","Replace expression arguments with the base columns that appear in GROUP BY.","Remove the GROUPING call if it is not needed (it is only meaningful in rollup/cube/grouping-sets queries)."],"exampleFix":"// before\nSELECT grouping(a + b) FROM t GROUP BY a, b;\n// after\nSELECT grouping(a) FROM t GROUP BY a, b;","handlingStrategy":"validation","validationCode":"// GROUPING args must exactly match GROUP BY columns\nfor (Expression arg : groupingCall.getGroupingColumns()) {\n    if (!groupByColumns.contains(arg)) {\n        throw new IllegalArgumentException(\"GROUPING arg not in GROUP BY: \" + arg);\n    }\n}","typeGuard":null,"tryCatchPattern":"try { runQuery(sql); } catch (SemanticException e) { if (e.getCode() == INVALID_PROCEDURE_ARGUMENTS) { /* fix GROUPING args */ } throw e; }","preventionTips":["Pass only exact GROUP BY column references to GROUPING().","Never pass computed expressions to GROUPING().","Recheck GROUPING args after editing GROUP BY/renames.","Use GROUPING only with ROLLUP/CUBE/GROUPING SETS queries."],"tags":["sql","grouping","group-by"],"backgroundTag":"grouping-argument-mismatch","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"}