feat(rules): add W021 having-without-group-by#39
Merged
Pawansingh3889 merged 2 commits intoPawansingh3889:mainfrom May 3, 2026
Merged
Conversation
Detects HAVING clauses without a preceding GROUP BY in the same statement. The SQL engine treats HAVING-without-GROUP BY as a single implicit group and produces results that surprise the author -- almost always a typo for WHERE. Mirrors the W006 OrderByWithoutLimit pattern: multiline rule, two compiled regexes (HAVING / GROUP BY), check_statement returns a Finding when HAVING appears without GROUP BY (or with GROUP BY appearing after HAVING). Warning severity. Closes Pawansingh3889#3
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
289fd7c to
6dc08bb
Compare
Owner
|
Thanks Matt. Fourth contributed rule and the quality bar keeps going up. The depth-aware GROUP BY detection is the right call, and the upfront acknowledgement of the nested-subquery edge case is the kind of disclosure a maintainer always wants to see. Squashing in. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds W021, a warning rule that flags
HAVINGwithout a precedingGROUP BYin the same query block. The SQL engine treats this as a single implicit group and produces results that surprise the author -- almost always a typo forWHERE.What changed
sql_guard/rules/warnings.py-- newHavingWithoutGroupByclass. Mirrors W006 (OrderByWithoutLimit) for structure (multiline rule, two compiled regexes,check_statementreturnsFinding | None). Strips line and block comments before matching, and only acceptsGROUP BYmatches at parenthesis depth 0 so aGROUP BYinside a subquery does not satisfy a HAVING in the outer query.sql_guard/rules/__init__.py-- importsHavingWithoutGroupBy, slots it intoALL_RULESbetweenTruncateTable(W020) andCrossJoinExplicit(W022) to fill the W021 gap.tests/fixtures/warnings.sql-- adds the canonicalSELECT total FROM orders HAVING total > 1000;example.tests/test_rules.py-- addstest_w021_having_without_group_by(fixture-driven), plus two unit tests covering the comment and subquery cases. Updates rule-count assertions:test_all_rules_loaded38 → 39,test_28_warningsrenamed totest_29_warningsand bumped 28 → 29 (W-series goes from 22 to 23).Verification
Known limitation
A
SELECT ... FROM (SELECT ... GROUP BY ... HAVING ...) x-- valid SQL where a grouped subquery uses its own HAVING -- can produce a W021 false positive. The rule finds the first HAVING anywhere in the statement and looks for a depth-0 GROUP BY before it; the nested HAVING is depth >0 and its GROUP BY also depth >0, so the depth-0 search misses it. Fixing this cleanly requires depth-aware HAVING/GROUP BY pairing, which is more invasive than W006's regex style and worth a follow-up if it shows up in real fixtures. Filing as-is because the simple form (the one the issue calls out) is correctly detected.Closes #3
This contribution was developed with AI assistance.