Skip to content

Commit 9b0f35d

Browse files
XuanYang-cnandrii-shchuriamjoker021jsirois
authored
enhance: Cherry pick multiple prs from master branch (#3048)
- enhance: Replace ujson with orjson (#2993) - Add PK in the upsert response (#3035) - fix: Declare `pymilvus` as a namespace package. (#3040) See also: #1354, #3033, #3039 --------- Signed-off-by: yangxuan <[email protected]> Co-authored-by: Andrii Shchur <[email protected]> Co-authored-by: Andrii Shchur <[email protected]> Co-authored-by: Joker021 <[email protected]> Co-authored-by: John Sirois <[email protected]>
1 parent 0159b0a commit 9b0f35d

File tree

14 files changed

+46
-31
lines changed

14 files changed

+46
-31
lines changed

pymilvus/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
# or implied. See the License for the specific language governing permissions and limitations under
1111
# the License.
1212

13+
# Ensure `pymilvus` is a namespace package other distributions (like `pymilvus.model`) can
14+
# participate in.
15+
from pkgutil import extend_path
16+
17+
__path__ = extend_path(__path__, __name__)
18+
1319
from .client import __version__
1420
from .client.abstract import AnnSearchRequest, RRFRanker, WeightedRanker
1521
from .client.asynch import SearchFuture

pymilvus/client/abstract.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
from typing import Any, Dict, List, Optional, Union
44

5-
import ujson
5+
import orjson
66

77
from pymilvus.exceptions import DataTypeNotMatchException, ExceptionsMessage
88
from pymilvus.settings import Config
@@ -60,7 +60,7 @@ def __pack(self, raw: Any):
6060
for type_param in raw.type_params:
6161
if type_param.key == "params":
6262
try:
63-
self.params[type_param.key] = ujson.loads(type_param.value)
63+
self.params[type_param.key] = orjson.loads(type_param.value)
6464
except Exception as e:
6565
logger.error(
6666
f"FieldSchema::__pack::65::Failed to load JSON type_param.value: {e}, original data: {type_param.value}"
@@ -92,7 +92,7 @@ def __pack(self, raw: Any):
9292
for index_param in raw.index_params:
9393
if index_param.key == "params":
9494
try:
95-
index_dict[index_param.key] = ujson.loads(index_param.value)
95+
index_dict[index_param.key] = orjson.loads(index_param.value)
9696
except Exception as e:
9797
logger.error(
9898
f"FieldSchema::__pack::92::Failed to load JSON index_param.value: {e}, original data: {index_param.value}"

pymilvus/client/entity_helper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Any, Dict, Iterable, List, Optional
55

66
import numpy as np
7-
import ujson
7+
import orjson
88

99
from pymilvus.exceptions import (
1010
DataNotMatchException,
@@ -201,7 +201,7 @@ def preprocess_numpy_types(obj: Any):
201201
if isinstance(obj, str):
202202
try:
203203
# Validate JSON string by parsing it
204-
parsed_obj = ujson.loads(obj)
204+
parsed_obj = orjson.loads(obj)
205205
# If it's a valid JSON string, validate dict keys if it's a dict
206206
if isinstance(parsed_obj, dict):
207207
for k in parsed_obj:
@@ -225,7 +225,7 @@ def preprocess_numpy_types(obj: Any):
225225

226226
processed_obj = preprocess_numpy_types(obj)
227227

228-
return ujson.dumps(processed_obj, ensure_ascii=False).encode(Config.EncodeProtocol)
228+
return orjson.dumps(processed_obj)
229229

230230

231231
def convert_to_json_arr(objs: List[object], field_info: Any):
@@ -933,7 +933,7 @@ def check_append(field_data: Any, row_data: Dict):
933933
row_data[field_data.field_name] = None
934934
return
935935
try:
936-
json_dict = ujson.loads(field_data.scalars.json_data.data[index])
936+
json_dict = orjson.loads(field_data.scalars.json_data.data[index])
937937
except Exception as e:
938938
logger.error(
939939
f"extract_row_data_from_fields_data::Failed to load JSON data: {e}, original data: {field_data.scalars.json_data.data[index]}"

pymilvus/client/prepare.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Any, Dict, Iterable, List, Mapping, Optional, Union
55

66
import numpy as np
7-
import ujson
7+
import orjson
88

99
from pymilvus.exceptions import DataNotMatchException, ExceptionsMessage, ParamError
1010
from pymilvus.grpc_gen import common_pb2
@@ -19,6 +19,7 @@
1919
isVectorDataType,
2020
)
2121
from pymilvus.orm.types import infer_dtype_by_scalar_data
22+
from pymilvus.settings import Config
2223

2324
from . import __version__, blob, check, entity_helper, ts_utils, utils
2425
from .abstract import BaseRanker
@@ -161,7 +162,11 @@ def get_schema_from_collection_schema(
161162
for k, v in f.params.items():
162163
kv_pair = common_types.KeyValuePair(
163164
key=str(k) if k != "mmap_enabled" else "mmap.enabled",
164-
value=ujson.dumps(v) if not isinstance(v, str) else str(v),
165+
value=(
166+
orjson.dumps(v).decode(Config.EncodeProtocol)
167+
if not isinstance(v, str)
168+
else str(v)
169+
),
165170
)
166171
field_schema.type_params.append(kv_pair)
167172

@@ -2311,7 +2316,7 @@ def run_analyzer(
23112316

23122317
if analyzer_params is not None:
23132318
if isinstance(analyzer_params, dict):
2314-
req.analyzer_params = ujson.dumps(analyzer_params)
2319+
req.analyzer_params = orjson.dumps(analyzer_params).decode(Config.EncodeProtocol)
23152320
else:
23162321
req.analyzer_params = analyzer_params
23172322

pymilvus/client/search_result.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Any, Dict, List, Optional, Tuple, Union
33

44
import numpy as np
5-
import ujson
5+
import orjson
66

77
from pymilvus.client.types import DataType
88
from pymilvus.exceptions import MilvusException
@@ -152,7 +152,7 @@ def materialize(self):
152152
json_data = field_data.scalars.json_data.data[idx]
153153
try:
154154
json_dict_list = (
155-
ujson.loads(json_data) if json_data is not None else None
155+
orjson.loads(json_data) if json_data is not None else None
156156
)
157157
except Exception as e:
158158
logger.error(
@@ -420,7 +420,7 @@ def _get_fields_by_range(
420420
for item in res:
421421
if item is not None:
422422
try:
423-
json_dict_list.append(ujson.loads(item))
423+
json_dict_list.append(orjson.loads(item))
424424
except Exception as e:
425425
logger.error(
426426
f"SearchResult::_get_fields_by_range::Failed to load JSON item: {e}, original item: {item}"
@@ -809,7 +809,7 @@ def extract_struct_field_value(field_data: schema_pb2.FieldData, index: int) ->
809809
return field_data.scalars.string_data.data[index]
810810
elif field_data.type == DataType.JSON:
811811
if index < len(field_data.scalars.json_data.data):
812-
return ujson.loads(field_data.scalars.json_data.data[index])
812+
return orjson.loads(field_data.scalars.json_data.data[index])
813813
elif field_data.type == DataType.FLOAT_VECTOR:
814814
dim = field_data.vectors.dim
815815
start_idx = index * dim

pymilvus/client/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Any, ClassVar, Dict, List, Optional, TypeVar, Union
55

66
import numpy as np
7-
import ujson
7+
import orjson
88

99
from pymilvus.exceptions import (
1010
AutoIDException,
@@ -1081,7 +1081,7 @@ def _extract_lazy_fields(self, index: int, field_data: Any, row_data: Dict) -> A
10811081
row_data[field_data.field_name] = None
10821082
return
10831083
try:
1084-
json_dict = ujson.loads(field_data.scalars.json_data.data[index])
1084+
json_dict = orjson.loads(field_data.scalars.json_data.data[index])
10851085
except Exception as e:
10861086
logger.error(
10871087
f"HybridExtraList::_extract_lazy_fields::Failed to load JSON data: {e}, original data: {field_data.scalars.json_data.data[index]}"

pymilvus/client/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
from datetime import timedelta
66
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Tuple, Union
77

8-
import ujson
8+
import orjson
99

1010
from pymilvus.exceptions import MilvusException, ParamError
1111
from pymilvus.grpc_gen.common_pb2 import Status
12+
from pymilvus.settings import Config
1213

1314
from .constants import LOGICAL_BITS, LOGICAL_BITS_MASK
1415
from .types import DataType
@@ -308,7 +309,7 @@ def get_server_type(host: str):
308309

309310

310311
def dumps(v: Union[dict, str]) -> str:
311-
return ujson.dumps(v) if isinstance(v, dict) else str(v)
312+
return orjson.dumps(v).decode(Config.EncodeProtocol) if isinstance(v, dict) else str(v)
312313

313314

314315
class SciPyHelper:

pymilvus/milvus_client/async_milvus_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ async def upsert(
338338
raise TypeError(msg)
339339

340340
if len(data) == 0:
341-
return {"upsert_count": 0}
341+
return {"upsert_count": 0, "ids": []}
342342

343343
conn = self._get_connection()
344344
# Upsert into the collection.
@@ -353,6 +353,7 @@ async def upsert(
353353
{
354354
"upsert_count": res.upsert_count,
355355
"cost": res.cost,
356+
"ids": res.primary_keys,
356357
}
357358
)
358359

pymilvus/milvus_client/milvus_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def upsert(
272272
raise TypeError(msg)
273273

274274
if len(data) == 0:
275-
return {"upsert_count": 0}
275+
return {"upsert_count": 0, "ids": []}
276276

277277
conn = self._get_connection()
278278
# Upsert into the collection.
@@ -289,7 +289,7 @@ def upsert(
289289
"cost": res.cost,
290290
# milvus server supports upsert on autoid=ture from v2.4.15
291291
# upsert on autoid=ture will return new ids for user
292-
"primary_keys": res.primary_keys,
292+
"ids": res.primary_keys,
293293
}
294294
)
295295

pymilvus/orm/partition.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212

1313
from typing import Dict, List, Optional, TypeVar, Union
1414

15+
import orjson
1516
import pandas as pd
16-
import ujson
1717

1818
from pymilvus.client import utils
1919
from pymilvus.client.abstract import BaseRanker
2020
from pymilvus.client.search_result import SearchResult
2121
from pymilvus.client.types import Replica
2222
from pymilvus.exceptions import MilvusException
23+
from pymilvus.settings import Config
2324

2425
from .mutation import MutationResult
2526

@@ -57,13 +58,13 @@ def __init__(
5758
conn.create_partition(self._collection.name, self.name, **kwargs)
5859

5960
def __repr__(self) -> str:
60-
return ujson.dumps(
61+
return orjson.dumps(
6162
{
6263
"name": self.name,
6364
"collection_name": self._collection.name,
6465
"description": self.description,
6566
}
66-
)
67+
).decode(Config.EncodeProtocol)
6768

6869
def _get_connection(self):
6970
return self._collection._get_connection()

0 commit comments

Comments
 (0)