Skip to content

fix(sync-rules): CAST(real AS INTEGER) should truncate towards zero like SQLite#694

Closed
sravan27 wants to merge 1 commit into
powersync-ja:mainfrom
sravan27:fix-cast-integer-truncation
Closed

fix(sync-rules): CAST(real AS INTEGER) should truncate towards zero like SQLite#694
sravan27 wants to merge 1 commit into
powersync-ja:mainfrom
sravan27:fix-cast-integer-truncation

Conversation

@sravan27

Copy link
Copy Markdown
Contributor

Problem

The number→integer path of cast() uses BigInt(Math.floor(value)), which rounds towards −∞. SQLite truncates a REAL towards zero when casting to INTEGER, so the two diverge on every negative non-integer real:

expression SQLite sync-rules (before)
CAST(-3.7 AS INTEGER) -3 -4
CAST(-0.9 AS INTEGER) 0 -1
CAST(-2.1 AS INTEGER) -2 -3

Since sync-rule expressions are evaluated client-side and must agree with the source database, an integer cast over a negative real (directly or via numeric coercion) can include/exclude rows differently on the client than on the server.

Fix

Use Math.trunc instead of Math.floor. Positive reals are unaffected (floor == trunc there), and the string/bigint cast paths are unchanged. Added regression tests; verified against the system sqlite3 as ground truth.

🤖 Generated with Claude Code

…ike SQLite

The number->integer cast used BigInt(Math.floor(value)), which rounds
towards negative infinity. SQLite truncates a REAL towards zero when
casting to INTEGER, so CAST(-3.7 AS INTEGER) is -3, not -4. Switched to
Math.trunc. Positive reals and string/bigint paths are unaffected.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Jun 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 647e696

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@powersync/service-sync-rules Patch
@powersync/service-core Patch
@powersync/lib-services-framework Patch
@powersync/service-module-convex Patch
@powersync/service-module-mongodb-storage Patch
@powersync/service-module-mongodb Patch
@powersync/service-module-mssql Patch
@powersync/service-module-mysql Patch
@powersync/service-module-postgres-storage Patch
@powersync/service-module-postgres Patch
@powersync/service-module-core Patch
@powersync/service-image Patch
test-client Patch
@powersync/service-rsocket-router Patch
@powersync/lib-service-mongodb Patch
@powersync/lib-service-postgres Patch
@powersync/service-schema Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Sravan Sridhar seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@rkistner

Copy link
Copy Markdown
Contributor

@sravan27 As mentioned before, we're moving towards using the node:sqlite engine, and are not fixing further compatibility differences with our JS engine. See #666.

@rkistner rkistner closed this Jun 27, 2026
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.

3 participants