Skip to content

Commit 979154a

Browse files
committed
fix(python): preserve unsupported unary vector filters
Limit constant folding to unary minus of exact int/float literals. Preserve boolean and unary-plus rejection, and cover negative integer and float filters without a live Qdrant client. Prepared with AI assistance; reviewed independently. Focused and related unit suites: 133 passed. Full repository and live-service tests were not run.
1 parent 95d9f97 commit 979154a

2 files changed

Lines changed: 37 additions & 25 deletions

File tree

‎python/semantic_kernel/data/vector.py‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import operator
66
import sys
77
from abc import abstractmethod
8-
from ast import AST, Constant, Lambda, NodeTransformer, NodeVisitor, UAdd, USub, UnaryOp, copy_location, expr, parse
8+
from ast import AST, Constant, Lambda, NodeTransformer, NodeVisitor, UnaryOp, USub, copy_location, expr, parse
99
from collections.abc import AsyncIterable, Callable, Mapping, Sequence
1010
from copy import deepcopy
1111
from dataclasses import dataclass
@@ -715,7 +715,7 @@ class GetFilteredRecordOptions:
715715

716716

717717
class _FoldUnaryNumericConstants(NodeTransformer):
718-
"""Fold ``-5`` / ``+5`` AST nodes into numeric constants before filter parsers run.
718+
"""Fold negative numeric literals into constants before filter parsers run.
719719
720720
Python parses negative literals as ``UnaryOp(USub, Constant(n))``. Most vector-store
721721
``_lambda_parser`` implementations reject unary ``+/-`` wholesale, so filters such as
@@ -724,13 +724,14 @@ class _FoldUnaryNumericConstants(NodeTransformer):
724724
"""
725725

726726
def visit_UnaryOp(self, node: UnaryOp) -> AST:
727-
"""Fold unary +/- of an int/float constant; leave other unary ops alone."""
727+
"""Fold unary minus of an int/float constant; leave other unary ops alone."""
728728
self.generic_visit(node)
729-
if isinstance(node.operand, Constant) and isinstance(node.operand.value, (int, float)):
730-
if isinstance(node.op, USub):
731-
return copy_location(Constant(-node.operand.value), node)
732-
if isinstance(node.op, UAdd):
733-
return copy_location(Constant(+node.operand.value), node)
729+
if (
730+
isinstance(node.op, USub)
731+
and isinstance(node.operand, Constant)
732+
and type(node.operand.value) in (int, float)
733+
):
734+
return copy_location(Constant(-node.operand.value), node)
734735
return node
735736

736737

@@ -2011,7 +2012,7 @@ def _build_filter(self, search_filter: OptionalOneOrMany[Callable | str] | None)
20112012
for filter_ in filters:
20122013
# parse lambda expression with AST
20132014
tree = parse(filter_ if isinstance(filter_, str) else getsource(filter_).strip())
2014-
# Fold -5 / +5 into constants so connector parsers see a numeric Constant (#14571).
2015+
# Fold negative literals so connector parsers see a numeric Constant (#14571).
20152016
tree = folder.visit(tree)
20162017
visitor.visit(tree)
20172018
created_filters = visitor.output_filters

‎python/tests/unit/data/test_vector_negative_filter_literals.py‎

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
"""Regression: negative numeric literals in vector-store filter lambdas (#14571)."""
44

5-
from ast import Constant, USub, UnaryOp, parse
6-
5+
from ast import Constant, UnaryOp, dump, parse
76
from unittest.mock import MagicMock
87

98
import pytest
109
from chromadb.api import ClientAPI
10+
from qdrant_client.async_qdrant_client import AsyncQdrantClient
1111
from qdrant_client.models import FieldCondition, Range
1212

1313
from semantic_kernel.connectors.chroma import ChromaCollection
@@ -30,46 +30,57 @@ def numeric_definition() -> VectorStoreCollectionDefinition:
3030
)
3131

3232

33-
def test_fold_unary_numeric_constants_negates_literal():
34-
tree = parse("lambda x: x.price > -5")
33+
@pytest.mark.parametrize("literal,expected", [("-5", -5), ("-5.5", -5.5)])
34+
def test_fold_unary_numeric_constants_negates_literal(literal, expected):
35+
tree = parse(f"lambda x: x.price > {literal}")
3536
folded = _FoldUnaryNumericConstants().visit(tree)
3637
# Body of the lambda should now compare against Constant(-5), not UnaryOp(USub, ...).
3738
compare = folded.body[0].value.body
3839
right = compare.comparators[0]
3940
assert isinstance(right, Constant)
40-
assert right.value == -5
41+
assert right.value == expected
4142
assert not isinstance(right, UnaryOp)
4243

4344

44-
def test_chroma_build_filter_accepts_negative_literal(numeric_definition):
45+
@pytest.mark.parametrize("expression", ["-True", "-False", "+True", "+False", "+5", "+5.5", "~5", "-x.price"])
46+
def test_fold_unary_numeric_constants_preserves_other_unary_expressions(expression):
47+
tree = parse(f"lambda x: x.price > {expression}")
48+
original = dump(tree)
49+
folded = _FoldUnaryNumericConstants().visit(tree)
50+
assert dump(folded) == original
51+
52+
53+
@pytest.mark.parametrize("literal,expected", [("-5", -5), ("-5.5", -5.5)])
54+
def test_chroma_build_filter_accepts_negative_literal(numeric_definition, literal, expected):
4555
collection = ChromaCollection(
4656
collection_name="temps",
4757
record_type=dict,
4858
definition=numeric_definition,
4959
client=MagicMock(spec=ClientAPI),
5060
)
51-
parsed = collection._build_filter("lambda x: x.price > -5")
52-
assert parsed == {"price": {"$gt": -5}}
61+
parsed = collection._build_filter(f"lambda x: x.price > {literal}")
62+
assert parsed == {"price": {"$gt": expected}}
5363

5464

55-
def test_chroma_build_filter_accepts_positive_unary_literal(numeric_definition):
65+
@pytest.mark.parametrize("expression", ["-True", "-False", "+True", "+False", "+5", "+5.5", "~5", "-x.price"])
66+
def test_chroma_build_filter_rejects_other_unary_expressions(numeric_definition, expression):
5667
collection = ChromaCollection(
5768
collection_name="temps",
5869
record_type=dict,
5970
definition=numeric_definition,
6071
client=MagicMock(spec=ClientAPI),
6172
)
62-
parsed = collection._build_filter("lambda x: x.price > +5")
63-
assert parsed == {"price": {"$gt": 5}}
73+
with pytest.raises(NotImplementedError, match="Unary"):
74+
collection._build_filter(f"lambda x: x.price > {expression}")
6475

6576

66-
def test_qdrant_build_filter_accepts_negative_literal(numeric_definition, monkeypatch):
67-
monkeypatch.setenv("QDRANT_LOCATION", "http://localhost:6333")
77+
@pytest.mark.parametrize("literal,expected", [("-5", -5), ("-5.5", -5.5)])
78+
def test_qdrant_build_filter_accepts_negative_literal(numeric_definition, literal, expected):
6879
collection = QdrantCollection(
6980
record_type=dict,
7081
collection_name="temps",
7182
definition=numeric_definition,
72-
env_file_path="test.env",
83+
client=MagicMock(spec=AsyncQdrantClient),
7384
)
74-
parsed = collection._build_filter("lambda x: x.price > -5")
75-
assert parsed == FieldCondition(key="price", range=Range(gt=-5))
85+
parsed = collection._build_filter(f"lambda x: x.price > {literal}")
86+
assert parsed == FieldCondition(key="price", range=Range(gt=expected))

0 commit comments

Comments
 (0)