Skip to content

fix(sync-rules): length() and instr() should count code points to match SQLite#693

Closed
sravan27 wants to merge 1 commit into
powersync-ja:mainfrom
sravan27:fix-length-instr-codepoints
Closed

fix(sync-rules): length() and instr() should count code points to match SQLite#693
sravan27 wants to merge 1 commit into
powersync-ja:mainfrom
sravan27:fix-length-instr-codepoints

Conversation

@sravan27

Copy link
Copy Markdown
Contributor

Follow-up to the substring() code-point fix — the same UTF-16-vs-code-point divergence affects two sibling functions.

Problem

length() used JS String.length (UTF-16 code units) and instr() used String.indexOf (a UTF-16 index), so both diverge from SQLite for text containing non-BMP characters (emoji, mathematical alphanumerics, etc.):

expression SQLite sync-rules (before)
length('😀') 1 2
length('a😀b') 3 4
instr('😀x', 'x') 2 3

Because sync-rule expressions are evaluated client-side and must agree with the source database, a filter like length(name) > 10 or instr(...) over text containing emoji could include/exclude rows differently on the client than on the server — silent sync divergence.

Fix

Both functions now iterate by code point (spreading the string / counting code points in the prefix), matching SQLite and the existing substring() behaviour. Blob handling is unchanged (still byte counts, which is correct per SQLite).

Tests

Added regression cases to sql_functions.test.ts for length and instr covering non-BMP characters, including the case where a non-BMP character precedes the match (the one that actually diverges). Full sync-rules suite passes.

Verified against the system sqlite3 as ground truth.

🤖 Generated with Claude Code

…ch SQLite

length() used JS String.length (UTF-16 code units) and instr() used
String.indexOf (UTF-16 index), so both diverged from SQLite for text
containing non-BMP characters (e.g. emoji): length('😀') returned 2
instead of 1, and instr() positions were shifted by non-BMP characters
before the match. Both now iterate by code point, matching SQLite and the
existing substring() behaviour. Blob handling (byte counts) is unchanged.

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: 2e6fd14

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