Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/gulliver/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}"
Expand Down
28 changes: 28 additions & 0 deletions tests/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Loading