Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix not check None Type in json and support no need to pass in None when insert row #2275

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pymilvus/client/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def get_fields_by_range(
res = apply_valid_data(
scalars.json_data.data[start:end], field.valid_data, start, end
)
json_dict_list = list(map(ujson.loads, res))
json_dict_list = [ujson.loads(item) if item is not None else item for item in res]
field2data[name] = json_dict_list, field_meta
continue

Expand Down
55 changes: 41 additions & 14 deletions pymilvus/client/entity_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,17 @@ def convert_to_json(obj: object):
return ujson.dumps(obj, ensure_ascii=False).encode(Config.EncodeProtocol)


def convert_to_json_arr(objs: List[object]):
def convert_to_json_arr(objs: List[object], field_info: Any):
arr = []
for obj in objs:
if obj is None:
raise ParamError(message=f"field ({field_info['name']}) expect not None input")
arr.append(convert_to_json(obj))
return arr


def entity_to_json_arr(entity: Dict):
return convert_to_json_arr(entity.get("values", []))
def entity_to_json_arr(entity: Dict, field_info: Any):
return convert_to_json_arr(entity.get("values", []), field_info)


def convert_to_array_arr(objs: List[Any], field_info: Any):
Expand Down Expand Up @@ -243,39 +245,55 @@ def pack_field_value_to_field_data(
field_name = field_info["name"]
if field_type == DataType.BOOL:
try:
field_data.scalars.bool_data.data.append(field_value)
if field_value is None:
field_data.scalars.bool_data.data.extend([])
else:
field_data.scalars.bool_data.data.append(field_value)
except (TypeError, ValueError) as e:
raise DataNotMatchException(
message=ExceptionsMessage.FieldDataInconsistent
% (field_name, "bool", type(field_value))
) from e
elif field_type in (DataType.INT8, DataType.INT16, DataType.INT32):
try:
field_data.scalars.int_data.data.append(field_value)
# need to extend it, or cannot correctly identify field_data.scalars.int_data.data
if field_value is None:
field_data.scalars.int_data.data.extend([])
else:
field_data.scalars.int_data.data.append(field_value)
except (TypeError, ValueError) as e:
raise DataNotMatchException(
message=ExceptionsMessage.FieldDataInconsistent
% (field_name, "int", type(field_value))
) from e
elif field_type == DataType.INT64:
try:
field_data.scalars.long_data.data.append(field_value)
if field_value is None:
field_data.scalars.long_data.data.extend([])
else:
field_data.scalars.long_data.data.append(field_value)
except (TypeError, ValueError) as e:
raise DataNotMatchException(
message=ExceptionsMessage.FieldDataInconsistent
% (field_name, "int64", type(field_value))
) from e
elif field_type == DataType.FLOAT:
try:
field_data.scalars.float_data.data.append(field_value)
if field_value is None:
field_data.scalars.float_data.data.extend([])
else:
field_data.scalars.float_data.data.append(field_value)
except (TypeError, ValueError) as e:
raise DataNotMatchException(
message=ExceptionsMessage.FieldDataInconsistent
% (field_name, "float", type(field_value))
) from e
elif field_type == DataType.DOUBLE:
try:
field_data.scalars.double_data.data.append(field_value)
if field_value is None:
field_data.scalars.double_data.data.extend([])
else:
field_data.scalars.double_data.data.append(field_value)
except (TypeError, ValueError) as e:
raise DataNotMatchException(
message=ExceptionsMessage.FieldDataInconsistent
Expand Down Expand Up @@ -369,25 +387,34 @@ def pack_field_value_to_field_data(
) from e
elif field_type == DataType.VARCHAR:
try:
field_data.scalars.string_data.data.append(
convert_to_str_array(field_value, field_info, CHECK_STR_ARRAY)
)
if field_value is None:
field_data.scalars.string_data.data.extend([])
else:
field_data.scalars.string_data.data.append(
convert_to_str_array(field_value, field_info, CHECK_STR_ARRAY)
)
except (TypeError, ValueError) as e:
raise DataNotMatchException(
message=ExceptionsMessage.FieldDataInconsistent
% (field_name, "varchar", type(field_value))
) from e
elif field_type == DataType.JSON:
try:
field_data.scalars.json_data.data.append(convert_to_json(field_value))
if field_value is None:
field_data.scalars.json_data.data.extend([])
else:
field_data.scalars.json_data.data.append(convert_to_json(field_value))
except (TypeError, ValueError) as e:
raise DataNotMatchException(
message=ExceptionsMessage.FieldDataInconsistent
% (field_name, "json", type(field_value))
) from e
elif field_type == DataType.ARRAY:
try:
field_data.scalars.array_data.data.append(convert_to_array(field_value, field_info))
if field_value is None:
field_data.scalars.array_data.data.extend([])
else:
field_data.scalars.array_data.data.append(convert_to_array(field_value, field_info))
except (TypeError, ValueError) as e:
raise DataNotMatchException(
message=ExceptionsMessage.FieldDataInconsistent
Expand Down Expand Up @@ -507,7 +534,7 @@ def entity_to_field_data(entity: Any, field_info: Any, num_rows: int):
) from e
elif entity_type == DataType.JSON:
try:
field_data.scalars.json_data.data.extend(entity_to_json_arr(entity))
field_data.scalars.json_data.data.extend(entity_to_json_arr(entity, field_info))
except (TypeError, ValueError) as e:
raise DataNotMatchException(
message=ExceptionsMessage.FieldDataInconsistent
Expand Down
16 changes: 14 additions & 2 deletions pymilvus/client/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,20 @@ def _parse_row_request(
"default_value", None
):
field_data.valid_data.append(v is not None)
if v is not None:
entity_helper.pack_field_value_to_field_data(v, field_data, field_info)
entity_helper.pack_field_value_to_field_data(v, field_data, field_info)
for field in fields_info:
key = field["name"]
if key in entity or field.get("auto_id", False):
continue

field_info, field_data = field_info_map[key], fields_data[key]
if field_info.get("nullable", False) or field_info.get("default_value", None):
field_data.valid_data.append(False)
entity_helper.pack_field_value_to_field_data(None, field_data, field_info)
else:
raise DataNotMatchException(
message=ExceptionsMessage.InsertMissedField % key
)
json_dict = {
k: v for k, v in entity.items() if k not in fields_data and enable_dynamic
}
Expand Down
3 changes: 3 additions & 0 deletions pymilvus/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ class ExceptionsMessage:
InsertUnexpectedField = (
"Attempt to insert an unexpected field `%s` to collection without enabling dynamic field"
)
InsertMissedField = (
"Insert missed an field `%s` to collection without set nullable==true or set default_value"
)
UpsertAutoIDTrue = "Upsert don't support autoid == true"
AmbiguousDeleteFilterParam = (
"Ambiguous filter parameter, only one deletion condition can be specified."
Expand Down
18 changes: 18 additions & 0 deletions tests/test_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,24 @@ def test_row_insert_param_with_auto_id(self):
]

Prepare.row_insert_param("", rows, "", fields_info=schema.to_dict()["fields"], enable_dynamic=True)

def test_row_insert_param_with_none(self):
import numpy as np
rng = np.random.default_rng(seed=19530)
dim = 8
schema = CollectionSchema([
FieldSchema("float_vector", DataType.FLOAT_VECTOR, dim=dim),
FieldSchema("nullable_field", DataType.INT64, nullable=True),
FieldSchema("default_field", DataType.FLOAT, default_value=10),
FieldSchema("pk_field", DataType.INT64, is_primary=True, auto_id=True),
FieldSchema("float", DataType.DOUBLE),
])
rows = [
{"float": 1.0,"nullable_field": None, "default_field": None,"float_vector": rng.random((1, dim))[0], "a": 1},
{"float": 1.0, "float_vector": rng.random((1, dim))[0], "b": 1},
]

Prepare.row_insert_param("", rows, "", fields_info=schema.to_dict()["fields"], enable_dynamic=True)


class TestAlterCollectionRequest:
Expand Down