Skip to content

Fix NotInFilter matching #1947

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 1 commit into from
Sep 9, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ public void testQueriesCanUseNotInFilters() {
// With Null.
List<Object> nullArray = new ArrayList<>();
nullArray.add(null);
snapshot = waitFor(collection.whereNotIn("key", nullArray).get());
snapshot = waitFor(collection.whereNotIn("zip", nullArray).get());
assertEquals(new ArrayList<>(), querySnapshotToValues(snapshot));

// With NaN.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class NotInFilter extends FieldFilter {

@Override
public boolean matches(Document doc) {
if (Values.contains(getValue().getArrayValue(), Values.NULL_VALUE)) {
return false;
}
Value other = doc.getField(getField());
return other != null && !Values.contains(getValue().getArrayValue(), other);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@
import com.google.firestore.v1.Write;
import com.google.protobuf.ByteString;
import com.google.protobuf.Int32Value;
import com.google.protobuf.NullValue;
import com.google.protobuf.Timestamp;
import com.google.type.LatLng;
import io.grpc.Status;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -706,6 +708,31 @@ public void testNotInSerialization() {
assertTrue(roundTripped instanceof NotInFilter);
}

@Test
public void testNotInWithNullSerialization() {
List<Object> nullArray = new ArrayList<>();
nullArray.add(null);
FieldFilter inputFilter = filter("field", "not-in", nullArray);
StructuredQuery.Filter apiFilter = serializer.encodeUnaryOrFieldFilter(inputFilter);

ArrayValue.Builder notInFilterValue =
ArrayValue.newBuilder().addValues(Value.newBuilder().setNullValue(NullValue.NULL_VALUE));
StructuredQuery.Filter expectedFilter =
Filter.newBuilder()
.setFieldFilter(
StructuredQuery.FieldFilter.newBuilder()
.setField(FieldReference.newBuilder().setFieldPath("field"))
.setOp(Operator.NOT_IN)
.setValue(Value.newBuilder().setArrayValue(notInFilterValue))
.build())
.build();

assertEquals(expectedFilter, apiFilter);
FieldFilter roundTripped = serializer.decodeFieldFilter(apiFilter.getFieldFilter());
assertEquals(roundTripped, inputFilter);
assertTrue(roundTripped instanceof NotInFilter);
}

@Test
public void testArrayContainsAnySerialization() {
FieldFilter inputFilter = filter("field", "array-contains-any", asList(42));
Expand Down