static_analysis: flag range filters on nullable fields with no is-set guard - #214
Open
amitascra wants to merge 1 commit into
Open
static_analysis: flag range filters on nullable fields with no is-set guard#214amitascra wants to merge 1 commit into
amitascra wants to merge 1 commit into
Conversation
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.
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.
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 wheredue_dateis 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_filtersas a new static_analysis check, following the existing validator pattern (own module,Resultdataclass with errors/warnings, wired intoStaticAnalysisConfig/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_datefilter 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_filterssection with a before/after example.