{"record":{"id":"ff5ca1dc5581e30d","repo":"prestodb/presto","slug":"too-many-grouping-sets","errorCode":"TOO_MANY_GROUPING_SETS","errorMessage":"GROUP BY has more than %s grouping sets but can contain at most %s","messagePattern":"GROUP BY has more than (.+?) grouping sets but can contain at most (.+?)","errorType":"error_code","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java","lineNumber":4356,"sourceCode":"                        int exponent = element.getExpressions().size();\n                        if (exponent > 30) {\n                            throw new ArithmeticException();\n                        }\n                        product = 1 << exponent;\n                    }\n                    else if (element instanceof Rollup) {\n                        product = element.getExpressions().size() + 1;\n                    }\n                    else if (element instanceof GroupingSets) {\n                        product = ((GroupingSets) element).getSets().size();\n                    }\n                    else {\n                        throw new UnsupportedOperationException(\"Unsupported grouping element type: \" + element.getClass().getName());\n                    }\n                    crossProduct = Math.multiplyExact(crossProduct, product);\n                }\n                catch (ArithmeticException e) {\n                    throw new SemanticException(TOO_MANY_GROUPING_SETS, node,\n                            \"GROUP BY has more than %s grouping sets but can contain at most %s\", Integer.MAX_VALUE, getMaxGroupingSets(session));\n                }\n                if (crossProduct > getMaxGroupingSets(session)) {\n                    throw new SemanticException(TOO_MANY_GROUPING_SETS, node,\n                            \"GROUP BY has %s grouping sets but can contain at most %s\", crossProduct, getMaxGroupingSets(session));\n                }\n            }\n        }\n\n        private List<Expression> analyzeGroupBy(QuerySpecification node, Scope scope, List<Expression> outputExpressions)\n        {\n            if (node.getGroupBy().isPresent()) {\n                ImmutableList.Builder<Set<FieldId>> cubes = ImmutableList.builder();\n                ImmutableList.Builder<List<FieldId>> rollups = ImmutableList.builder();\n                ImmutableList.Builder<List<Set<FieldId>>> sets = ImmutableList.builder();\n                ImmutableList.Builder<Expression> complexExpressions = ImmutableList.builder();\n                ImmutableList.Builder<Expression> groupingExpressions = ImmutableList.builder();\n","sourceCodeStart":4338,"sourceCodeEnd":4374,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java#L4338-L4374","documentation":"During GROUP BY analysis Presto computes the cross-product size of all grouping sets (CUBE/ROLLUP/GROUPING SETS elements). If the multiplication overflows a long (Math.multiplyExact throws ArithmeticException), the analyzer reports that the GROUP BY implies more than Integer.MAX_VALUE grouping sets, exceeding the configured maximum (session/system max grouping sets). The query is rejected before execution.","triggerScenarios":"A GROUP BY with many CUBE or GROUPING SETS columns whose combined product of per-element set counts overflows (e.g. CUBE on 40+ columns yields 2^40 sets), or any product above the configured max grouping sets limit.","commonSituations":"Autogenerated BI queries that CUBE every dimension column; users unaware grouping sets grow exponentially with cube columns; queries migrated from engines without such limits.","solutions":["Reduce the number of columns in CUBE/GROUPING SETS so the total set count stays within the limit.","Replace full CUBE with explicit GROUPING SETS listing only the combinations actually needed.","Raise the grouping-sets limit via its session/config property if the query is legitimate and cluster resources allow."],"exampleFix":"// before\nSELECT a, b, c, d, e, f, g, h, count(*) FROM t GROUP BY CUBE (a,b,c,d,e,f,g,h);\n// after\nSELECT a, b, c, count(*) FROM t GROUP BY CUBE (a,b,c);","handlingStrategy":"validation","validationCode":"long sets = 1;\nfor (GroupingElement e : groupByElements) {\n    int product = e instanceof Cube ? (1 << e.getExpressions().size()) : (e instanceof Rollup ? e.getExpressions().size() + 1 : 1);\n    sets = Math.multiplyExact(sets, product);\n}\nif (sets > maxGroupingSets) throw new IllegalArgumentException(\"Too many grouping sets: \" + sets);","typeGuard":null,"tryCatchPattern":"try { session.execute(sql); } catch (SemanticException e) { if (e.getCode() == TOO_MANY_GROUPING_SETS) { /* reduce CUBE columns or use explicit GROUPING SETS */ } else { throw e; } }","preventionTips":["Compute 2^n for CUBE columns before emitting the query.","Prefer explicit GROUPING SETS over full CUBE in generated BI SQL.","Know the cluster's max grouping sets config and enforce it in your query builder."],"tags":["sql","presto","group-by","grouping-sets"],"backgroundTag":"grouping-sets-limit-exceeded","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"}