Skip to content

rule: W012 group-by-ordinal - warn on GROUP BY 1, 2 (non-portable) #2

@Pawansingh3889

Description

@Pawansingh3889

GROUP BY 1, 2 groups by output columns by position. It's terse but
brittle: if the SELECT list is reordered, the query silently produces
different results. Also non-portable (some dialects reject ordinals in
newer modes).

Should fail

SELECT region, status, COUNT(*)
FROM orders
GROUP BY 1, 2;

Should pass

SELECT region, status, COUNT(*)
FROM orders
GROUP BY region, status;

Implementation hints

  • Single-line rule in sql_guard/rules/warnings.py.
  • Regex: r"\bGROUP\s+BY\s+\d+(\s*,\s*\d+)*\b" - matches GROUP BY 1 or
    GROUP BY 1, 2, 3.
  • Careful: don't trigger on GROUP BY col1, col2 where the column names
    happen to start with digits (rare but possible).
  • Severity: warning.

Estimated LOC: ~20 code + ~15 test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions