Skip to content

lint: do not suggest a truncating division for col * n on Postgres - #196

Merged
laaielichter merged 1 commit into
laaielichter:mainfrom
tyza66:fix/gv005-mul-postgres
Nov 17, 2026
Merged

laaielichter merged 1 commit into
laaielichter:mainfrom
tyza66:fix/gv005-mul-postgres

Conversation

@tyza66

@tyza66 tyza66 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Closes #195.

_gv005_fix moves amount * 2 >= 11 to the constant side as amount >= 11 / 2, and on Postgres that division is 5: the rewrite also keeps amount = 5, whose 5 * 2 is 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 is postgres, and dividing the comparison constant by the multiplier leaves a remainder, the generic sentence is returned instead:

Move the calculation to the constant side of the comparison.

I took the second of the two options in the issue: an exact division is still spelled out, because it keeps the same rows.

SQL (postgres) advice
amount * 2 >= 11 generic sentence (no truncating rewrite)
11 <= amount * 2 generic sentence
amount * 2 >= -11 generic sentence
amount * 2 >= 10 amount >= 10 / 2
amount * 1.2 > 100 amount > 100 / 1.2 (unchanged)
amount * 2.0 >= 11 amount >= 11 / 2.0

A 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_value reads a Literal or a Neg around one (the constant can be written with a leading minus), and _loses_a_fraction returns False whenever either side is not an integer literal, so an unreadable node falls back to the concrete rewrite rather than guessing. The postgres division check from #191 is untouched.

Verification

  • New test_gv005_postgres_mul_is_not_rewritten_as_a_truncating_division in tests/test_lint.py: both orders, the negative constant, the exact division, both decimal forms, and the three decimal-division dialects.
  • It fails without the source change: - 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 (minus tests/test_review.py, which fails on Windows with a WinError 50 this machine has had all along) -> 1114 passed, 10 skipped.
  • ruff check on both touched files: clean.

No CHANGELOG.md change, per the repo rules.

`_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.
Copilot AI balanced review requested due to automatic review settings November 17, 2026 14:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@laaielichter
laaielichter merged commit 9d3d234 into laaielichter:main Nov 17, 2026
14 checks passed

Copy link
Copy Markdown
Owner

Thanks @tyza66, this is merged. The before/after check is exactly what #195 asked for: amount * 2 >= 11 on Postgres now gets the generic advice, the exact amount * 2 >= 10 case keeps its concrete rewrite, and the new test fails without the change. Your name goes in the next release notes with the other contributors.

If you want another one, #98 (incremental --lookback and prune --keep-last: refuse negative numbers) is a good first issue and nobody has claimed it yet. Comment on it before you start.


Generated by Claude Code

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.

GV005: fix advice for col * n on Postgres suggests an integer division that changes results

3 participants