Skip to content

fix(sync-rules): length() should count Unicode code points to match SQLite - #664

Closed
sravan27 wants to merge 1 commit into
powersync-ja:mainfrom
sravan27:fix-sync-streams-length-code-points
Closed

sravan27 wants to merge 1 commit into
powersync-ja:mainfrom
sravan27:fix-sync-streams-length-code-points

Conversation

@sravan27

@sravan27 sravan27 commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Same silent-data-loss class as the merged Sync Streams fixes #644 / #645 / #646 / #647, the open #565 JOIN loud-error PR #662, and the ASCII upper/lower PR #663.

The bug

length() on text returned value.length — JavaScript's String.prototype.length, which counts UTF-16 code units. SQLite's length(X) returns the number of characters (Unicode code points).

For any string containing a non-BMP code point (emoji like 😀, CJK Extension B–G, ancient scripts) the two counts diverge: each non-BMP code point is one UTF-16 surrogate pair = 2 code units in JS but 1 character in SQLite.

Source value Server length() (JS) Client length() (SQLite) Bucket key match?
"hello" 5 5 ✅
"straße" 6 6 ✅ (ß is BMP)
"😀" 2 1 ❌ silent miss
"a😀b" 4 3 ❌ silent miss
"𐀀" (Linear B) 2 1 ❌ silent miss

A bucket-parameter or filter using length(...) silently routed any row containing such characters to a different bucket than the client query asks for.

Fix

A tiny countCodePoints(text) helper that iterates the string with for…of (which walks code points, not code units) and length uses it for the text case. The blob branch already matches SQLite (byte length).

Tests

New syncTest('length() counts Unicode code points, not UTF-16 code units (matches SQLite)') in sqlite_semantics.test.ts covering:

  • "hello" → 5n (ASCII control)
  • "straße" → 6n (BMP non-ASCII, unchanged)
  • "😀" → 1n (was 2n)
  • "a😀b" → 3n (was 4n)
  • "𐀀" (U+10000, Linear B) → 1n (was 2n)

Full sync_rules.test.ts: 40/40.

@changeset-bot

changeset-bot Bot commented Jun 6, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 80cd6ee

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

CLAassistant commented Jun 6, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@sravan27
sravan27 force-pushed the fix-sync-streams-length-code-points branch from e6f94fe to f0033dc Compare June 6, 2026 15:54
@sravan27
sravan27 force-pushed the fix-sync-streams-length-code-points branch from f0033dc to 5842027 Compare June 6, 2026 16:12
sravan27 pushed a commit to sravan27/powersync-service that referenced this pull request Jun 6, 2026
substring() used String.prototype.substring and .length, which count UTF-16 code units. For non-BMP code points (emoji 😀, CJK Extension B-G, ancient scripts) slicing in the middle of a surrogate pair returned a broken unpaired surrogate. Server-side substring output silently disagreed with SQLite client output.

Same silent-failure class as merged powersync-ja#644 / powersync-ja#645 / powersync-ja#646 / powersync-ja#647 and open PRs powersync-ja#662 (JOIN) / powersync-ja#663 (ASCII upper/lower) / powersync-ja#664 (length code points).
…QLite

JS String.prototype.length counts UTF-16 code units. SQLite length() returns characters (code points). For any non-BMP code point (emoji 😀, CJK Extension B-G, ancient scripts) the two diverge: 2 units vs 1 character. Bucket-key expressions like length(name) silently routed rows to the wrong bucket.

Same silent-failure class as merged powersync-ja#644 / powersync-ja#645 / powersync-ja#646 / powersync-ja#647, open powersync-ja#565 JOIN PR powersync-ja#662 and ASCII upper/lower PR powersync-ja#663.
@sravan27
sravan27 force-pushed the fix-sync-streams-length-code-points branch from 5842027 to 80cd6ee Compare June 7, 2026 09:12
sravan27 added a commit to sravan27/powersync-service that referenced this pull request Jun 7, 2026
substring() used String.prototype.substring and .length, which count UTF-16 code units. For non-BMP code points (emoji 😀, CJK Extension B-G, ancient scripts) slicing in the middle of a surrogate pair returned a broken unpaired surrogate. Server-side substring output silently disagreed with SQLite client output.

Same silent-failure class as merged powersync-ja#644 / powersync-ja#645 / powersync-ja#646 / powersync-ja#647 and open PRs powersync-ja#662 (JOIN) / powersync-ja#663 (ASCII upper/lower) / powersync-ja#664 (length code points).
@sravan27

sravan27 commented Jun 7, 2026

Copy link
Copy Markdown
Contributor Author

recheck

@sravan27

sravan27 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Acknowledged — closing reasoning carries over from #663 (Unicode-aware JS over ASCII-only SQLite is the preferred direction for the engine). Will park the SQLite-matching framing and look at the inverse: client-side overrides in powersync-js so the wa-sqlite client matches the Service's Unicode behavior. Thanks for the time.

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.

2 participants