ROW_NUMBER() OVER () or SUM(x) OVER () without PARTITION BY computes
over the entire result set. Sometimes intentional, often a bug - the
author forgot to specify the partition key.
Should fail
SELECT
user_id,
ROW_NUMBER() OVER () AS rn
FROM events;
Should pass
SELECT
user_id,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY event_time) AS rn
FROM events;
Implementation hints
- Structural rule in
sql_guard/rules/structural.py.
- Model after
S002 deeply-nested-subquery - uses sqlparse tokens.
- Detect
OVER ( ... ) where the inner content lacks PARTITION BY.
- Severity:
warning.
Estimated LOC: ~40 code + ~25 test.
ROW_NUMBER() OVER ()orSUM(x) OVER ()withoutPARTITION BYcomputesover the entire result set. Sometimes intentional, often a bug - the
author forgot to specify the partition key.
Should fail
Should pass
Implementation hints
sql_guard/rules/structural.py.S002 deeply-nested-subquery- uses sqlparse tokens.OVER ( ... )where the inner content lacksPARTITION BY.warning.Estimated LOC: ~40 code + ~25 test.