Skip to content

Commit 8220a59

Browse files
anidotnetclaude
andcommitted
fix: array-field eq/in must match by containment on a collection scan
Array membership (field.eq(x) / field.in(..)) was evaluated element-wise on the index path but as whole-value equality (deepEquals) on the collection-scan path, so results depended on whether an index existed or was chosen by the planner. When both an array field and a range field are indexed, the planner claims the range index for a between and relegates the array-eq to a collection scan, which then silently matched nothing. Make EqualsFilter/InFilter apply honor array element containment, mirroring applyOnIndex, restoring index-independent results. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent f598991 commit 8220a59

3 files changed

Lines changed: 116 additions & 1 deletion

File tree

‎nitrite/src/main/java/org/dizitart/no2/filters/EqualsFilter.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,24 @@ public boolean apply(Pair<NitriteId, Document> element) {
4848

4949
Document document = element.getSecond();
5050
Object fieldValue = document.get(getField());
51-
return deepEquals(fieldValue, getValue());
51+
if (deepEquals(fieldValue, getValue())) {
52+
return true;
53+
}
54+
// An array/collection field matches by element containment, mirroring
55+
// applyOnIndex() (arrays are indexed element-wise). Without this,
56+
// field.eq(x) on a list field returns different results depending on
57+
// whether an index exists / is chosen by the planner: an indexed
58+
// array-eq that the planner relegates to a collection scan (e.g. when
59+
// a range filter on another field claims the index) would otherwise
60+
// silently match nothing.
61+
if (fieldValue instanceof Iterable) {
62+
for (Object element0 : (Iterable<?>) fieldValue) {
63+
if (deepEquals(element0, getValue())) {
64+
return true;
65+
}
66+
}
67+
}
68+
return false;
5269
}
5370

5471
@Override

‎nitrite/src/main/java/org/dizitart/no2/filters/InFilter.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ public boolean apply(Pair<NitriteId, Document> element) {
5858
Comparable<?> comparable = (Comparable<?>) fieldValue;
5959
return comparableSet.contains(comparable);
6060
}
61+
// `in` is multi-value `eq`, so it matches an array/collection field by
62+
// element containment for the same index-independence reason as
63+
// EqualsFilter: the index path already treats arrays element-wise, so
64+
// a collection scan must too.
65+
if (fieldValue instanceof Iterable) {
66+
for (Object element0 : (Iterable<?>) fieldValue) {
67+
if (element0 instanceof Comparable && comparableSet.contains(element0)) {
68+
return true;
69+
}
70+
}
71+
}
6172
return false;
6273
}
6374

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2017-2021 Nitrite author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.dizitart.no2.integration.collection;
18+
19+
import org.dizitart.no2.Nitrite;
20+
import org.dizitart.no2.collection.NitriteCollection;
21+
import org.dizitart.no2.index.IndexOptions;
22+
import org.dizitart.no2.index.IndexType;
23+
import org.junit.Test;
24+
25+
import java.util.Collections;
26+
27+
import static org.dizitart.no2.collection.Document.createDocument;
28+
import static org.dizitart.no2.filters.Filter.and;
29+
import static org.dizitart.no2.filters.FluentFilter.where;
30+
import static org.junit.Assert.assertEquals;
31+
32+
/**
33+
* A query's results must not depend on which indexes happen to exist.
34+
* {@code field.eq(x)} / {@code field.in(..)} on an array (list) field is
35+
* defined by the index path as element containment; before the fix the
36+
* collection-scan path did whole-value equality, so an indexed array-eq that
37+
* the planner relegated to a collection scan (e.g. when a range filter on
38+
* another field claimed the index) silently matched nothing.
39+
*/
40+
public class ArrayFieldIndexIndependenceTest {
41+
42+
// days 0 & 2 tagged "todo", days 1 & 3 tagged "misc"; created_at = day*1000
43+
private NitriteCollection seed(boolean tagsIndex, boolean createdIndex) {
44+
Nitrite db = Nitrite.builder().fieldSeparator(".").openOrCreate();
45+
NitriteCollection c = db.getCollection("array_index_independence");
46+
c.remove(org.dizitart.no2.filters.Filter.ALL);
47+
if (tagsIndex) c.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "tags");
48+
if (createdIndex) c.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "created_at");
49+
for (long day = 0; day < 4; day++) {
50+
String tag = (day % 2 == 0) ? "todo" : "misc";
51+
c.insert(createDocument("created_at", day * 1000L).put("tags", Collections.singletonList(tag)));
52+
}
53+
return c;
54+
}
55+
56+
@Test
57+
public void arrayEqMatchesByContainmentWithoutIndex() {
58+
NitriteCollection c = seed(false, false);
59+
assertEquals(2, c.find(where("tags").eq("todo")).size());
60+
assertEquals(0, c.find(where("tags").eq("nope")).size());
61+
}
62+
63+
@Test
64+
public void arrayEqResultIsIndexIndependent() {
65+
assertEquals(seed(false, false).find(where("tags").eq("todo")).size(),
66+
seed(true, false).find(where("tags").eq("todo")).size());
67+
}
68+
69+
@Test
70+
public void arrayEqCombinedWithRangeWhenBothFieldsIndexed() {
71+
NitriteCollection c = seed(true, true);
72+
// day 2 is the only "todo" inside [1000,3000]
73+
assertEquals(1, c.find(and(where("tags").eq("todo"), where("created_at").between(1000L, 3000L))).size());
74+
assertEquals(1, c.find(and(where("created_at").between(1000L, 3000L), where("tags").eq("todo"))).size());
75+
assertEquals(1, c.find(and(where("tags").eq("todo"),
76+
where("created_at").gte(1000L), where("created_at").lte(3000L))).size());
77+
assertEquals(1, c.find(and(where("tags").eq("todo"), where("created_at").gte(1000L))).size());
78+
}
79+
80+
@Test
81+
public void arrayInMatchesByContainmentOnCollectionScan() {
82+
NitriteCollection c = seed(false, true);
83+
assertEquals(2, c.find(where("tags").in("todo", "other")).size());
84+
assertEquals(1, c.find(and(where("tags").in("todo"),
85+
where("created_at").between(1000L, 3000L))).size());
86+
}
87+
}

0 commit comments

Comments
 (0)