Skip to content

Commit b0e3f1e

Browse files
committed
Fix local mode filters cross-matching booleans and integers
Python treats bool as a subclass of int (True == 1, False == 0), but Qdrant keeps booleans and integers as distinct payload value types. Local mode compared them with a plain `==` / `in` / `isinstance(value, (int, float))`, so: - MatchValue(value=1) matched a payload of True, and MatchValue(value=True) matched a payload of 1 (same for 0 / False) - MatchAny / MatchExcept cross-matched the same way - Range matched booleans as if they were 0 / 1 The server never cross-matches these (its ValueVariants keeps Integer and Bool distinct, and booleans are not numeric for range conditions). Add a type-aware equality helper used by the value-match conditions, and exclude booleans from range checks. Adds an in-memory regression test.
1 parent b4713cd commit b0e3f1e

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎qdrant_client/local/payload_filters.py‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ def check_range_interface(condition: models.RangeInterface, value: Any) -> bool:
104104

105105

106106
def check_range(condition: models.Range, value: Any) -> bool:
107-
if not isinstance(value, (int, float)):
107+
# bool is a subclass of int in Python, but booleans are not numeric values
108+
# in Qdrant and must not be matched by a range condition.
109+
if isinstance(value, bool) or not isinstance(value, (int, float)):
108110
return False
109111
return (
110112
(condition.lt is None or value < condition.lt)
@@ -146,17 +148,25 @@ def make_condition_tz_aware(dt: datetime | date | None) -> datetime | None:
146148
)
147149

148150

151+
def values_match(value: Any, other: Any) -> bool:
152+
# bool is a subclass of int in Python (True == 1), but Qdrant treats booleans
153+
# and integers as distinct payload value types, so they must not cross-match.
154+
if isinstance(value, bool) != isinstance(other, bool):
155+
return False
156+
return value == other
157+
158+
149159
def check_match(condition: models.Match, value: Any) -> bool:
150160
if isinstance(condition, models.MatchValue):
151-
return value == condition.value
161+
return values_match(value, condition.value)
152162
if isinstance(condition, models.MatchText):
153163
return isinstance(value, str) and condition.text in value
154164
if isinstance(condition, models.MatchTextAny):
155165
return isinstance(value, str) and any(word in value for word in condition.text_any.split())
156166
if isinstance(condition, models.MatchAny):
157-
return value in condition.any
167+
return any(values_match(value, v) for v in condition.any)
158168
if isinstance(condition, models.MatchExcept):
159-
return value not in condition.except_
169+
return not any(values_match(value, v) for v in condition.except_)
160170
raise ValueError(f"Unknown match condition: {condition}")
161171

162172

‎tests/test_in_memory.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,37 @@ def test_fusion_dbsf_score_threshold(qdrant: QdrantClient):
294294
f"Expected 3 points after filtering (threshold 1.0), got {len(result_with_threshold.points)}. "
295295
f"Scores: {[p.score for p in result_no_threshold.points]}"
296296
)
297+
298+
299+
def test_bool_and_int_filters_do_not_cross_match(qdrant: QdrantClient):
300+
# bool is a subclass of int in Python (True == 1), but Qdrant treats booleans
301+
# and integers as distinct payload value types, so a filter on one must not
302+
# match a payload value of the other.
303+
qdrant.create_collection(
304+
collection_name="bool_int",
305+
vectors_config=models.VectorParams(size=2, distance=models.Distance.COSINE),
306+
)
307+
qdrant.upsert(
308+
collection_name="bool_int",
309+
points=[
310+
models.PointStruct(id=1, vector=[0.1, 0.2], payload={"n": 1}),
311+
models.PointStruct(id=2, vector=[0.1, 0.2], payload={"n": True}),
312+
models.PointStruct(id=3, vector=[0.1, 0.2], payload={"n": 0}),
313+
models.PointStruct(id=4, vector=[0.1, 0.2], payload={"n": False}),
314+
],
315+
)
316+
317+
def matched_ids(condition: models.FieldCondition) -> list:
318+
points, _ = qdrant.scroll(
319+
collection_name="bool_int",
320+
scroll_filter=models.Filter(must=[condition]),
321+
limit=10,
322+
)
323+
return sorted(point.id for point in points)
324+
325+
assert matched_ids(models.FieldCondition(key="n", match=models.MatchValue(value=1))) == [1]
326+
assert matched_ids(models.FieldCondition(key="n", match=models.MatchValue(value=True))) == [2]
327+
assert matched_ids(models.FieldCondition(key="n", match=models.MatchValue(value=0))) == [3]
328+
assert matched_ids(models.FieldCondition(key="n", match=models.MatchValue(value=False))) == [4]
329+
assert matched_ids(models.FieldCondition(key="n", match=models.MatchAny(any=[1, 0]))) == [1, 3]
330+
assert matched_ids(models.FieldCondition(key="n", range=models.Range(gte=1, lte=1))) == [1]

0 commit comments

Comments
 (0)