Skip to content

Commit a46885b

Browse files
committed
fix: remove null from != and not-in filters' result
1 parent d7a56a1 commit a46885b

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/QueryTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,4 +1630,56 @@ public void testOrderByEquality() {
16301630
Query query2 = collection.where(inArray("a", asList(2, 3))).orderBy("a");
16311631
checkOnlineAndOfflineResultsMatch(query2, "doc6", "doc3");
16321632
}
1633+
1634+
@Test
1635+
public void testSDKUsesNotEqualFiltersSameAsServer() {
1636+
Map<String, Map<String, Object>> testDocs =
1637+
map(
1638+
"a", map("zip", Double.NaN),
1639+
"b", map("zip", 91102L),
1640+
"c", map("zip", 98101L),
1641+
"d", map("zip", "98101"),
1642+
"e", map("zip", asList(98101L)),
1643+
"f", map("zip", asList(98101L, 98102L)),
1644+
"g", map("zip", asList("98101", map("zip", 98101L))),
1645+
"h", map("zip", map("code", 500L)),
1646+
"i", map("zip", null),
1647+
"j", map("code", 500L));
1648+
CollectionReference collection = testCollectionWithDocs(testDocs);
1649+
1650+
// populate cache with all documents first to ensure getDocsFromCache() scans all docs
1651+
waitFor(testCollectionWithDocs(testDocs).get());
1652+
1653+
Query query = collection.whereNotEqualTo("zip", 98101L);
1654+
checkOnlineAndOfflineResultsMatch(query, "a", "b", "d", "e", "f", "g", "h");
1655+
1656+
query = collection.whereNotEqualTo("zip", Double.NaN);
1657+
checkOnlineAndOfflineResultsMatch(query, "b", "c", "d", "e", "f", "g", "h");
1658+
}
1659+
1660+
@Test
1661+
public void testSDKUsesNotInFiltersSameAsServer() {
1662+
Map<String, Map<String, Object>> testDocs =
1663+
map(
1664+
"a", map("zip", Double.NaN),
1665+
"b", map("zip", 91102L),
1666+
"c", map("zip", 98101L),
1667+
"d", map("zip", "98101"),
1668+
"e", map("zip", asList(98101L)),
1669+
"f", map("zip", asList(98101L, 98102L)),
1670+
"g", map("zip", asList("98101", map("zip", 98101L))),
1671+
"h", map("zip", map("code", 500L)),
1672+
"i", map("zip", null),
1673+
"j", map("code", 500L));
1674+
CollectionReference collection = testCollectionWithDocs(testDocs);
1675+
1676+
// populate cache with all documents first to ensure getDocsFromCache() scans all docs
1677+
waitFor(testCollectionWithDocs(testDocs).get());
1678+
1679+
Query query = collection.whereNotIn("zip", asList(98101L, 98103L, asList(98101L, 98102L)));
1680+
checkOnlineAndOfflineResultsMatch(query, "a", "b", "d", "e", "g", "h");
1681+
1682+
query = collection.whereNotIn("zip", nullList());
1683+
checkOnlineAndOfflineResultsMatch(query);
1684+
}
16331685
}

firebase-firestore/src/main/java/com/google/firebase/firestore/core/FieldFilter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ public boolean matches(Document doc) {
115115
Value other = doc.getField(field);
116116
// Types do not have to match in NOT_EQUAL filters.
117117
if (operator == Operator.NOT_EQUAL) {
118-
return other != null && this.matchesComparison(Values.compare(other, value));
118+
return other != null
119+
&& !other.hasNullValue()
120+
&& this.matchesComparison(Values.compare(other, value));
119121
}
120122
// Only compare types with matching backend order (such as double and int).
121123
return other != null

firebase-firestore/src/main/java/com/google/firebase/firestore/core/NotInFilter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public boolean matches(Document doc) {
3434
return false;
3535
}
3636
Value other = doc.getField(getField());
37-
return other != null && !Values.contains(getValue().getArrayValue(), other);
37+
return other != null
38+
&& !other.hasNullValue()
39+
&& !Values.contains(getValue().getArrayValue(), other);
3840
}
3941
}

firebase-firestore/src/test/java/com/google/firebase/firestore/core/QueryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public void testNotInFilters() {
237237

238238
// Null match.
239239
document = doc("collection/1", 0, map("zip", null));
240-
assertTrue(query.matches(document));
240+
assertFalse(query.matches(document));
241241

242242
// NaN match.
243243
document = doc("collection/1", 0, map("zip", Double.NaN));
@@ -333,7 +333,7 @@ public void testNaNFilter() {
333333
assertTrue(query.matches(doc3));
334334
assertTrue(query.matches(doc4));
335335
assertTrue(query.matches(doc5));
336-
assertTrue(query.matches(doc6));
336+
assertFalse(query.matches(doc6));
337337
}
338338

339339
@Test

0 commit comments

Comments
 (0)