Skip to content

Commit 95935ba

Browse files
committed
fix: handle floats in cross-match local mode filters, add congruence tests
1 parent a270fb9 commit 95935ba

3 files changed

Lines changed: 76 additions & 45 deletions

File tree

‎qdrant_client/local/payload_filters.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,14 @@ def make_condition_tz_aware(dt: datetime | date | None) -> datetime | None:
149149

150150

151151
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.
152+
# Qdrant keeps bool, integer and float as distinct payload value types for exact
153+
# match, but Python cross-matches them (bool is a subclass of int, and 1 == 1.0).
154+
# Match condition operands are only ever bool / int / str (never float), so a float
155+
# payload value can never be matched by an exact-match condition on the server.
154156
if isinstance(value, bool) != isinstance(other, bool):
155157
return False
158+
if isinstance(value, float) != isinstance(other, float):
159+
return False
156160
return value == other
157161

158162

‎tests/congruence_tests/test_scroll.py‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import random
22

3+
import pytest
4+
5+
from qdrant_client import QdrantClient
36
from qdrant_client.client_base import QdrantBase
47
from qdrant_client.http.models import models
58
from tests.congruence_tests.test_common import (
@@ -100,3 +103,70 @@ def test_sparse_mixed_ids() -> None:
100103
init_client(remote_client, fixture_points, sparse_vectors_config=sparse_vectors_config)
101104

102105
compare_client_results(local_client, remote_client, scroller.scroll_all)
106+
107+
108+
def _init_value_type_collection(client: QdrantBase) -> None:
109+
if client.collection_exists(COLLECTION_NAME):
110+
client.delete_collection(COLLECTION_NAME)
111+
client.create_collection(
112+
collection_name=COLLECTION_NAME,
113+
vectors_config=models.VectorParams(size=2, distance=models.Distance.COSINE),
114+
)
115+
client.upsert(
116+
collection_name=COLLECTION_NAME,
117+
points=[
118+
models.PointStruct(id=1, vector=[0.1, 0.2], payload={"n": 1}),
119+
models.PointStruct(id=2, vector=[0.1, 0.2], payload={"n": True}),
120+
models.PointStruct(id=3, vector=[0.1, 0.2], payload={"n": 0}),
121+
models.PointStruct(id=4, vector=[0.1, 0.2], payload={"n": False}),
122+
models.PointStruct(id=5, vector=[0.1, 0.2], payload={"n": 1.0}),
123+
models.PointStruct(id=6, vector=[0.1, 0.2], payload={"n": 0.0}),
124+
],
125+
wait=True,
126+
)
127+
128+
129+
def _scroll_value_type(client: QdrantBase, scroll_filter: models.Filter) -> list[models.Record]:
130+
records, _next_page = client.scroll(
131+
collection_name=COLLECTION_NAME,
132+
scroll_filter=scroll_filter,
133+
limit=10,
134+
with_payload=True,
135+
)
136+
return records
137+
138+
139+
@pytest.mark.parametrize("prefer_grpc", [True, False])
140+
def test_bool_int_float_filters_do_not_cross_match(prefer_grpc) -> None:
141+
"""Python cross-matches bool / int / float (bool is a subclass of int, and
142+
1 == 1.0), but Qdrant keeps them as distinct payload value types for exact
143+
match. Local mode must not cross-match them, matching the remote server
144+
behaviour. Match condition operands can only be bool / int / str (never float),
145+
so a float payload value is never matched by an exact-match condition.
146+
"""
147+
local_client: QdrantClient = init_local()
148+
_init_value_type_collection(local_client)
149+
150+
remote_client: QdrantClient = init_remote(prefer_grpc=prefer_grpc)
151+
_init_value_type_collection(remote_client)
152+
153+
filters = [
154+
models.Filter(must=[models.FieldCondition(key="n", match=models.MatchValue(value=1))]),
155+
models.Filter(must=[models.FieldCondition(key="n", match=models.MatchValue(value=True))]),
156+
models.Filter(must=[models.FieldCondition(key="n", match=models.MatchValue(value=0))]),
157+
models.Filter(must=[models.FieldCondition(key="n", match=models.MatchValue(value=False))]),
158+
models.Filter(must=[models.FieldCondition(key="n", match=models.MatchAny(any=[1, 0]))]),
159+
models.Filter(
160+
must=[models.FieldCondition(key="n", match=models.MatchExcept(**{"except": [1, 0]}))]
161+
),
162+
models.Filter(must=[models.FieldCondition(key="n", range=models.Range(gte=1, lte=1))]),
163+
models.Filter(must=[models.FieldCondition(key="n", range=models.Range(gte=0, lte=1))]),
164+
]
165+
166+
for scroll_filter in filters:
167+
compare_client_results(
168+
local_client,
169+
remote_client,
170+
_scroll_value_type,
171+
scroll_filter=scroll_filter,
172+
)

‎tests/test_in_memory.py‎

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -294,46 +294,3 @@ 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-
# MatchExcept(except=[1]) must not exclude the True payload (id 2): a bool is not
331-
# the integer 1, so it stays in the result even though True == 1 in Python.
332-
assert matched_ids(
333-
models.FieldCondition(key="n", match=models.MatchExcept(**{"except": [1]}))
334-
) == [2, 3, 4]
335-
assert matched_ids(models.FieldCondition(key="n", range=models.Range(gte=1, lte=1))) == [1]
336-
# A single-value MatchAny and a numeric range that True would satisfy must exclude the
337-
# bool payload too, locking in the isolated MatchAny and check_range paths.
338-
assert matched_ids(models.FieldCondition(key="n", match=models.MatchAny(any=[1]))) == [1]
339-
assert matched_ids(models.FieldCondition(key="n", range=models.Range(gt=0))) == [1]

0 commit comments

Comments
 (0)