fix(pgvector): raise ValueError when 'in'/'nin' filter value is not a list (#6879)
This commit is contained in:
@@ -89,6 +89,10 @@ def _build_filter_conditions(filters):
|
|||||||
raise ValueError(f"Unsupported filter operator: {op}")
|
raise ValueError(f"Unsupported filter operator: {op}")
|
||||||
template, is_numeric = OPERATOR_SQL_MAP[op]
|
template, is_numeric = OPERATOR_SQL_MAP[op]
|
||||||
if op in ("in", "nin"):
|
if op in ("in", "nin"):
|
||||||
|
if not isinstance(op_value, list):
|
||||||
|
raise ValueError(
|
||||||
|
f"Filter operator {op!r} for key {key!r} requires a list value, got {type(op_value).__name__}"
|
||||||
|
)
|
||||||
str_list = [str(v) for v in op_value]
|
str_list = [str(v) for v in op_value]
|
||||||
conditions.append(template)
|
conditions.append(template)
|
||||||
params.extend([key, str_list])
|
params.extend([key, str_list])
|
||||||
|
|||||||
@@ -2508,3 +2508,20 @@ class TestBuildFilterConditions(unittest.TestCase):
|
|||||||
def test_numeric_scalar_becomes_string(self):
|
def test_numeric_scalar_becomes_string(self):
|
||||||
conditions, params = _build_filter_conditions({"priority": 42})
|
conditions, params = _build_filter_conditions({"priority": 42})
|
||||||
self.assertEqual(params, ["priority", "42"])
|
self.assertEqual(params, ["priority", "42"])
|
||||||
|
|
||||||
|
def test_in_rejects_string_value(self):
|
||||||
|
"""Passing a string to 'in' would iterate characters and produce a misleading ANY() clause."""
|
||||||
|
with self.assertRaises(ValueError, msg="Expected ValueError for non-list 'in' value"):
|
||||||
|
_build_filter_conditions({"user_id": {"in": "alice"}})
|
||||||
|
|
||||||
|
def test_in_rejects_dict_value(self):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
_build_filter_conditions({"user_id": {"in": {"$gt": 0}}})
|
||||||
|
|
||||||
|
def test_nin_rejects_string_value(self):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
_build_filter_conditions({"user_id": {"nin": "alice"}})
|
||||||
|
|
||||||
|
def test_in_accepts_list_value(self):
|
||||||
|
conditions, params = _build_filter_conditions({"user_id": {"in": ["alice", "bob"]}})
|
||||||
|
self.assertEqual(params, ["user_id", ["alice", "bob"]])
|
||||||
|
|||||||
Reference in New Issue
Block a user