Firestore: local matching of in/array-contains ignores int/double equality, and NaN matches range filters (getDocsFromServer hangs)

Open
#10,594 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
firebase, node.js, typescript
Domain
backend, database

Research direction

Start with packages/firestore/src/core/filter.ts and trace how in, array-contains, and range filters compare values; the issue also points to mixIntegerDouble and semanticsEqual in the query pipeline. Reproduce the numeric equality and NaN range cases with the Firestore emulator using the supplied script. Done means local matching agrees with the backend for integer/double equality, signed zero, and NaN range filters, and the reported getDocsFromServer query resolves.

Written by the indexing model from the issue text.

Description

api: firestore needs-attention question Repro Needed
Operating System

Linux (Debian 12, kernel 6.1)

Environment (if applicable)

Node.js 26.13.0, Firestore emulator cloud-firestore-emulator-v1.22.0 (firebase-tools 15.35.0)

Firebase SDK Version

13.2.0 (@firebase/firestore 4.19.1)

Firebase SDK Product(s)

Firestore

Project Tooling

Plain Node.js ESM script, no bundler.

Detailed Problem Description

The SDK's local query matching disagrees with the backend on numbers:

  1. in, not-in, array-contains and array-contains-any compare with strict valueEquals, so integer 1 does not match double 1.0 and -0 does not match 0. == uses valueCompare and does match them. The backend returns the documents, and the SDK then drops them from the getDocsFromServer result.
  2. Range filters use valueCompare, where NaN sorts before every number, so a cached { v: NaN } matches v <= 5 locally. The backend excludes it, the view never becomes consistent, and getDocsFromServer never resolves (an onSnapshot listener stays on fromCache: true).

Output of the script below:

v == 1: [ 'dbl1', 'int1' ]
v in [1]: [ 'int1' ]
a array-contains 1: [ 'int1' ]
v == -0: [ 'dbl0', 'int0' ]
v in [-0]: []
v <= 5 (cache): [ 'minf', 'nan', 'one' ]
v <= 5 (server): TIMEOUT

Expected: v in [1] and a array-contains 1 return [ 'dbl1', 'int1' ] like v == 1; v in [-0] returns [ 'dbl0', 'int0' ] like v == -0; v <= 5 returns [ 'minf', 'one' ] from both cache and server. Without the NaN document the last query resolves immediately. The docs define one numeric order for integers and doubles, with NaN only equal to NaN: https://firebase.google.com/docs/firestore/manage-data/data-types

The code is unchanged on main (8f4977509): packages/firestore/src/core/filter.ts uses arrayValueContains (strict equality) for the in/array-contains filters and valueCompare for ranges. Using valueEquals with mixIntegerDouble and semanticsEqual (as the pipeline code already does) and excluding NaN from range comparisons would match the backend.

Steps and code to reproduce issue
  1. npm i [email protected] and start the Firestore emulator on 127.0.0.1:8580 (firebase emulators:start --only firestore with {"emulators": {"firestore": {"port": 8580}}} in firebase.json).
  2. Save as repro.mjs and run node repro.mjs:
import { initializeApp } from "firebase/app";
import {
  initializeFirestore, connectFirestoreEmulator, collection, doc, setDoc,
  getDocsFromServer, getDocsFromCache, query, where,
} from "firebase/firestore";

const PROJ = "demo-test";
const BASE = `http://127.0.0.1:8580/v1/projects/${PROJ}/databases/(default)/documents`;
const db = initializeFirestore(initializeApp({ projectId: PROJ }), {});
connectFirestoreEmulator(db, "127.0.0.1", 8580);
const ids = (snap) => snap.docs.map((d) => d.id).sort();
const timeout = (p) => Promise.race([p, new Promise((r) => setTimeout(() => r("TIMEOUT"), 8000))]);

// 1. Integer 1 / double 1.0 and integer 0 / double 0.0, written over REST
//    (as the Admin, Python or Java SDKs can store them).
const c1 = "c1_" + Date.now();
const rest = (id, v) =>
  fetch(`${BASE}/${c1}?documentId=${id}`, {
    method: "POST",
    headers: { "Content-Type": "application/json", Authorization: "Bearer owner" },
    body: JSON.stringify({ fields: { v, a: { arrayValue: { values: [v] } } } }),
  });
await rest("int1", { integerValue: "1" });
await rest("dbl1", { doubleValue: 1.0 });
await rest("int0", { integerValue: "0" });
await rest("dbl0", { doubleValue: 0.0 });

const col1 = collection(db, c1);
console.log("v == 1:", ids(await getDocsFromServer(query(col1, where("v", "==", 1)))));
console.log("v in [1]:", ids(await getDocsFromServer(query(col1, where("v", "in", [1])))));
console.log("a array-contains 1:", ids(await getDocsFromServer(query(col1, where("a", "array-contains", 1)))));
console.log("v == -0:", ids(await getDocsFromServer(query(col1, where("v", "==", -0)))));
console.log("v in [-0]:", ids(await getDocsFromServer(query(col1, where("v", "in", [-0])))));

// 2. NaN with a range filter, after the documents are in the local cache.
const c2 = "c2_" + Date.now();
await setDoc(doc(db, c2, "nan"), { v: NaN });
await setDoc(doc(db, c2, "one"), { v: 1 });
await setDoc(doc(db, c2, "minf"), { v: -Infinity });
const le5 = query(collection(db, c2), where("v", "<=", 5));
console.log("v <= 5 (cache):", ids(await getDocsFromCache(le5)));
console.log("v <= 5 (server):", await timeout(getDocsFromServer(le5).then(ids)));
process.exit(0);

Credit: https://ucsbplse.github.io/

Dominant language
TypeScript
Stars
5.2k
Forks
1k
Avg merge
2d 2h
Merged PRs (30d)
77

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from firebase/firebase-js-sdk

All issues in firebase/firebase-js-sdk

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.