10
10
# or implied. See the License for the specific language governing permissions and limitations under
11
11
# the License.
12
12
13
+ import json
13
14
import logging
14
15
from threading import Lock
15
16
17
+ import numpy as np
18
+
16
19
from pymilvus .client .types import DataType
17
20
from pymilvus .exceptions import MilvusException
18
21
from pymilvus .orm .schema import CollectionSchema
@@ -85,6 +88,16 @@ def commit(self, **kwargs):
85
88
def data_path (self ):
86
89
return ""
87
90
91
+ def _try_convert_json (self , field_name : str , obj : object ):
92
+ if isinstance (obj , str ):
93
+ try :
94
+ return json .loads (obj )
95
+ except Exception as e :
96
+ self ._throw (
97
+ f"Illegal JSON value for field '{ field_name } ', type mismatch or illegal format, error: { e } "
98
+ )
99
+ return obj
100
+
88
101
def _throw (self , msg : str ):
89
102
logger .error (msg )
90
103
raise MilvusException (message = msg )
@@ -109,10 +122,12 @@ def _verify_row(self, row: dict):
109
122
dtype = DataType (field .dtype )
110
123
validator = TYPE_VALIDATOR [dtype .name ]
111
124
if dtype in {DataType .BINARY_VECTOR , DataType .FLOAT_VECTOR }:
125
+ if isinstance (row [field .name ], np .ndarray ):
126
+ row [field .name ] = row [field .name ].tolist ()
112
127
dim = field .params ["dim" ]
113
128
if not validator (row [field .name ], dim ):
114
129
self ._throw (
115
- f"Illegal vector data for vector field: '{ dtype .name } ',"
130
+ f"Illegal vector data for vector field: '{ field .name } ',"
116
131
f" dim is not { dim } or type mismatch"
117
132
)
118
133
@@ -126,20 +141,23 @@ def _verify_row(self, row: dict):
126
141
max_len = field .params ["max_length" ]
127
142
if not validator (row [field .name ], max_len ):
128
143
self ._throw (
129
- f"Illegal varchar value for field '{ dtype .name } ',"
144
+ f"Illegal varchar value for field '{ field .name } ',"
130
145
f" length exceeds { max_len } or type mismatch"
131
146
)
132
147
133
148
row_size = row_size + len (row [field .name ])
134
149
elif dtype == DataType .JSON :
150
+ row [field .name ] = self ._try_convert_json (field .name , row [field .name ])
135
151
if not validator (row [field .name ]):
136
- self ._throw (f"Illegal varchar value for field '{ dtype .name } ', type mismatch" )
152
+ self ._throw (f"Illegal JSON value for field '{ field .name } ', type mismatch" )
137
153
138
154
row_size = row_size + len (row [field .name ])
139
155
else :
156
+ if isinstance (row [field .name ], np .generic ):
157
+ row [field .name ] = row [field .name ].item ()
140
158
if not validator (row [field .name ]):
141
159
self ._throw (
142
- f"Illegal scalar value for field '{ dtype .name } ', value overflow or type mismatch"
160
+ f"Illegal scalar value for field '{ field .name } ', value overflow or type mismatch"
143
161
)
144
162
145
163
row_size = row_size + TYPE_SIZE [dtype .name ]
0 commit comments