Skip to content

Conversation

@matt-metivier
Copy link

@matt-metivier matt-metivier commented Jan 25, 2026

Problem

Users performing multi-axis analytics need flexible grouping options. Currently, HogQL parses but doesn't implement CUBE and ROLLUP, and doesn't support GROUPING SETS at all. This makes it difficult to create queries that group data across multiple dimensions without extensive copy-pasting.

Closes #35619

Changes

  • Lexer: Added GROUPING and SETS tokens to HogQLLexer.common.g4
  • Parser Grammar: Updated groupByClause rule in HogQLParser.g4 to properly capture CUBE, ROLLUP, and GROUPING SETS syntax
  • AST: Added group_by_modifier (CUBE/ROLLUP) and grouping_sets fields to SelectQuery in ast.py
  • C++ Parsers: Updated parser_python.cpp and parser_json.cpp to extract and populate the new AST fields
  • Visitors: Updated TraversingVisitor and CloningVisitor in visitor.py to handle new fields
  • Resolver: Updated resolver.py to clone the new fields
  • Printer: Added _build_group_by_clause method in printer/base.py with security limits
  • Security: Added MAX_CUBE_ROLLUP_COLUMNS=10 and MAX_GROUPING_SETS=64 limits in constants.py to prevent resource exhaustion

How did you test this code?

  • Added parser tests in _test_parser.py for CUBE, ROLLUP, and GROUPING SETS syntax
  • Added printer tests in test_printer.py for SQL output verification
  • Added security tests to verify limits are enforced (exceeding limits raises QueryError)

Publish to changelog?

Yes - This enables users to use GROUP BY CUBE(), GROUP BY ROLLUP(), and GROUP BY GROUPING SETS() in HogQL queries for flexible multi-axis analytics.

@matt-metivier matt-metivier requested a review from a team as a code owner January 25, 2026 20:16
@matt-metivier matt-metivier force-pushed the feature/grouping-sets-support branch from 2189427 to b10a598 Compare January 25, 2026 20:34
@matt-metivier
Copy link
Author

Best to look at the first commit, it has the changes before the ANTLR parser was regenerated

@matt-metivier
Copy link
Author

When you've time @Gilbert09

@matt-metivier
Copy link
Author

Docs if we move forward with this PostHog/posthog.com#14550

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support clickhouse grouping sets

1 participant