-
Notifications
You must be signed in to change notification settings - Fork 912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: sort strings in UTF-8 encoded byte order with lazy encoding #8787
base: main
Are you sure you want to change the base?
Conversation
Size Report 1Affected Products
Test Logs |
Size Analysis Report 1This report is too large (575,697 characters) to be displayed here in a GitHub comment. Please use the below link to see the full report on Google Cloud Storage.Test Logs |
🦋 Changeset detectedLatest commit: 9174b34 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
packages/firestore/src/util/misc.ts
Outdated
for ( | ||
let j = 0; | ||
j < Math.min(leftBytes.length, rightBytes.length); | ||
j++ | ||
) { | ||
const comp = primitiveComparator(leftBytes[j], rightBytes[j]); | ||
if (comp !== 0) { | ||
return comp; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC we're having this loop because we can't use Buffer.from(...)
in the browser (outside of Node). If leftBytes.length
is not equal to rightBytes.length
and the first Math.min(...)
bytes do match, then this loop won't return a comparison. So the code falls through to the primitiveComparator
(line 112) way more often than it should.
- can we use the
compareBlobs
function? - if not, can you take this loop out of here and make a helper function that compares 2 Uint8Arrays byte by byte and also considers the length of them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we cannot import compareBlobs as it would create circular dependency due to the ByteString.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Please check the GitHub Actions failures.
Strings should be sorted in UTF-8 encoded byte order. Public document: https://cloud.google.com/firestore/docs/concepts/data-types#data_types
SDK sorts strings using built in comparator method, which sorts lexicographically, and leads to mismatch between server and sdk when special characters are present. This PR fixes the string order mismatches on document field, map key, and document key.
The previous fix added created a performance issue due to expensive UTF-8 encoding and reverted, #8774, #8778.
compareUtf8Strings
is updated to use lazy encoding instead.b/329441702