prestodb/presto · error · SemanticException

INVALID_PROCEDURE_ARGUMENTS

INVALID_PROCEDURE_ARGUMENTS

Error message

The arguments to GROUPING() must be expressions referenced by the GROUP BY at the associated query level. Mismatch due to %s.

What it means

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.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/AggregationAnalyzer.java:707

            checkArgument(node.getPosition() < parameters.size(), "Invalid parameter number %s, max values is %s", node.getPosition(), parameters.size() - 1);
            return process(parameters.get(NodeRef.of(node)), context);
        }

        public Boolean visitGroupingOperation(GroupingOperation node, Void context)
        {
            // ensure that no output fields are referenced from ORDER BY clause
            if (orderByScope.isPresent()) {
                node.getGroupingColumns().forEach(groupingColumn -> verifyNoOrderByReferencesToOutputColumns(
                        groupingColumn,
                        REFERENCE_TO_OUTPUT_ATTRIBUTE_WITHIN_ORDER_BY_GROUPING,
                        "Invalid reference to output of SELECT clause from grouping() expression in ORDER BY"));
            }

            Optional<Expression> argumentNotInGroupBy = node.getGroupingColumns().stream()
                    .filter(argument -> !columnReferences.containsKey(NodeRef.of(argument)) || !isGroupingKey(argument))
                    .findAny();
            if (argumentNotInGroupBy.isPresent()) {
                throw new SemanticException(
                        INVALID_PROCEDURE_ARGUMENTS,
                        node,
                        "The arguments to GROUPING() must be expressions referenced by the GROUP BY at the associated query level. Mismatch due to %s.",
                        argumentNotInGroupBy.get());
            }
            return true;
        }

        @Override
        public Boolean process(Node node, @Nullable Void context)
        {
            if (expressions.stream().anyMatch(node::equals)
                    && (!orderByScope.isPresent() || !hasOrderByReferencesToOutputColumns(node))
                    && !hasFreeReferencesToLambdaArgument(node, analysis)) {
                return true;
            }

            return super.process(node, context);

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Make the argument exactly match a column listed in GROUP BY.
  2. Add the argument column to GROUP BY if it should be a grouping key.
  3. Replace expression arguments with the base columns that appear in GROUP BY.
  4. Remove the GROUPING call if it is not needed (it is only meaningful in rollup/cube/grouping-sets queries).

Example fix

// before
SELECT grouping(a + b) FROM t GROUP BY a, b;
// after
SELECT grouping(a) FROM t GROUP BY a, b;
Defensive patterns

Strategy: validation

Validate before calling

// GROUPING args must exactly match GROUP BY columns
for (Expression arg : groupingCall.getGroupingColumns()) {
    if (!groupByColumns.contains(arg)) {
        throw new IllegalArgumentException("GROUPING arg not in GROUP BY: " + arg);
    }
}

Try / catch

try { runQuery(sql); } catch (SemanticException e) { if (e.getCode() == INVALID_PROCEDURE_ARGUMENTS) { /* fix GROUPING args */ } throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/8ab168e64df3570a. Report an issue: GitHub.