From ad15e2da1df294b2bd70348c1202bf4309febfa8 Mon Sep 17 00:00:00 2001 From: tyza66 <83948628+tyza66@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:14:59 +0800 Subject: [PATCH] lint: do not suggest a truncating division for `col * n` on Postgres `_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 #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. --- src/gulliver/lint.py | 33 +++++++++++++++++++++++++++++++++ tests/test_lint.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/gulliver/lint.py b/src/gulliver/lint.py index 1dc75e5..704786f 100644 --- a/src/gulliver/lint.py +++ b/src/gulliver/lint.py @@ -245,6 +245,32 @@ def _count_for_existence(node: exp.Expression) -> str | None: return None +def _integer_value(node: exp.Expression | None) -> int | None: + """The value of an integer literal, with a leading minus sign if there is one.""" + sign = 1 + if isinstance(node, exp.Neg): + sign, node = -1, node.this + if isinstance(node, exp.Literal) and node.is_int: + try: + return sign * int(str(node.this)) + except (TypeError, ValueError): + return None + return None + + +def _loses_a_fraction(const: exp.Expression | None, divisor: exp.Expression) -> bool: + """True when ``const / divisor`` drops a fraction under Postgres integer division. + + Moving ``amount * 2 >= 11`` to ``amount >= 11 / 2`` is wrong on Postgres: ``11 / 2`` is 5, so the + rewrite also keeps rows with ``amount = 5``, whose ``5 * 2`` is 10. Only an exact division, such as + ``10 / 2``, gives the same rows. A decimal on either side is fine: Postgres divides it as numeric. + """ + const_value, divisor_value = _integer_value(const), _integer_value(divisor) + if const_value is None or divisor_value is None or divisor_value == 0: + return False + return const_value % divisor_value != 0 + + def _gv005_fix( operand: exp.Expression, column_sql: str, @@ -340,6 +366,13 @@ def _gv005_fix( is_col_left = False cmp_const_sql = cmp_const_node.sql(dialect=dialect) if cmp_const_node is not None else "100" + # A multiplication is moved to the other side as a division, and Postgres divides integers with + # truncation, so `amount * 2 >= 11` must not become `amount >= 11 / 2` (that is 5, which also + # matches amount = 5). An exact division keeps the same rows, so it is still worth suggesting. + if isinstance(operand, exp.Mul) and dialect == "postgres" \ + and _loses_a_fraction(cmp_const_node, arith_const_node): + return "Move the calculation to the constant side of the comparison." + if is_col_left: orig = f"{operand.sql(dialect=dialect)} {cmp_op_str} {cmp_const_sql}" rewrite = f"{column_sql} {cmp_op_str} {cmp_const_sql} {inv_op} {arith_const_sql}" diff --git a/tests/test_lint.py b/tests/test_lint.py index 83b94e1..fa1700d 100644 --- a/tests/test_lint.py +++ b/tests/test_lint.py @@ -275,6 +275,34 @@ def test_gv005_postgres_div_integer_division(): assert findings_duck[0].fix == "Move the calculation to the constant side, e.g. amount > 10 * 2 instead of amount / 2 > 10." +def test_gv005_postgres_mul_is_not_rewritten_as_a_truncating_division(): + """`amount * 2 >= 11` must not become `amount >= 11 / 2`: Postgres truncates that to 5.""" + + def fix(sql, dialect): + stmt = classify(sql, dialect) + findings = [f for f in lint(stmt.select, dialect) if f.rule == "GV005"] + assert len(findings) == 1, (sql, dialect) + return findings[0].fix + + generic = "Move the calculation to the constant side of the comparison." + assert fix("select id from t where amount * 2 >= 11 limit 5", "postgres") == generic + # Same comparison written the other way round, and with a negative constant. + assert fix("select id from t where 11 <= amount * 2 limit 5", "postgres") == generic + assert fix("select id from t where amount * 2 >= -11 limit 5", "postgres") == generic + # An exact division changes nothing on Postgres, so the concrete rewrite is still offered. + assert fix("select id from t where amount * 2 >= 10 limit 5", "postgres") == ( + "Move the calculation to the constant side, e.g. amount >= 10 / 2 instead of amount * 2 >= 10.") + # A decimal on either side is divided as numeric by Postgres, so it keeps the concrete rewrite. + assert fix("select id from t where amount * 1.2 > 100 limit 5", "postgres") == ( + "Move the calculation to the constant side, e.g. amount > 100 / 1.2 instead of amount * 1.2 > 100.") + assert fix("select id from t where amount * 2.0 >= 11 limit 5", "postgres") == ( + "Move the calculation to the constant side, e.g. amount >= 11 / 2.0 instead of amount * 2.0 >= 11.") + # DuckDB, BigQuery and Snowflake divide with decimals, so nothing changes for them. + for dialect in ("duckdb", "bigquery", "snowflake"): + assert fix("select id from t where amount * 2 >= 11 limit 5", dialect) == ( + "Move the calculation to the constant side, e.g. amount >= 11 / 2 instead of amount * 2 >= 11.") + + def test_order_without_limit(): assert rules("select id from events order by id")["GV006"] == "warn"