Skip to content
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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

milaGGL
Copy link
Contributor

@milaGGL milaGGL commented Feb 11, 2025

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

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Feb 11, 2025

Size Report 1

Affected Products

  • @firebase/app-check

    TypeBase (3418ef8)Merge (8a720fc)Diff
    browser26.3 kB26.4 kB+128 B (+0.5%)
    main27.3 kB27.4 kB+128 B (+0.5%)
    module26.3 kB26.4 kB+128 B (+0.5%)
  • @firebase/auth

    TypeBase (3418ef8)Merge (8a720fc)Diff
    browser189 kB189 kB+281 B (+0.1%)
    cordova164 kB164 kB+281 B (+0.2%)
    main145 kB146 kB+291 B (+0.2%)
    module189 kB189 kB+281 B (+0.1%)
    react-native164 kB164 kB+291 B (+0.2%)
  • @firebase/auth-cordova

    TypeBase (3418ef8)Merge (8a720fc)Diff
    browser164 kB164 kB+281 B (+0.2%)
    module164 kB164 kB+281 B (+0.2%)
  • @firebase/auth-web-extension

    TypeBase (3418ef8)Merge (8a720fc)Diff
    browser140 kB141 kB+281 B (+0.2%)
    main158 kB158 kB+295 B (+0.2%)
    module140 kB141 kB+281 B (+0.2%)
  • @firebase/auth/internal

    TypeBase (3418ef8)Merge (8a720fc)Diff
    browser199 kB200 kB+281 B (+0.1%)
    main172 kB172 kB+297 B (+0.2%)
    module199 kB200 kB+281 B (+0.1%)
  • @firebase/database

    TypeBase (3418ef8)Merge (8a720fc)Diff
    browser249 kB249 kB+300 B (+0.1%)
    main254 kB254 kB+295 B (+0.1%)
    module249 kB249 kB+300 B (+0.1%)
  • @firebase/database-compat/standalone

    TypeBase (3418ef8)Merge (8a720fc)Diff
    main366 kB366 kB+295 B (+0.1%)
  • @firebase/firestore

    TypeBase (3418ef8)Merge (8a720fc)Diff
    browser380 kB381 kB+896 B (+0.2%)
    main589 kB590 kB+1.19 kB (+0.2%)
    module380 kB381 kB+896 B (+0.2%)
    react-native380 kB381 kB+896 B (+0.2%)
  • @firebase/firestore-lite

    TypeBase (3418ef8)Merge (8a720fc)Diff
    browser112 kB113 kB+885 B (+0.8%)
    main154 kB155 kB+1.24 kB (+0.8%)
    module112 kB113 kB+885 B (+0.8%)
    react-native112 kB113 kB+885 B (+0.8%)
  • @firebase/remote-config

    TypeBase (3418ef8)Merge (8a720fc)Diff
    browser21.7 kB23.2 kB+1.51 kB (+7.0%)
    main22.9 kB24.4 kB+1.51 kB (+6.6%)
    module21.7 kB23.2 kB+1.51 kB (+7.0%)
  • @firebase/vertexai

    TypeBase (3418ef8)Merge (8a720fc)Diff
    browser29.0 kB29.2 kB+196 B (+0.7%)
    main29.9 kB30.0 kB+196 B (+0.7%)
    module29.0 kB29.2 kB+196 B (+0.7%)
  • bundle

    29 size changes

    TypeBase (3418ef8)Merge (8a720fc)Diff
    app-check (CustomProvider)37.4 kB37.5 kB+102 B (+0.3%)
    app-check (ReCaptchaEnterpriseProvider)39.9 kB40.0 kB+102 B (+0.3%)
    app-check (ReCaptchaV3Provider)39.9 kB40.0 kB+102 B (+0.3%)
    auth (GoogleFBTwitterGitHubPopup)104 kB104 kB+145 B (+0.1%)
    database (Append to a list of data)150 kB150 kB+172 B (+0.1%)
    database (Filtering data)148 kB149 kB+172 B (+0.1%)
    database (Listen for child events)165 kB165 kB+172 B (+0.1%)
    database (Listen for value events + Detach listeners)165 kB165 kB+172 B (+0.1%)
    database (Listen for value events)165 kB165 kB+172 B (+0.1%)
    database (Read data once)164 kB164 kB+172 B (+0.1%)
    database (Save data as transactions)167 kB167 kB+172 B (+0.1%)
    database (Sort data)150 kB150 kB+172 B (+0.1%)
    database (Write data)149 kB149 kB+172 B (+0.1%)
    firestore (CSI Auto Indexing Disable and Delete)271 kB272 kB+661 B (+0.2%)
    firestore (CSI Auto Indexing Enable)271 kB272 kB+661 B (+0.2%)
    firestore (Persistence)302 kB303 kB+624 B (+0.2%)
    firestore (Query Cursors)249 kB250 kB+624 B (+0.3%)
    firestore (Query)247 kB248 kB+624 B (+0.3%)
    firestore (Read data once)235 kB236 kB+624 B (+0.3%)
    firestore (Read Write w Persistence)327 kB328 kB+624 B (+0.2%)
    firestore (Realtime updates)237 kB238 kB+624 B (+0.3%)
    firestore (Transaction)214 kB215 kB+661 B (+0.3%)
    firestore (Write data)214 kB214 kB+661 B (+0.3%)
    firestore-lite (Query Cursors)103 kB104 kB+635 B (+0.6%)
    firestore-lite (Query)99.2 kB99.8 kB+635 B (+0.6%)
    firestore-lite (Read data once)74.6 kB75.3 kB+635 B (+0.9%)
    firestore-lite (Transaction)99.9 kB101 kB+635 B (+0.6%)
    firestore-lite (Write data)84.2 kB84.9 kB+635 B (+0.8%)
    remote-config (getAndFetch)47.7 kB48.8 kB+1.13 kB (+2.4%)

  • firebase

    15 size changes

    TypeBase (3418ef8)Merge (8a720fc)Diff
    firebase-app-check-compat.js22.6 kB22.7 kB+89 B (+0.4%)
    firebase-app-check.js24.9 kB25.0 kB+101 B (+0.4%)
    firebase-auth-compat.js140 kB140 kB+418 B (+0.3%)
    firebase-auth-cordova.js137 kB137 kB+154 B (+0.1%)
    firebase-auth-web-extension.js119 kB119 kB+154 B (+0.1%)
    firebase-auth.js155 kB155 kB+154 B (+0.1%)
    firebase-compat.js791 kB793 kB+1.44 kB (+0.2%)
    firebase-database-compat.js164 kB164 kB+442 B (+0.3%)
    firebase-database.js187 kB187 kB+513 B (+0.3%)
    firebase-firestore-compat.js338 kB339 kB+612 B (+0.2%)
    firebase-firestore-lite.js130 kB131 kB+885 B (+0.7%)
    firebase-firestore.js439 kB440 kB+896 B (+0.2%)
    firebase-remote-config-compat.js27.8 kB28.3 kB+439 B (+1.6%)
    firebase-remote-config.js31.1 kB32.7 kB+1.54 kB (+4.9%)
    firebase-vertexai.js24.2 kB24.4 kB+195 B (+0.8%)

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/m5RBS613eR.html

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Feb 11, 2025

