Skip to content

rule: S004 window-without-partition - warn on OVER () without PARTITION BY #9

@Pawansingh3889

Description

@Pawansingh3889

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions