Skip to content

static_analysis: flag range filters on nullable fields with no is-set guard - #214

Open
amitascra wants to merge 1 commit into
agritheory:mainfrom
amitascra:nullable-filter-check
Open

static_analysis: flag range filters on nullable fields with no is-set guard#214
amitascra wants to merge 1 commit into
agritheory:mainfrom
amitascra:nullable-filter-check

Conversation

@amitascra

Copy link
Copy Markdown

Frappe wraps filters on nullable columns in ifnull(col, ''). That makes '' a valid operand for range operators — '' < '2026-01-01' is string-true on both MariaDB and Postgres — so ["due_date", "<", nowdate()] also matches rows where due_date is NULL, not just rows before the date. Fix is pairing the comparison with ["field", "is", "set"].

This bit us in production: a scheduled sweep advancing overdue subscriptions through Past Due -> Expired stages jumped straight to the terminal stage in the same run a row first became overdue, because the not-yet-populated deadline column read as '' and compared less than "now".

Adds nullable_filters as a new static_analysis check, following the existing validator pattern (own module, Result dataclass with errors/warnings, wired into StaticAnalysisConfig/StaticAnalyzer/CLI). It's warning-only, not an error — a caller may already exclude NULLs some other way and the check doesn't try to prove that.

Ran it against a live app (bench_manager, ~30 doctypes) with all other checks off: 167 get_all/get_list/get_count/exists calls scanned, one real hit — an expiry_date <= alert_date filter on a nullable Datetime field with no is-set guard, same shape as the bug above.

Docs added in docs/static_analysis.md under a new nullable_filters section with a before/after example.

Frappe wraps filters on nullable columns in ifnull(col, ''), so a range
operator on a Date/Datetime/Time/Data field also matches NULL rows unless
paired with an explicit is-set guard. Adds a static_analysis check for it,
wired into the CLI (--no-nullable-filters) and StaticAnalysisConfig.
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.

1 participant