Size Analysis Report 1

This report is too large (572,783 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

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/IAvJBxrIyh.html

@milaGGL milaGGL changed the title Fix: change the compareUtf8Strings to lazy encoding Fix: sort strings in UTF-8 encoded byte order with lazy encoding Feb 11, 2025
Copy link

changeset-bot bot commented Feb 11, 2025

🦋 Changeset detected

Latest commit: 9653bdc

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

This PR includes changesets to release 3 packages
Name Type
@firebase/firestore Patch
firebase Patch
@firebase/firestore-compat 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

@milaGGL milaGGL requested a review from a team as a code owner February 19, 2025 15:17
}
}

// Compare lengths if all bytes are equal
Copy link
Contributor

Choose a reason for hiding this comment

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

Under what circumstances would all bytes be equal? If there were all equal, then wouldn't the "if" condition on line 85 have evaluated to false?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, IIUC since leftCodePoint !== rightCodePoint, comparison on line 101 should be non-zero for at least one iteration of the loop on line 96.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope, there are some cases where trailing surrogate gets falsy involved, and being misrepresented by bytes, which could lead to 2 equal byte array for 2 different code points. That's why the unit tests were failing in android sdk.

Copy link
Contributor

Choose a reason for hiding this comment

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

😮

@milaGGL milaGGL requested a review from dconeybe February 20, 2025 21:27
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.

4 participants