Skip to content

fix: remove null value inclusion from whereNotEqualTo and whereNotIn filter results #6859

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

Merged
merged 5 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion firebase-firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Unreleased

* [fixed] Fixed the `null` value handling in `whereNotEqualTo` and `whereNotIn` filters.

# 25.1.3
* [fixed] Use lazy encoding in UTF-8 encoded byte comparison for strings to solve performance issues. [#6706](//github.com/firebase/firebase-android-sdk/pull/6706)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1647,4 +1647,53 @@ public void testOrderByEquality() {
Query query2 = collection.where(inArray("a", asList(2, 3))).orderBy("a");
checkOnlineAndOfflineResultsMatch(collection, query2, "doc6", "doc3");
}

@Test
public void testSDKUsesNotEqualFiltersSameAsServer() {
Map<String, Map<String, Object>> testDocs =
map(
"a", map("zip", Double.NaN),
"b", map("zip", 91102L),
"c", map("zip", 98101L),
"d", map("zip", "98101"),
"e", map("zip", asList(98101L)),
"f", map("zip", asList(98101L, 98102L)),
"g", map("zip", asList("98101", map("zip", 98101L))),
"h", map("zip", map("code", 500L)),
"i", map("zip", null),
"j", map("code", 500L));
CollectionReference collection = testCollectionWithDocs(testDocs);

Query query = collection.whereNotEqualTo("zip", 98101L);
checkOnlineAndOfflineResultsMatch(collection, query, "a", "b", "d", "e", "f", "g", "h");

query = collection.whereNotEqualTo("zip", Double.NaN);
checkOnlineAndOfflineResultsMatch(collection, query, "b", "c", "d", "e", "f", "g", "h");

query = collection.whereNotEqualTo("zip", null);
checkOnlineAndOfflineResultsMatch(collection, query, "a", "b", "c", "d", "e", "f", "g", "h");
}

@Test
public void testSDKUsesNotInFiltersSameAsServer() {
Map<String, Map<String, Object>> testDocs =
map(
"a", map("zip", Double.NaN),
"b", map("zip", 91102L),
"c", map("zip", 98101L),
"d", map("zip", "98101"),
"e", map("zip", asList(98101L)),
"f", map("zip", asList(98101L, 98102L)),
"g", map("zip", asList("98101", map("zip", 98101L))),
"h", map("zip", map("code", 500L)),
"i", map("zip", null),
"j", map("code", 500L));
CollectionReference collection = testCollectionWithDocs(testDocs);

Query query = collection.whereNotIn("zip", asList(98101L, 98103L, asList(98101L, 98102L)));
checkOnlineAndOfflineResultsMatch(collection, query, "a", "b", "d", "e", "g", "h");

query = collection.whereNotIn("zip", nullList());
checkOnlineAndOfflineResultsMatch(collection, query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ public boolean matches(Document doc) {
Value other = doc.getField(field);
// Types do not have to match in NOT_EQUAL filters.
if (operator == Operator.NOT_EQUAL) {
return other != null && this.matchesComparison(Values.compare(other, value));
return other != null
&& !other.hasNullValue()
&& this.matchesComparison(Values.compare(other, value));
}
// Only compare types with matching backend order (such as double and int).
return other != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public boolean matches(Document doc) {
return false;
}
Value other = doc.getField(getField());
return other != null && !Values.contains(getValue().getArrayValue(), other);
return other != null
&& !other.hasNullValue()
&& !Values.contains(getValue().getArrayValue(), other);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public void testNotInFilters() {

// Null match.
document = doc("collection/1", 0, map("zip", null));
assertTrue(query.matches(document));
assertFalse(query.matches(document));

// NaN match.
document = doc("collection/1", 0, map("zip", Double.NaN));
Expand Down Expand Up @@ -333,7 +333,7 @@ public void testNaNFilter() {
assertTrue(query.matches(doc3));
assertTrue(query.matches(doc4));
assertTrue(query.matches(doc5));
assertTrue(query.matches(doc6));
assertFalse(query.matches(doc6));
}

@Test
Expand Down
Loading