Skip to content

Commit 6ac9d7f

Browse files
Fix IN filters (#3002)
1 parent e8ac306 commit 6ac9d7f

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteIndexManager.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,7 @@ private SQLitePersistence.Query generateQuery(
243243
// The number of total statements we union together. This is similar to a distributed normal
244244
// form, but adapted for array values. We create a single statement per value in an
245245
// ARRAY_CONTAINS or ARRAY_CONTAINS_ANY filter combined with the values from the query bounds.
246-
int statementCount =
247-
max(arrayValues.size(), 1)
248-
* lowerBounds.length
249-
* (upperBounds == null ? 1 : upperBounds.length);
250-
// The number of "question marks" per single statement
246+
int statementCount = max(arrayValues.size(), 1) * lowerBounds.length;
251247
int bindsPerStatement = 2 + (arrayValues.isEmpty() ? 0 : 1) + (upperBounds != null ? 1 : 0);
252248
Object[] bindArgs = new Object[statementCount * bindsPerStatement];
253249

@@ -256,7 +252,9 @@ private SQLitePersistence.Query generateQuery(
256252
StringBuilder statement = new StringBuilder();
257253
statement.append(
258254
"SELECT document_name, directional_value FROM index_entries WHERE index_id = ? ");
259-
statement.append(arrayValues.isEmpty() ? "AND array_value IS NULL " : "AND array_value = ? ");
255+
if (!arrayValues.isEmpty()) {
256+
statement.append("AND array_value = ? ");
257+
}
260258
statement.append("AND directional_value ").append(lowerBoundOp).append(" ? ");
261259
if (upperBounds != null) {
262260
statement.append("AND directional_value ").append(upperBoundOp).append(" ? ");
@@ -289,17 +287,18 @@ private int fillBounds(
289287
@Nullable Object arrayValue,
290288
Object[] lowerBounds,
291289
@Nullable Object[] upperBounds) {
290+
hardAssert(
291+
upperBounds == null || upperBounds.length == lowerBounds.length,
292+
"Length of upper and lower bound should match");
292293
// Add bind variables for each combination of arrayValue, lowerBound and upperBound.
293-
for (Object lower : lowerBounds) {
294-
for (int i = 0; i < (upperBounds != null ? upperBounds.length : 1); ++i) {
295-
bindArgs[offset++] = indexId;
296-
if (arrayValue != null) {
297-
bindArgs[offset++] = arrayValue;
298-
}
299-
bindArgs[offset++] = lower;
300-
if (upperBounds != null) {
301-
bindArgs[offset++] = upperBounds[i];
302-
}
294+
for (int i = 0; i < lowerBounds.length; ++i) {
295+
bindArgs[offset++] = indexId;
296+
if (arrayValue != null) {
297+
bindArgs[offset++] = arrayValue;
298+
}
299+
bindArgs[offset++] = lowerBounds[i];
300+
if (upperBounds != null) {
301+
bindArgs[offset++] = upperBounds[i];
303302
}
304303
}
305304
return offset;

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteSchema.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ private void createFieldIndex() {
361361
+ "update_time_nanos INTEGER, "
362362
+ "PRIMARY KEY (index_id))");
363363

364+
// The index entries table only has a single primary index. `array_value` should be set
365+
// for all queries.
364366
db.execSQL(
365367
"CREATE TABLE index_entries ("
366368
+ "index_id INTEGER, "

firebase-firestore/src/test/java/com/google/firebase/firestore/local/SQLiteIndexManagerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ public void testRangeWithBoundFilter() {
193193
@Test
194194
public void testInFilter() {
195195
setUpSingleValueFilter();
196-
Query query = query("coll").filter(filter("count", "in", Arrays.asList(1, 2)));
197-
verifyResults(query, "coll/doc1", "coll/doc2");
196+
Query query = query("coll").filter(filter("count", "in", Arrays.asList(1, 3)));
197+
verifyResults(query, "coll/doc1", "coll/doc3");
198198
}
199199

200200
@Test

0 commit comments

Comments
 (0)