Repository navigation
lint: do not suggest a truncating division for col * n on Postgres - #196
Merged
Merged
Conversation
`_gv005_fix` moves `amount * 2 >= 11` to the constant side as `amount >= 11 / 2`. On Postgres `/` between integers truncates, so that is `amount >= 5`, which also keeps rows with `amount = 5` (5 * 2 = 10, not >= 11). The advice would change the results, which is the same problem laaielichter#191 fixed for a division, only the other way round. When the operand is `exp.Mul`, the dialect is `postgres`, and dividing the comparison constant by the multiplier leaves a remainder, the generic sentence ("Move the calculation to the constant side of the comparison.") is given instead. An exact division is still spelled out, because it keeps the same rows: `amount * 2 >= 10` still suggests `amount >= 10 / 2`. A decimal on either side is divided as numeric by Postgres, so `amount * 1.2 > 100` keeps its concrete rewrite too. DuckDB, BigQuery and Snowflake divide with decimals; their advice is unchanged. The constant may be written with a leading minus (`amount * 2 >= -11`), so the new `_integer_value` helper reads a `Literal` or a `Neg` around one, and `_loses_a_fraction` returns False whenever it cannot read both sides as integers - an unreadable node means "say nothing specific", not "guess". The test table covers the truncating case in both orders and with a negative constant, the exact division, both decimal forms, and the three decimal-division dialects. It fails without the change with the old `amount >= 11 / 2` advice.
Owner
|
Thanks @tyza66, this is merged. The before/after check is exactly what #195 asked for: If you want another one, #98 (incremental Generated by Claude Code |
laaielichter
added a commit
that referenced
this pull request
Nov 17, 2026
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.
Closes #195.
_gv005_fixmovesamount * 2 >= 11to the constant side asamount >= 11 / 2, and on Postgres that division is5: the rewrite also keepsamount = 5, whose5 * 2is 10. So the fix advice suggests a query that returns different rows, which is the same problem as #191 with the operands the other way round.What changed
When the operand is
exp.Mul, the dialect ispostgres, and dividing the comparison constant by the multiplier leaves a remainder, the generic sentence is returned instead:I took the second of the two options in the issue: an exact division is still spelled out, because it keeps the same rows.
amount * 2 >= 1111 <= amount * 2amount * 2 >= -11amount * 2 >= 10amount >= 10 / 2amount * 1.2 > 100amount > 100 / 1.2(unchanged)amount * 2.0 >= 11amount >= 11 / 2.0A decimal on either side is divided as numeric by Postgres, so those keep their concrete rewrite. DuckDB, BigQuery and Snowflake divide with decimals, so their advice is byte-for-byte unchanged; the test asserts that too.
Two small helpers keep the check honest:
_integer_valuereads aLiteralor aNegaround one (the constant can be written with a leading minus), and_loses_a_fractionreturnsFalsewhenever either side is not an integer literal, so an unreadable node falls back to the concrete rewrite rather than guessing. Thepostgresdivision check from #191 is untouched.Verification
test_gv005_postgres_mul_is_not_rewritten_as_a_truncating_divisionintests/test_lint.py: both orders, the negative constant, the exact division, both decimal forms, and the three decimal-division dialects.- Move the calculation to the constant side of the comparison./+ Move ... e.g. amount >= 11 / 2 ....PYTHONPATH=src pytest tests/test_lint.py-> 160 passed. Full suite (minustests/test_review.py, which fails on Windows with aWinError 50this machine has had all along) -> 1114 passed, 10 skipped.ruff checkon both touched files: clean.No
CHANGELOG.mdchange, per the repo rules.