Skip to content

fix: correct SQLite div_i formula for small dividends#5739

Closed
prql-bot wants to merge 1 commit intomainfrom
fix/issue-5735
Closed

fix: correct SQLite div_i formula for small dividends#5739
prql-bot wants to merge 1 commit intomainfrom
fix/issue-5735

Conversation

@prql-bot
Copy link
Copy Markdown
Collaborator

Problem

The SQLite div_i implementation used ROUND(ABS(a / b) - 0.5) * SIGN(a) * SIGN(b) to emulate floor division. When |a| < |b|, SQLite's integer / truncates to 0, so ABS(0) - 0.5 = -0.5, and ROUND(-0.5) = -1 (SQLite rounds away from zero). This produced -1 instead of the correct 0.

Solution

Replaced the formula with CAST(ABS(a * 1.0 / b) AS INTEGER) * SIGN(a) * SIGN(b). The * 1.0 forces float division, and CAST(... AS INTEGER) truncates toward zero — exactly the semantics needed for integer division.

Testing

  • Added a regression test (sqlite_div_i_small_dividend) that compiles 1 // 2 targeting SQLite
  • Verified the test fails with the old formula and passes with the fix
  • All 633 prqlc tests pass, all book documentation tests pass

Closes #5735 — automated triage

The ROUND(ABS(a/b) - 0.5) formula produced wrong results when |a| < |b|
because SQLite integer division truncates to 0, then ROUND(-0.5) = -1.

Replace with CAST(ABS(a * 1.0 / b) AS INTEGER) which forces float
division and truncates correctly.

Closes #5735

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator Author

@prql-bot prql-bot left a comment

Choose a reason for hiding this comment

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

Duplicate of #5736 (by @queelius) — both PRs apply the same fix to the same line. One should be closed.

The fix itself is correct: * 1.0 forces float division before truncation, avoiding the ROUND(-0.5) = -1 edge case in SQLite.

@max-sixty max-sixty closed this Mar 25, 2026
auto-merge was automatically disabled March 25, 2026 09:28

Pull request was closed

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.

SQLite div_i (integer division //) produces wrong results when |dividend| < |divisor|

2 participants