Skip to content

Markduckworth/or queries 4274 #6896

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
Dec 21, 2022
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
5 changes: 5 additions & 0 deletions .changeset/quick-radios-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": patch
---

Update canonifyFilter to compute the canonization for flat conjunctions the same as implicit AND queries.
8 changes: 8 additions & 0 deletions packages/firestore/src/core/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ export function canonifyFilter(filter: Filter): string {
filter.op.toString() +
canonicalId(filter.value)
);
} else if (compositeFilterIsFlatConjunction(filter)) {
// Older SDK versions use an implicit AND operation between their filters.
// In the new SDK versions, the developer may use an explicit AND filter.
// To stay consistent with the old usages, we add a special case to ensure
// the canonical ID for these two are the same. For example:
// `col.whereEquals("a", 1).whereEquals("b", 2)` should have the same
// canonical ID as `col.where(and(equals("a",1), equals("b",2)))`.
return filter.filters.map(filter => canonifyFilter(filter)).join(',');
Copy link
Contributor

@ehsannas ehsannas Dec 21, 2022

Choose a reason for hiding this comment

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

it's interesting that the canonical id of a==1 && b==1 is different strings in web and android.
In Android it'll be a==1b==1. In the Web, it'll be a==1,b==1. Nothing wrong with either one. I just checked target.ts (canonifyTarget) to make sure this is correct.

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 had the same surprise. Tests caught it. Thanks for the thorough review!

} else {
// filter instanceof CompositeFilter
const canonicalIdsString = filter.filters
Expand Down
26 changes: 14 additions & 12 deletions packages/firestore/src/lite-api/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,14 @@ export type QueryFilterConstraint =
| QueryCompositeFilterConstraint;

/**
* Creates a {@link QueryCompositeFilterConstraint} that performs a logical OR
* of all the provided {@link QueryFilterConstraint}s.
* Creates a new {@link QueryCompositeFilterConstraint} that is a disjunction of
* the given filter constraints. A disjunction filter includes a document if it
* satisfies any of the given filters.
*
* @param queryConstraints - Optional. The {@link QueryFilterConstraint}s
* for OR operation. These must be created with calls to {@link where},
* {@link or}, or {@link and}.
* @returns The created {@link QueryCompositeFilterConstraint}.
* @param queryConstraints - Optional. The list of
* {@link QueryFilterConstraint}s to perform a disjunction for. These must be
* created with calls to {@link where}, {@link or}, or {@link and}.
* @returns The newly created {@link QueryCompositeFilterConstraint}.
* @internal TODO remove this internal tag with OR Query support in the server
*/
export function or(
Expand All @@ -388,13 +389,14 @@ export function or(
}

/**
* Creates a {@link QueryCompositeFilterConstraint} that performs a logical AND
* of all the provided {@link QueryFilterConstraint}s.
* Creates a new {@link QueryCompositeFilterConstraint} that is a conjunction of
* the given filter constraints. A conjunction filter includes a document if it
* satisfies all of the given filters.
*
* @param queryConstraints - Optional. The {@link QueryFilterConstraint}s
* for AND operation. These must be created with calls to {@link where},
* {@link or}, or {@link and}.
* @returns The created {@link QueryCompositeFilterConstraint}.
* @param queryConstraints - Optional. The list of
* {@link QueryFilterConstraint}s to perform a conjunction for. These must be
* created with calls to {@link where}, {@link or}, or {@link and}.
* @returns The newly created {@link QueryCompositeFilterConstraint}.
* @internal TODO remove this internal tag with OR Query support in the server
*/
export function and(
Expand Down
14 changes: 13 additions & 1 deletion packages/firestore/test/unit/core/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import {
FieldFilter,
Operator
} from '../../../src/core/filter';
import { andFilter, filter, orFilter } from '../../util/helpers';
import { queryToTarget } from '../../../src/core/query';
import { canonifyTarget } from '../../../src/core/target';
import { andFilter, filter, orFilter, query } from '../../util/helpers';

describe('FieldFilter', () => {
it('exposes field filter members', () => {
Expand Down Expand Up @@ -93,4 +95,14 @@ describe('CompositeFilter', () => {
expect(compositeFilterIsFlat(orFilter2)).false;
expect(compositeFilterIsFlatConjunction(orFilter2)).false;
});

it('computes canonical id of flat conjunctions', () => {
const target1 = query('col', a, b, c);

const target2 = query('col', andFilter(a, b, c));

expect(canonifyTarget(queryToTarget(target1))).to.equal(
canonifyTarget(queryToTarget(target2))
);
});
});