Skip to content

Commit ee685d4

Browse files
authored
fix: Aviod coping functions when init CollectionSchema (#2903)
See also: #2776 pr: #2902 Signed-off-by: yangxuan <[email protected]>
1 parent 9da22d9 commit ee685d4

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

pymilvus/orm/schema.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,14 @@ def __init__(
100100
self._partition_key_field = None
101101
self._clustering_key_field = None
102102

103-
if functions is None:
104-
functions = []
105-
106-
if not isinstance(functions, list):
107-
raise FunctionsTypeException(message=ExceptionsMessage.FunctionsType)
108-
for function in functions:
109-
if not isinstance(function, Function):
110-
raise SchemaNotReadyException(message=ExceptionsMessage.FunctionIncorrectType)
111-
self._functions = [copy.deepcopy(function) for function in functions]
103+
self._functions = []
104+
if functions:
105+
if not isinstance(functions, list):
106+
raise FunctionsTypeException(message=ExceptionsMessage.FunctionsType)
107+
for function in functions:
108+
if not isinstance(function, Function):
109+
raise SchemaNotReadyException(message=ExceptionsMessage.FunctionIncorrectType)
110+
self._functions = functions
112111

113112
if not isinstance(fields, list):
114113
raise FieldsTypeException(message=ExceptionsMessage.FieldsType)
@@ -118,7 +117,6 @@ def __init__(
118117
self._fields = [copy.deepcopy(field) for field in fields]
119118

120119
self._mark_output_fields()
121-
122120
self._check_kwargs()
123121
if kwargs.get("check_fields", True):
124122
self._check_fields()

tests/test_schema.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
11
import numpy
22
import pytest
33

4-
from pymilvus import CollectionSchema, FieldSchema, DataType, MilvusException
4+
from pymilvus import CollectionSchema, FieldSchema, DataType
55
from utils import *
66
from pymilvus.orm import schema as s
77

88

9+
from pymilvus.orm.schema import Function, FunctionType
10+
911
class TestCollectionSchema:
1012
@pytest.fixture(scope="function")
1113
def raw_dict(self):
12-
_dict = {}
13-
_dict["description"] = "TestCollectionSchema_description"
14-
fields = [
15-
{
16-
"name": "vec1",
17-
"description": "desc1",
18-
"type": DataType.FLOAT_VECTOR,
19-
"params": {"dim": 128},
20-
},
21-
22-
{
23-
"name": "vec2",
24-
"description": "desc2",
25-
"type": DataType.BINARY_VECTOR,
26-
"params": {"dim": 128},
27-
},
28-
{
29-
"name": "ID",
30-
"description": "ID",
31-
"type": DataType.INT64,
32-
"is_primary": True,
33-
"auto_id": False
34-
},
35-
]
36-
_dict["fields"] = fields
37-
_dict["enable_dynamic_field"] = True
38-
39-
return _dict
14+
return {
15+
"description": "TestCollectionSchema_description",
16+
"enable_dynamic_field": True,
17+
"fields": [
18+
{
19+
"name": "vec1",
20+
"description": "desc1",
21+
"type": DataType.FLOAT_VECTOR,
22+
"params": {"dim": 128},
23+
},
24+
{
25+
"name": "vec2",
26+
"description": "desc2",
27+
"type": DataType.BINARY_VECTOR,
28+
"params": {"dim": 128},
29+
},
30+
{
31+
"name": "ID",
32+
"description": "ID",
33+
"type": DataType.INT64,
34+
"is_primary": True,
35+
"auto_id": False
36+
},
37+
]
38+
}
4039

4140
def test_constructor_from_dict(self, raw_dict):
4241
schema = CollectionSchema.construct_from_dict(raw_dict)
@@ -54,6 +53,14 @@ def test_to_dict(self, raw_dict):
5453
assert target == raw_dict
5554
assert target is not raw_dict
5655

56+
def test_init_with_functions(self, raw_dict):
57+
functions = [
58+
Function("func1", FunctionType.BM25, ["field1"], ["field2"])
59+
]
60+
schema = CollectionSchema.construct_from_dict(raw_dict)
61+
schema_with_func = CollectionSchema(schema.fields, schema.description, functions=functions)
62+
assert schema_with_func.functions == functions
63+
5764

5865
class TestFieldSchema:
5966
@pytest.fixture(scope="function")

0 commit comments

Comments
 (0)