Repository navigation
Add a lint warning for comparisons with NULL (#129) - #137
Conversation
laaielichter
left a comment
There was a problem hiding this comment.
Welcome to Gulliver, @amritkang165, and thank you for #137. This is a careful first PR: the rule is small and readable, the 47 tests cover both operand orders, four dialects, nested comparisons, the negative cases, file positions, inline ignores and [lint] config, and the PR text is honest about what was and was not run. You commented on #129 at 13:17, before opening the PR, nobody else had claimed it, so it is yours and I have assigned it to you.
Two small things need fixing before it can merge (one is a real bug in a supported dialect), plus a few suggestions.
What I checked. I merged 7bc19dc onto current main (c48adf8; it is a fast-forward, no conflicts) in a scratch worktree with a fresh venv:
ruff check .: all checks passedpytest -q: 918 passed, 10 skipped (the skips need a live Postgres)gulliver rules --format markdownagainst the README table: identical, so the README row is correct and GV019 is correctly left free for #125gulliver ruleslists GV020null-comparison(warn);rule_docs()has it with autofixnone;fixleaves it alone- File mode (
gulliver lint q.sql) reportsq.sql:3:7for a three-line query, and skips anUPDATE ... SET amount = NULLin the same file (it is not a read), so no false positive there - I also ran 24 realistic queries through
lint():= NULL,<> NULL,!= NULL,NULL = x, inJOIN ... ON,CASE WHEN, aNOT INsubquery, aGROUP BYquery andx IS NULL OR x = NULL(one finding, correct) all warn;IS NULL,IS NOT NULL,IS NOT DISTINCT FROM NULL,status = 'NULL'(string literal),IN (1, NULL), MySQL<=> NULL, SnowflakeEQUAL_NULL(x, NULL),COALESCE/NULLIF,x = CAST(NULL AS INT)and a CTE column that happens to be NULL do not. I found no false positives.
Please fix
- In BigQuery the message loses the column.
gulliver lint --dialect bigquery "SELECT event_id FROM events WHERE amount = NULL LIMIT 5"printsNULL is never true: a comparison with NULL gives NULL.(and the same forNULL = amountand foru.deleted_at = NULLin a join). The cause iscomparison.sql(dialect=dialect): sqlglot's BigQuery generator writesx = NULLas justNULL. The other three dialects printamount = NULL is never true.... The test only asserts"never true" in finding.message, which is why it passes. One way to fix it: build the text from the two operands, for examplef"{comparison.this.sql(dialect=dialect)} {op} {comparison.expression.sql(dialect=dialect)}", and tighten the test to assert thatamountis in the message for every dialect. This also lets you print the operator the user wrote:amount != NULLcurrently shows asamount <> NULL(that part is only cosmetic, see 4). - A before/after SQL example in the docs. The rule table and the changelog line are there, but a reader who sees GV020 has nowhere to read why
= NULLreturns nothing.docs/features/lint-ci.mdhas a## GV015 fan-trap (new rule)section: please add a short## GV020 null-comparisonsection in the same style, with the before and after from #129 (WHERE amount = NULLreturns no rows;WHERE amount IS NULLfinds the rows without an amount), whenIS NOT DISTINCT FROMis the right choice, and the-- gulliver: ignore GV020line.
Suggestions, not blocking
- The CHANGELOG line sits at the very top of
### Added, which is where several other open PRs also add theirs (#65, #105 and #100/#101 already conflict withmainthere). Putting it at the end of the### Addedlist, just above### Thanks, avoids a "keep both lines" merge for whoever lands second. NULL = NULLandx = (NULL)are flagged, which is right. A one-line test for!=that checks the message uses the operator the user wrote would be nice once 1 is done.CASE x WHEN NULL THEN ...(simple CASE) has the same problem but is a different AST node and is not flagged. That is fine for this issue; if you like, mention it in the PR as a possible follow-up.
CI: the ci and codeql runs for this commit are action_required. That is GitHub holding a first-time contributor's first run until the maintainer approves it, so no check has run on GitHub yet; the results above are from my own run. You do not need to do anything for that.
Once 1 and 2 are pushed I will look again, and I expect it to be ready then.
For a next one, if you enjoyed working with sqlglot: #128 (SQL syntax errors show sqlglot's internal Token repr, and the next_step talks about statement types instead of the syntax error) is a free good first issue; comment on it to get it assigned.
If Gulliver is useful to you, a ⭐ helps other people find it.
Generated by Claude Code
|
Thanks for the detailed review. The warning now renders the two operands separately, so BigQuery keeps the column name and operand order. The tests now check the complete message across all four dialects, with additional cases for qualified columns in joins. I also added the GV020 section with before/after SQL, NULL-safe equality guidance and an ignore example, and moved the changelog entry to the end of Added. The optional operator-spelling change is left out for now: SQLGlot represents both != and <> as the same NEQ node, so the message still uses <>. Simple CASE with WHEN NULL remains a possible follow-up. Validation: 930 tests passed, 10 skipped because no live PostgreSQL test database is configured; Ruff passes. |
laaielichter
left a comment
There was a problem hiding this comment.
Thank you @amritkang165, both points are fixed and this is ready to merge.
What I checked. I merged 35ce18f onto current main (c48adf8, no conflicts) in a scratch worktree with a fresh venv:
ruff check .: all checks passedpytest -q: 930 passed, 10 skipped (the skips need a live Postgres)gulliver rules --format markdownagainst the README table: identical
The two requested fixes
- BigQuery message.
amount = NULL,NULL = amount,u.deleted_at = NULLin a join andamount != NULLnow print their operands in all four dialects (duckdb, postgres, bigquery, snowflake), for exampleamount = NULL is never true: a comparison with NULL gives NULL.The tests now assert the exact message for every dialect, plus a separate join-operand test, so the old loss of the column cannot come back unnoticed. Building the text from the two operands was a good fix. - Docs.
docs/features/lint-ci.mdhas a## GV020 null-comparisonsection with the before and after (= NULLreturns no rows,IS NULLfinds them), whenIS NOT DISTINCT FROMis the right choice, the no-autofix reason and the-- gulliver: ignore GV020line. I also ran that ignore example through file lint: the GV020 finding is suppressed.
You also moved the CHANGELOG line to the end of ### Added, which keeps the merge simple, and llms.txt and the skill table mention the rule.
Two tiny notes, neither blocks the merge: != is printed as <> (the test pins that, so it is a deliberate choice), and simple CASE x WHEN NULL is still not flagged. That would make a nice follow-up if you ever want one.
CI: the ci and codeql runs for 35ce18f are action_required. That is GitHub holding a first-time contributor's run until the maintainer approves it, so nothing has run on GitHub yet; the results above are from my own run. You do not need to do anything.
Thanks again for a careful first contribution. I will list you under Thanks in the CHANGELOG when it lands.
Generated by Claude Code
|
Merged. Thank you @amritkang165! The new GV020 null-comparison rule catches Generated by Claude Code |
What and why
gulliver lintcurrently misses comparisons such asamount = NULLandamount != NULL. These evaluate to NULL rather than true, so they can silently prevent rows from matching.This adds
GV020 null-comparisonas a warning for=,<>and!=when either operand is a literal NULL. It also catches comparisons inside joins, CASE expressions and subqueries, while leavingIS NULL,IS NOT NULL, NULL-safe comparisons andIN (1, NULL)alone. The warning suggests the appropriate NULL predicates without applying an autofix, since replacing the comparison can change the result.The rule is included in
gulliver rules, the README, agent docs, skill table and changelog. It uses the existing file-location, inline-ignore and severity configuration support. GV019 is reserved by #125, so this uses GV020 as requested in the issue.Fixes #129
How it was tested
On macOS arm64 with Python 3.14.7:
python -m pytest -q tests/test_lint.py tests/test_lint_report.pypasses all 183 tests.python -m pytest -q— 918 passed, 10 skipped. No live PostgreSQL test database was configured.ruff check .— passes.Also checked the CLI directly:
amount = NULLreports GV020, whileamount IS NULLstays clean. Linux and Windows validation is left to the repository's CI matrix.