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: Fields used in whereIn should be equality filters #216

Merged
merged 3 commits into from May 14, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -104,7 +104,7 @@ Value encodeValue() {
return encodedValue;
}

abstract boolean isEqualsFilter();
abstract boolean isInequalityFilter();

abstract Filter toProto();
}
Expand All @@ -117,8 +117,8 @@ private static class UnaryFilter extends FieldFilter {
}

@Override
boolean isEqualsFilter() {
return true;
boolean isInequalityFilter() {
return false;
}

Filter toProto() {
Expand Down Expand Up @@ -148,8 +148,11 @@ private static class ComparisonFilter extends FieldFilter {
}

@Override
boolean isEqualsFilter() {
return operator.equals(EQUAL);
boolean isInequalityFilter() {
return operator.equals(GREATER_THAN)
|| operator.equals(GREATER_THAN_OR_EQUAL)
|| operator.equals(LESS_THAN)
|| operator.equals(LESS_THAN_OR_EQUAL);
}

Filter toProto() {
Expand Down Expand Up @@ -306,7 +309,7 @@ private ImmutableList<FieldOrder> createImplicitOrderBy() {
if (implicitOrders.isEmpty()) {
// If no explicit ordering is specified, use the first inequality to define an implicit order.
for (FieldFilter fieldFilter : options.getFieldFilters()) {
if (!fieldFilter.isEqualsFilter()) {
if (fieldFilter.isInequalityFilter()) {
implicitOrders.add(new FieldOrder(fieldFilter.fieldPath, Direction.ASCENDING));
break;
}
Expand Down
Expand Up @@ -303,6 +303,39 @@ public void inQueriesWithReferenceArray() throws Exception {
assertEquals(expectedRequest, runQuery.getValue());
}

@Test
public void inQueriesFieldsNotUsedInOrderBy() throws Exception {
Copy link

Choose a reason for hiding this comment

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

You may also want to validate that unary filters are handled properly and that inequalities result in an implicit order by if that's not already present.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think unary filters are basically isNull and isNan, which are equality checks by nature.

doAnswer(queryResponse())
.when(firestoreMock)
.streamRequest(
runQuery.capture(),
streamObserverCapture.capture(),
Matchers.<ServerStreamingCallable>any());

// Field "foo" used in `whereIn` should not appear in implicit orderBys in the resulting query.
query
.whereIn(FieldPath.of("foo"), Arrays.<Object>asList("value1", "value2"))
.startAt(SINGLE_FIELD_SNAPSHOT)
.get()
.get();

Value value =
Value.newBuilder()
.setArrayValue(
ArrayValue.newBuilder()
.addValues(Value.newBuilder().setStringValue("value1").build())
.addValues(Value.newBuilder().setStringValue("value2").build())
.build())
.build();
RunQueryRequest expectedRequest =
query(
filter(Operator.IN, "foo", value),
order("__name__", Direction.ASCENDING),
startAt(reference(DOCUMENT_NAME), true));

assertEquals(expectedRequest, runQuery.getValue());
}

@Test
public void validatesInQueries() {
try {
Expand Down