Skip to content

b/79432277: Limit Queries to only a single array-contains clause. #835

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 4 commits into from
May 16, 2018
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
41 changes: 27 additions & 14 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1735,21 +1735,34 @@ export class Query implements firestore.Query {
}

private validateNewFilter(filter: Filter): void {
if (filter instanceof RelationFilter && filter.isInequality()) {
const existingField = this._query.getInequalityFilterField();
if (existingField !== null && !existingField.isEqual(filter.field)) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Invalid query. All where filters with an inequality' +
' (<, <=, >, or >=) must be on the same field. But you have' +
` inequality filters on '${existingField.toString()}'` +
` and '${filter.field.toString()}'`
);
}
if (filter instanceof RelationFilter) {
if (filter.isInequality()) {
const existingField = this._query.getInequalityFilterField();
if (existingField !== null && !existingField.isEqual(filter.field)) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Invalid query. All where filters with an inequality' +
' (<, <=, >, or >=) must be on the same field. But you have' +
` inequality filters on '${existingField.toString()}'` +
` and '${filter.field.toString()}'`
);
}

const firstOrderByField = this._query.getFirstOrderByField();
if (firstOrderByField !== null) {
this.validateOrderByAndInequalityMatch(filter.field, firstOrderByField);
const firstOrderByField = this._query.getFirstOrderByField();
if (firstOrderByField !== null) {
this.validateOrderByAndInequalityMatch(
filter.field,
firstOrderByField
);
}
} else if (filter.op === RelationOp.ARRAY_CONTAINS) {
if (this._query.hasArrayContainsFilter()) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Invalid query. Queries only support a single array-contains ' +
'filter.'
);
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/firestore/src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,16 @@ export class Query {
return null;
}

hasArrayContainsFilter(): boolean {
return (
this.filters.find(
filter =>
filter instanceof RelationFilter &&
filter.op === RelationOp.ARRAY_CONTAINS
) !== undefined
);
}

isDocumentQuery(): boolean {
return DocumentKey.isDocumentKey(this.path) && this.filters.length === 0;
}
Expand Down
15 changes: 15 additions & 0 deletions packages/firestore/test/integration/api/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,21 @@ apiDescribe('Validation:', persistence => {
}
);

validationIt(
persistence,
'with multiple array-contains filters fail.',
db => {
expect(() =>
db
.collection('test')
.where('foo', arrayContainsOp, 1)
.where('foo', arrayContainsOp, 2)
).to.throw(
'Invalid query. Queries only support a single array-contains filter.'
);
}
);

validationIt(
persistence,
'must not specify starting or ending point after orderBy',
Expand Down